# Multiple commands output in a single file

**URL:** <https://forum.ansible.com/t/multiple-commands-output-in-a-single-file/35256>\
**Category:** Ansible Project\
**Created:** [May 31, 2021, 5:58am UTC](https://forum.ansible.com/t/multiple-commands-output-in-a-single-file/35256 "2021-05-31T05:58:17Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Djay\_wadhwa](https://avatars.discourse-cdn.com/v4/letter/d/3bc359/32.png) [@Djay\_wadhwa](https://forum.ansible.com/u/Djay_wadhwa)\
**Post date:** [May 31, 2021, 5:58am UTC](https://forum.ansible.com/t/multiple-commands-output-in-a-single-file/35256/1 "2021-05-31T05:58:17Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![Pushparaj\_Gothandam](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/pushparaj_gothandam/32/5538_2.png) [@Pushparaj\_Gothandam](https://forum.ansible.com/u/Pushparaj_Gothandam)\
**Post date:** [May 31, 2021, 3:29pm UTC](https://forum.ansible.com/t/multiple-commands-output-in-a-single-file/35256/2 "2021-05-31T15:29:00Z")

</div>

```auto
Can you try this

```

```auto
Use this to write several strings into a file:

```

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

```

---

<div class="post-metadata">

**Author:** ![Djay\_wadhwa](https://avatars.discourse-cdn.com/v4/letter/d/3bc359/32.png) [@Djay\_wadhwa](https://forum.ansible.com/u/Djay_wadhwa)\
**Post date:** [May 31, 2021, 11:49pm UTC](https://forum.ansible.com/t/multiple-commands-output-in-a-single-file/35256/3 "2021-05-31T23:49:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Pushparaj\_Gothandam](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/pushparaj_gothandam/32/5538_2.png) [@Pushparaj\_Gothandam](https://forum.ansible.com/u/Pushparaj_Gothandam)\
**Post date:** [June 1, 2021, 1:18am UTC](https://forum.ansible.com/t/multiple-commands-output-in-a-single-file/35256/4 "2021-06-01T01:18:27Z")

</div>

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] }}”

---

<div class="post-metadata">

**Author:** ![Djay\_wadhwa](https://avatars.discourse-cdn.com/v4/letter/d/3bc359/32.png) [@Djay\_wadhwa](https://forum.ansible.com/u/Djay_wadhwa)\
**Post date:** [June 1, 2021, 2:42am UTC](https://forum.ansible.com/t/multiple-commands-output-in-a-single-file/35256/5 "2021-06-01T02:42:02Z")

</div>

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] }}”

---

<div class="post-metadata">

**Author:** ![The\_Vandyy\_Vines](https://avatars.discourse-cdn.com/v4/letter/t/7993a0/32.png) [@The\_Vandyy\_Vines](https://forum.ansible.com/u/The_Vandyy_Vines)\
**Post date:** [June 1, 2021, 6:23am UTC](https://forum.ansible.com/t/multiple-commands-output-in-a-single-file/35256/6 "2021-06-01T06:23:35Z")

</div>

I think you can test once with

with\_items: "{{ print\_output.stdout }}

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [June 1, 2021, 7:54am UTC](https://forum.ansible.com/t/multiple-commands-output-in-a-single-file/35256/7 "2021-06-01T07:54:17Z")

</div>

What does print\_output look like?  
Without that we're just guessing.

Bbit of a confusing variable name btw

---

<div class="post-metadata">

**Author:** ![Djay\_wadhwa](https://avatars.discourse-cdn.com/v4/letter/d/3bc359/32.png) [@Djay\_wadhwa](https://forum.ansible.com/u/Djay_wadhwa)\
**Post date:** [June 1, 2021, 11:10am UTC](https://forum.ansible.com/t/multiple-commands-output-in-a-single-file/35256/8 "2021-06-01T11:10:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [June 1, 2021, 1:16pm UTC](https://forum.ansible.com/t/multiple-commands-output-in-a-single-file/35256/9 "2021-06-01T13:16:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Djay\_wadhwa](https://avatars.discourse-cdn.com/v4/letter/d/3bc359/32.png) [@Djay\_wadhwa](https://forum.ansible.com/u/Djay_wadhwa)\
**Post date:** [June 3, 2021, 5:42am UTC](https://forum.ansible.com/t/multiple-commands-output-in-a-single-file/35256/10 "2021-06-03T05:42:49Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [June 3, 2021, 8:20am UTC](https://forum.ansible.com/t/multiple-commands-output-in-a-single-file/35256/11 "2021-06-03T08:20:07Z")

</div>

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.
