Playbook output is not saved properly in file, please guide

56 views
Skip to first unread message

Vikram S

unread,
Feb 7, 2021, 5:32:48 PM2/7/21
to Ansible Project

I am practising writing ansible playbook using Network automation docker container in GNS3. After running below playbook, only the output of first command is saved (show version) and i don't see the 2nd command's output in file. This happens for any command i run.

Am i doing something wrong or should i export the .txt file to a windows PC to view the complete output?  

(formatting below is not in proper order while pasting here but playbook has the correct format)


---
- name: Backup few config from Cisco router
hosts: R1
gather_facts: false
connection: local

tasks:
- name: run multiple commands on remote nodes
ios_command:
commands:
- show ip arp
- show version

register: print_output

- debug: var=print_output.stdout_lines

- name: save output to a file
copy: content="{{ print_output.stdout[0] }}" dest="./output/R1.txt"


Brian Coca

unread,
Feb 8, 2021, 9:30:23 AM2/8/21
to Ansible Project
This will only print the first element of 'stdout', remove the [0] to
get the full thing.

print_output.stdout[0]

vs

print_output.stdout

--
----------
Brian Coca

Vikram S

unread,
Feb 8, 2021, 12:13:22 PM2/8/21
to Ansible Project
Yes, removing [0] worked!! 
I am learning ansible using a udemy tutorial and i am practising sample playbook in tutorial in GNS3. I am not sure why it worked for the author to save output of multiple commands with [0] present. It's a 3 yr old course, probably something changed in ansible recently.

ahmad aljarah

unread,
Mar 22, 2021, 4:30:32 PM3/22/21
to Ansible Project
i get wrong format below is my script: 
---
- name: BACKUP ROUTER CONFIGURATIONS
  hosts: all
  connection: local
  gather_facts: false
  vars:
    data_dir: foo
  tasks:
    - name: BACKUP THE CONFIG
      iosxr_command:
        commands:
          #- { command: show run }
          - { command: show logging | ex SSH }
          - { command: show ospf neighbor }
          - { command: show bgp all all summary  }
          - { command: show mpls ldp neighbor }      
      register: config_output

    - name: RENAME BACKUP
      copy:
        content: "{{ config_output.stdout }}"
        dest: "/var/lib/awx/projects/XR-ISP-GW-showOSPF/LOGFILE/{{ inventory_hostname }}.txt"
i got something like this:

nAddress Family: IPv4 Unicast\n----------------------------\n\nBGP router identifier 89.211.1.7, local AS number 8781\nBGP generic scan interval 60 secs\nNon-stop routing is enabled\nBGP table state: Active\nTable ID: 0xe0000000   RD version: 1579942429\nBGP main routing table version 1579942429\nBGP NSR Initial initsync version 395208 (Reached)\nBGP NSR/ISSU Sync-Group versions 1579942429/0\nBGP scan interval 60 secs\n\nBGP is operating in STANDALONE mode.\n\n\nProcess       RcvTblVer   bRIB/RIB   LabelVer  ImportVer  SendTblVer  StandbyVer\nSpeaker       1579942429  1579942429  1579942429  1579942429  1579942429  1579942429\n\nNeighbor        Spk    AS MsgRcvd MsgSent   TblVer  InQ OutQ  Up/Down  St/PfxRcd\n72.14.198.144     0 15169 3847027 3304487 1579942429    0    0    29w3d        985\n72.14.211.141     0 15169 3994770 3430527 1579942429    0    0    23w2d        985\n82.148.106.13     0 16509 3955040 3453340 1579942429    0    0     3w0d       1405\n82.148
_________________________________________________________________________________________________________________________
how can it be solved?

phil.gr...@gmail.com

unread,
Mar 22, 2021, 5:49:45 PM3/22/21
to Ansible Project
you might want to use stdout_lines but it would be better to use the https://github.com/ansible-collections/cisco.iosxr/blob/main/docs/cisco.iosxr.iosxr_config_module.rst#ciscoiosxriosxr_config module with the backup parameter, as this should do the job for you.

Vikram S

unread,
Mar 23, 2021, 10:13:35 PM3/23/21
to Ansible Project
Hi,

Use the below syntax and that will fix the issue.

- name: SAVE BACKUP TO A FILE NAMED WITH DEVICE HOSTNAME 
      copy: content="{{ print_backup.stdout | join ('\n') }}" dest="./iosupgrade/{{ inventory_hostname }}.txt"

Regards,
Vikram

Reply all
Reply to author
Forward
0 new messages