# \\r\\n in Ansible output

**URL:** <https://forum.ansible.com/t/r-n-in-ansible-output/4594>\
**Category:** Get Help\
**Created:** [March 27, 2024, 2:20pm UTC](https://forum.ansible.com/t/r-n-in-ansible-output/4594 "2024-03-27T14:20:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![msmith](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/msmith/32/1934_2.png) [@msmith](https://forum.ansible.com/u/msmith)\
**Post date:** [March 27, 2024, 2:20pm UTC](https://forum.ansible.com/t/r-n-in-ansible-output/4594/1 "2024-03-27T14:20:03Z")

</div>

Hi,

I am trying to get info from some old Cisco switches by using a telnet connection as they do not support SSH. I am running a couple of IOS commands and then writing the output to one text file per device. I am using stdout\_callback = yaml which formats the output nicely on screen, but the text files have \r\n for every newline.

Can anyone advise how I can get the text file output to have line breaks instead of the \r\n - ie it should look like the debug output below?

```auto
cat ansible.cfg

[defaults]
inventory = ./inventory.yml
host_key_checking = False
stdout_callback = yaml
callback_whitelist = profile_tasks
callbacks_enabled=ansible.posix.profile_tasks

```

```auto
cat cisco-backup.yml

- name: run show commands
  hosts: all
  gather_facts: false
  tasks:
   - name: Telnet to device and run commands
     telnet:
       user: "{{ ansible_user }}"
       password: "{{ ansible_pass }}"
       login_prompt: 'Username: '
       prompts:
         - '[>#]'
       command:
         - terminal length 0
         - sho clo
         - show int statu
         - sho int tru
     register: out
   - name: Debug Output
     ansible.builtin.debug:
       msg: "{{ out }}"
   - name: Copy output to file
     ansible.builtin.copy:
       content: "{{out}}"
       dest: "{{inventory_hostname}}.txt"
     delegate_to: localhost

```

```auto
ansible-playbook cisco-backup.yml

PLAY [run show commands] ******************************************************************************************************************************************************

TASK [Telnet to device and run commands] **************************************************************************************************************************************
Wednesday 27 March 2024 14:05:44 +0000 (0:00:00.008) 0:00:00.008 *******
changed: [lab-3560-22]

TASK [Debug Output] ***********************************************************************************************************************************************************
Wednesday 27 March 2024 14:05:49 +0000 (0:00:04.831) 0:00:04.839 *******
ok: [lab-3560-22] =>
  msg:
    changed: true
    failed: false
    output:
    - |-
      terminal length 0
      lab-3560-22#
    - |-
      sho clo
      *02:28:38.037 UTC Mon Mar 1 1993
      lab-3560-22#
    - |-
      show int statu

      Port Name Status Vlan Duplex Speed Type
      Fa0/1 connected 1 a-full a-100 10/100BaseTX
      Fa0/2 notconnect 1 auto auto 10/100BaseTX
      Fa0/3 notconnect 1 auto auto 10/100BaseTX
      Fa0/4 notconnect 1 auto auto 10/100BaseTX
      Fa0/5 notconnect 1 auto auto 10/100BaseTX

```

```auto
 cat lab-3560-22.txt

{"changed": true, "failed": false, "output": ["terminal length 0\r\nlab-3560-22#", "sho clo\r\n*02:28:38.037 UTC Mon Mar 1 1993\r\nlab-3560-22#", "show int statu\r\n\r\nPort Name Status Vlan Duplex Speed Type\r\nFa0/1 connected 1 a-full a-100 10/100BaseTX\r\nFa0/2 notconnect 1 auto auto 10/100BaseTX\r\nFa0/3 notconnect 1 auto auto 10/100BaseTX\r\nFa0/4 notconnect 1 auto auto 10/100BaseTX\r\nFa0/5 notconnect 1 auto auto 10/100BaseTX\r\nFa0/6 notconnect 1 auto auto 10/100BaseTX\r\nFa0/7 notconnect 1 auto auto 10/100BaseTX\r\nFa0/8 notconnect 1 auto auto 10/100BaseTX\r\nFa0/9 notconnect 1 auto auto 10/100BaseTX\r\nFa0/10 notconnect 1 auto auto 10/100BaseTX\r\nFa0/11 notconnect 1 auto auto 10/100BaseTX\r\nFa0/12 notconnect 1 auto auto 10/100BaseTX\r\nFa0/13 notconnect 1 auto auto 10/100BaseTX\r\nFa0/14 notconnect 1 auto auto 10/100BaseTX\r\nFa0/15 notconnect 1 auto auto 10/100BaseTX\r\nFa0/16 notconnect 1 auto auto 10/100BaseTX\r\nFa0/17 notconnect 1 auto auto 10/100BaseTX\r\nFa0/18 notconnect 1 auto auto 10/100BaseTX\r\nFa0/19 notconnect 1 auto auto 10/100BaseTX\r\nFa0/20 notconnect 1 auto auto 10/100BaseTX\r\nFa0/21 notconnect 1 auto auto 10/100BaseTX\r\nFa0/22 notconnect 1 auto auto 10/100BaseTX\r\nFa0/23 notconnect 1 auto auto 10/100BaseTX\r\nFa0/24 notconnect 1 auto auto 10/100BaseTX\r\nFa0/25 notconnect 1 auto auto 10/100BaseTX\r\nFa0/26 notconnect 1 auto auto 10/100BaseTX\r\nFa0/27 notconnect 1 auto auto 10/100BaseTX\r\nFa0/28 notconnect 1 auto auto 10/100BaseTX\r\nFa0/29 notconnect 1 auto auto 10/100BaseTX\r\nFa0/30 notconnect 1 auto auto 10/100BaseTX\r\nFa0/31 notconnect 1 auto auto 10/100BaseTX\r\nFa0/32 notconnect 1 auto auto 10/100BaseTX\r\nFa0/33 notconnect 1 auto auto 10/100BaseTX\r\nFa0/34 notconnect 1 auto auto 10/100BaseTX\r\nFa0/35 notconnect 1 auto auto 10/100BaseTX\r\nFa0/36 notconnect 1 auto auto 10/100BaseTX\r\nFa0/37 notconnect 1 auto auto 10/100BaseTX\r\nFa0/38 notconnect 1 auto auto 10/100BaseTX\r\nFa0/39 notconnect 1 auto auto 10/100BaseTX\r\nFa0/40 notconnect 1 auto auto 10/100BaseTX\r\nFa0/41 notconnect 1 auto auto 10/100BaseTX\r\nFa0/42 notconnect 1 auto auto 10/100BaseTX\r\nFa0/43 notconnect 1 auto auto 10/100BaseTX\r\nFa0/44 notconnect 1 auto auto 10/100BaseTX\r\nFa0/45 notconnect 1 auto auto 10/100BaseTX\r\nFa0/46 notconnect 1 auto auto 10/100BaseTX\r\nFa0/47 notconnect 1 auto auto 10/100BaseTX\r\nFa0/48 notconnect 1 auto auto 10/100BaseTX\r\nGi0/1 notconnect 1 auto auto Not Present\r\nGi0/2 notconnect 1 auto auto Not Present\r\nGi0/3 notconnect 1 auto auto Not Present\r\nGi0/4 notconnect 1 auto auto Not Present\r\nPo45 notconnect unassigned auto auto \r\nlab-3560-22#", "sho int tru\r\n\r\nlab-3560-22#"]}

```

---

<div class="post-metadata">

**Author:** ![akira6592](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akira6592/32/222_2.png) [@akira6592](https://forum.ansible.com/u/akira6592)\
**Post date:** [March 27, 2024, 2:31pm UTC](https://forum.ansible.com/t/r-n-in-ansible-output/4594/2 "2024-03-27T14:31:20Z")

</div>

@msmith Hello.

How about following:

```auto
   - name: Copy output to file
     ansible.builtin.copy:
       content: "{{ out.output[0] }}"
       dest: "{{ inventory_hostname }}.txt"

```

If you wish only LF not CRLF.

```auto
   - name: Copy output to file
     ansible.builtin.copy:
       content: "{{ out.output[0] | regex_replace('\\r','') }}"
       dest: "{{ inventory_hostname }}.txt"

```

---

<div class="post-metadata">

**Author:** ![msmith](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/msmith/32/1934_2.png) [@msmith](https://forum.ansible.com/u/msmith)\
**Post date:** [March 27, 2024, 2:42pm UTC](https://forum.ansible.com/t/r-n-in-ansible-output/4594/3 "2024-03-27T14:42:49Z")

</div>

Hi Akira

Thank-you very much for your help 😃

This only saves the first line of text, but I want to save multiple (hundreds) lines of text -

```auto
cat lab-3560-22.txt
terminal length 0

```

Do I need to loop through multiple lines somehow?

Thanks,

Mike

---

<div class="post-metadata">

**Author:** ![akira6592](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akira6592/32/222_2.png) [@akira6592](https://forum.ansible.com/u/akira6592)\
**Post date:** [March 27, 2024, 2:53pm UTC](https://forum.ansible.com/t/r-n-in-ansible-output/4594/4 "2024-03-27T14:53:25Z")

</div>

@msmith

Sorry, `out.output[0]` means only first command…

How about below one?

```yaml
   - name: Copy output to file
     ansible.builtin.copy:
       content: "{{ out.output | map('regex_replace', '\\r','') | join('\n') }}"
       dest: "{{ inventory_hostname }}.txt"
     delegate_to: localhost

```

---

<div class="post-metadata">

**Author:** ![msmith](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/msmith/32/1934_2.png) [@msmith](https://forum.ansible.com/u/msmith)\
**Post date:** [March 27, 2024, 3:01pm UTC](https://forum.ansible.com/t/r-n-in-ansible-output/4594/5 "2024-03-27T15:01:10Z")

</div>

That’s perfect! Thanks Akira 😃

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [April 26, 2024, 3:01pm UTC](https://forum.ansible.com/t/r-n-in-ansible-output/4594/6 "2024-04-26T15:01:32Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
