Multiple commands output in a single file

Hi All

I have this ansible code where i need output of all these commands in one file, however, all i am getting is either output of first command or second command if i change the following to 0,1,2 however ansible is not appending/writing all outputs into a single file.

Appreciate if there is a method which members could suggest to make it work.

"{{ print_output.stdout[1] }}

  • name: Name servers

ios_command:

authorize: yes

provider: “{{ cli }}”

commands:

  • show ip name-servers

  • show ip int brief

  • show logging | in host

register: print_output

  • name: save output to a file

copy: content=“{{ print_output.stdout[1] }}” dest=“/opt/ansible/dev/Dheeraj/verf/adnan/NTP/{{ inventory_hostname }}.txt”

Can you try this
Use this to write several strings into a file:

- name: save output to a file
  lineinfile:
    create: yes
    line: "{{item}}"
    path: ./output/{{ inventory_hostname }}.txt
  with_items: "{{ print_output.stdout[1] }}"

Hi Pushpraj,

I am getting the error below if i try to run this with lineinfile as per suggestion.

Please see my playbook as attached. Appreciate your help

There is a indentation error in your playbook

  • name: save output to a file
    lineinfile:
    create: yes
    line: “{{item}}”

path: /opt/ansible/dev/Dheeraj/verf/adnan/Log/{{ inventory_hostname }}.txt
with_items: “{{ print_output.stdout[1] }}”

Hi Pushp,

Script worked but desired outcome not achieved. still getting output of only 1st command ( show ip name-servers) in the file.

  • name: Check Ip routes
    hosts: routers
    gather_facts: false
    connection: local

vars:
cli:
username: “{{ hdn_user_rw }}”
password: “{{ hdn_pass_rw }}”

tasks:

  • name: Name servers
    ios_command:
    authorize: yes
    provider: “{{ cli }}”
    commands:
  • show ip name-servers
  • show run | in ntp
  • show run | in host
  • show run | in address ipv4
  • show run | in logging host
    register: print_output

#- name: save output to a file
#copy: content=“{{ print_output.stdout[0] }}” dest=“/opt/ansible/dev/Dheeraj/verf/adnan/Log/{{ inventory_hostname }}.txt”

  • name: save output to a file
    lineinfile:
    create: yes
    line: “{{item}}”
    path: /opt/ansible/dev/Dheeraj/verf/adnan/Log/{{ inventory_hostname }}.txt
    with_items: “{{ print_output.stdout[0] }}”

I think you can test once with

with_items: "{{ print_output.stdout }}

What does print_output look like?
Without that we're just guessing.

Bbit of a confusing variable name btw

Hi Dick I think if we use print_output.stdout[1] }} without 0 or 1 in it, it will throw a syntax error.

Print_output.stdout alone might be invalid.

Thanks

Please let's not waste more time by thinking of what might be.
Instead post the value of the print_output (as I already requested),
so we can know for sure.

Hi D

Ran this code and got the error.

  • name: TACACS
    ios_command:
    authorize: yes
    provider: “{{ cli }}”
    commands:
  • show run | in address ipv4
  • show ver

register: print_output

  • debug: var=print_output.stdout_lines

  • name: save output to a file
    copy: content=“{{ print_output.stdout }}” dest=“/opt/ansible/dev/Dheeraj/verf/adnan/TACACS/{{ inventory_hostname }}.txt”

Hi

I asked explicitly (twice) for the value of the print_output variable.
But you again did something else, which gives an error.
Please try to read carefully and try again with just the print_output variable.