\r\n in Ansible output

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?

cat ansible.cfg

[defaults]
inventory = ./inventory.yml
host_key_checking = False
stdout_callback = yaml
callback_whitelist = profile_tasks
callbacks_enabled=ansible.posix.profile_tasks
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
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
 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#"]}

@msmith Hello.

How about following:

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

If you wish only LF not CRLF.

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

Hi Akira

Thank-you very much for your help :smiley:

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

cat lab-3560-22.txt
terminal length 0

Do I need to loop through multiple lines somehow?

Thanks,

Mike

@msmith

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

How about below one?

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

That’s perfect! Thanks Akira :smiley:

1 Like

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