# How to save stdout\_lines to a file without losing the newlines

**URL:** <https://forum.ansible.com/t/how-to-save-stdout-lines-to-a-file-without-losing-the-newlines/22973>\
**Category:** Ansible Project\
**Created:** [November 7, 2016, 3:07pm UTC](https://forum.ansible.com/t/how-to-save-stdout-lines-to-a-file-without-losing-the-newlines/22973 "2016-11-07T15:07:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![actionmystique](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/actionmystique/32/1678_2.png) [@actionmystique](https://forum.ansible.com/u/actionmystique)\
**Post date:** [November 7, 2016, 3:07pm UTC](https://forum.ansible.com/t/how-to-save-stdout-lines-to-a-file-without-losing-the-newlines/22973/1 "2016-11-07T15:07:36Z")

</div>

Hi everyone,

Considering a variable {{ table.stdout\_lines ]] containing the following when printing it out on the terminal:

TASK [save\_table : Printing the returned table] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [IOSv\_L2\_10] =\> {  
“table.stdout\_lines”: [  
[  
“Extended IP access list authorized-snmp-nms”,  
" 10 permit ip host 172.21.100.1 host 172.21.100.210 log",  
“Extended IP access list preauth\_ipv4\_acl (per-user)”,  
" 10 permit udp any any eq domain",  
" 20 permit tcp any any eq domain",  
" 30 permit udp any eq bootps any",  
" 40 permit udp any any eq bootpc",  
" 50 permit udp any eq bootpc any",  
" 60 deny ip any any",  
“IPv6 access list preauth\_ipv6\_acl (per-user)”,  
" permit udp any any eq domain sequence 10",  
" permit tcp any any eq domain sequence 20",  
" permit icmp any any nd-ns sequence 30",  
" permit icmp any any nd-na sequence 40",  
" permit icmp any any router-solicitation sequence 50",  
" permit icmp any any router-advertisement sequence 60",  
" permit icmp any any redirect sequence 70",  
" permit udp any eq 547 any eq 546 sequence 80",  
" permit udp any eq 546 any eq 547 sequence 90",  
" deny ipv6 any any sequence 100"  
]  
]  
}

I need to save those lines into a local file, easy right?

- name: Saving “{{ item }}” into local file  
local\_action: copy content=“{{ table.stdout\_lines }}” dest=“{{ dest\_file }}”

Unfortunately, all newlines are lost rendering the file difficult to read:  
[[“Extended IP access list authorized-snmp-nms”, " 10 permit ip host 172.21.100.1 host 172.21.100.210 log", “Extended IP access list preauth\_ipv4\_acl (per-user)”, " 10 permit udp any any eq domain", " 20 permit tcp any any eq domain", " 30 permit udp any eq bootps any", " 40 permit udp any any eq bootpc", " 50 permit udp any eq bootpc any", " 60 deny ip any any", “IPv6 access list preauth\_ipv6\_acl (per-user)”, " permit udp any any eq domain sequence 10", " permit tcp any any eq domain sequence 20", " permit icmp any any nd-ns sequence 30", " permit icmp any any nd-na sequence 40", " permit icmp any any router-solicitation sequence 50", " permit icmp any any router-advertisement sequence 60", " permit icmp any any redirect sequence 70", " permit udp any eq 547 any eq 546 sequence 80", " permit udp any eq 546 any eq 547 sequence 90", " deny ipv6 any any sequence 100"]]

Anyone has a brilliant simple idea to save {{ table.stdout\_lines }} with all newlines in the file?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 7, 2016, 3:38pm UTC](https://forum.ansible.com/t/how-to-save-stdout-lines-to-a-file-without-losing-the-newlines/22973/2 "2016-11-07T15:38:42Z")

</div>

Yes, use table.stdout 🙂

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [November 7, 2016, 3:41pm UTC](https://forum.ansible.com/t/how-to-save-stdout-lines-to-a-file-without-losing-the-newlines/22973/3 "2016-11-07T15:41:05Z")

</div>

Have you tried the following?

{{table.stdout\_lines|join(‘\n’) }}

Not tested but sounds like it might do what you want.

Jon

---

<div class="post-metadata">

**Author:** ![actionmystique](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/actionmystique/32/1678_2.png) [@actionmystique](https://forum.ansible.com/u/actionmystique)\
**Post date:** [November 7, 2016, 4:17pm UTC](https://forum.ansible.com/t/how-to-save-stdout-lines-to-a-file-without-losing-the-newlines/22973/4 "2016-11-07T16:17:31Z")

</div>

@Kai Stian Olstad: stdout adds literal ‘\n’ to each line

@Jon: |join(‘\n’) has no effect on stdout\_lines

However, if I combine both proposals into **table.stdout|join(‘\n’)**, it works! Thanks guys 😉

For instance:

Mac Address Table

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 7, 2016, 4:27pm UTC](https://forum.ansible.com/t/how-to-save-stdout-lines-to-a-file-without-losing-the-newlines/22973/5 "2016-11-07T16:27:29Z")

</div>

That's strange, i use that all the time and get correct newlines.

---

<div class="post-metadata">

**Author:** ![actionmystique](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/actionmystique/32/1678_2.png) [@actionmystique](https://forum.ansible.com/u/actionmystique)\
**Post date:** [November 7, 2016, 5:10pm UTC](https://forum.ansible.com/t/how-to-save-stdout-lines-to-a-file-without-losing-the-newlines/22973/6 "2016-11-07T17:10:59Z")

</div>

I suppose it depends on how the data are returned by the target device, which is not a Linux server here, but a Cisco IOS networking device, which is a very specific beast.

---

<div class="post-metadata">

**Author:** ![psprygada](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@psprygada](https://forum.ansible.com/u/psprygada)\
**Post date:** [November 7, 2016, 6:42pm UTC](https://forum.ansible.com/t/how-to-save-stdout-lines-to-a-file-without-losing-the-newlines/22973/7 "2016-11-07T18:42:28Z")

</div>

Write the stdout var instead.

- name: Saving “{{ item }}” into local file  
local\_action: copy content=“{{ table.stdout[0] }}” dest=“{{ dest\_file }}”

---

<div class="post-metadata">

**Author:** ![actionmystique](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/actionmystique/32/1678_2.png) [@actionmystique](https://forum.ansible.com/u/actionmystique)\
**Post date:** [November 7, 2016, 6:53pm UTC](https://forum.ansible.com/t/how-to-save-stdout-lines-to-a-file-without-losing-the-newlines/22973/8 "2016-11-07T18:53:30Z")

</div>

Thanks, it’s even simpler and it seems to function correctly in **all cases**.

---

<div class="post-metadata">

**Author:** ![actionmystique](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/actionmystique/32/1678_2.png) [@actionmystique](https://forum.ansible.com/u/actionmystique)\
**Post date:** [November 8, 2016, 3:10pm UTC](https://forum.ansible.com/t/how-to-save-stdout-lines-to-a-file-without-losing-the-newlines/22973/9 "2016-11-08T15:10:13Z")

</div>

@Peter Sprygada: I’m now facing a more complicated case where I have trouble accessing stdout[0]:

- name: Fetching PACL\_Table on all L2 interfaces from the remote node  
ios\_command:  
provider: “{{ connections.ssh }}”  
commands:

- “show run interface {{ net\_item.key }} | include ^interface|access-group”  
with\_dict: “{{ ansible\_net\_interfaces }}”  
when: net\_item.value.ipv4.address is not defined  
loop\_control:  
loop\_var: net\_item  
register: pacl\_tables

- name: Printing pacl\_tables.results  
debug: var=pacl\_tables.results

…leads to:

ok: [IOSv\_L2\_10] =\> {  
“pacl\_tables.results”: [  
{  
“\_ansible\_item\_label”: {  
“key”: “GigabitEthernet1/2”,  
“value”: {  
“bandwidth”: 1000000,  
“description”: “Connected to [u’IOSv\_Leaf\_16.actionmystique.net’] on its port [u’Gi0/0’]”,  
“duplex”: “Full”,  
“ipv4”: null,  
“lineprotocol”: "up (connected) ",  
“macaddress”: “0036.2586.7e06”,  
“mediatype”: “unknown media type”,  
“mtu”: 1500,  
“operstatus”: “up”,  
“type”: “iGbE”  
}  
},  
“\_ansible\_item\_result”: true,  
“\_ansible\_no\_log”: false,  
“\_ansible\_parsed”: true,  
“changed”: false,  
“invocation”: {  
“module\_args”: {  
“auth\_pass”: “VALUE\_SPECIFIED\_IN\_NO\_LOG\_PARAMETER”,  
“authorize”: true,  
“commands”: [  
“show run interface GigabitEthernet1/2 | include ^interface|access-group”  
],  
“host”: “172.21.100.210”,  
“interval”: 1,  
“match”: “all”,  
“password”: “VALUE\_SPECIFIED\_IN\_NO\_LOG\_PARAMETER”,  
“port”: 22,  
“provider”: {  
“auth\_pass”: “VALUE\_SPECIFIED\_IN\_NO\_LOG\_PARAMETER”,  
“authorize”: true,  
“host”: “172.21.100.210”,  
“password”: “VALUE\_SPECIFIED\_IN\_NO\_LOG\_PARAMETER”,  
“port”: 22,  
“ssh\_keyfile”: “~/.ssh/id\_rsa”,  
“timeout”: 10,  
“transport”: “cli”,  
“username”: “admin”,  
“version”: 2  
},  
“retries”: 10,  
“ssh\_keyfile”: “/root/.ssh/id\_rsa”,  
“timeout”: 10,  
“transport”: “cli”,  
“use\_ssl”: true,  
“username”: “admin”,  
“validate\_certs”: true,  
“wait\_for”: null  
},  
“module\_name”: “ios\_command”  
},  
“net\_item”: {  
“key”: “GigabitEthernet1/2”,  
“value”: {  
“bandwidth”: 1000000,  
“description”: “Connected to [u’IOSv\_Leaf\_16.actionmystique.net’] on its port [u’Gi0/0’]”,  
“duplex”: “Full”,  
“ipv4”: null,  
“lineprotocol”: "up (connected) ",  
“macaddress”: “0036.2586.7e06”,  
“mediatype”: “unknown media type”,  
“mtu”: 1500,  
“operstatus”: “up”,  
“type”: “iGbE”  
}  
},  
“stdout”: [  
“show run interface GigabitEthernet1/2 | include ^interface|access-$terface GigabitEthernet1/2 | include ^interface|access-g oup\ninterface GigabitEthernet1/2”  
],  
“stdout\_lines”: [  
[  
“show run interface GigabitEthernet1/2 | include ^interface|access-$terface GigabitEthernet1/2 | include ^interface|access-g oup”,  
“interface GigabitEthernet1/2”  
]  
],  
“warnings”:   
},

…repeated n times, for all found L2 interfaces.

However, I cannot access stdout[0] for all these interfaces with:

- name: Printing pacl\_tables.results.stdout  
debug: var=print\_item.stdout[0]  
with\_dict: “{{ pacl\_tables.results }}”  
loop\_control:  
loop\_var: print\_item  
… which leads to:

fatal: [IOSv\_L2\_10]: FAILED! =\> {“failed”: true, “msg”: “with\_dict expects a dict”}

How can we correctly access a table of results? (I’m not mentioning the issue with the strange result itself.)
