Cisco ios_command output command to text file

Hi,

I’m looking to output a number of commands from Cisco switches which i have a playbook that does this, however, what i would like is the name of the command to be printed in the output above the returned value from the switch.

So

show version

output from switch

show inventory

output from switch

Hope that makes sense.

This is the copy of the playbook i’m using at the moment.


  • name: capture show output
    hosts: switches
    gather_facts: no
    connection: network_cli

    tasks:

    • name: show commands
      ios_command:
      commands:
      • show inventory

      • show version

      • show vlan

      • show ip interface brief
        register: config

      • name: save output to local directory
        copy:
        content: “{{ config.stdout | replace(‘\n’, ‘\n’) }}”
        dest: “show-output/{{ inventory_hostname }}.ios”

Any help gratefully appreciated.

1 Like

Hey Baily,

If I’m understanding you properly, you’d like to clean up the output from your playbook a bit to make it more digestible by organizing the output from the commands ran against your network to be output to a file and organized in that file so that you know what command produced which specific output.

If so, I had a playbook lying around designed to grab the version running on the IOS devices in my network, and was able to modify it to hopefully be of some help to you:

  tasks:
    - name: show version
      ios_command:
        commands: show version
      register: show_version_output

    - name: show inventory
      ios_command:
        commands: show inventory
      register: show_inventory_output
  
    - name: Save output to local directory
      local_action:
        module: lineinfile
        dest: "./ansible/playbook_outputs/show_commands.txt"
        create: yes
        line: "Hostname: {{ inventory_hostname }}\nshow version:{{ show_version_output }}\nshow inventory:{{ show_inventory_output }}\n"

The above play saving the output to a local dir simply creates the dir and the file if it hasn’t already, and then it outputs the text following the “line” argument.

The output of what that looks like is below:

Hostname: SW1
show version:{'changed': False, 'stdout': ['Cisco IOS Software, C2960X Software (C2960X-UNIVERSALK9-M).....
show inventory:{'changed': False, 'stdout': ['NAME: "1", DESCR: "WS-C2960X-24PS-L"\nPID: WS-C2960X-24PS.....

I’ve omitted most of the output for brevity, but you can “\n” new line it out and parse the output to what meets your needs.

Here below is the playbook I drew inspiration from in case it helps:

---
- name: Get IOS version

  gather_facts: yes
  
  vars_prompt:
    - name: hosts 
      prompt: "Which group should we run the playbook against?"
      private: no
      default: LAB

    - name: "ansible_user"
      prompt: "Enter your username"
      private: no

    - name: "ansible_password"
      prompt: "Enter your password"
      private: yes

  hosts: "{{hosts}}"

  tasks:

    - name: Save output to local directory
      local_action:
        module: lineinfile
        dest: "./ansible/playbook_outputs/IOS_version_report.txt"
        create: yes
        line: "Hostname:{{ inventory_hostname }}\nPlatform:{{ ansible_facts['net_model'] }}\nIOS Version:{{ ansible_net_version }}\n"

The output of the above playbook looks like this:

Hostname:SW1
Platform:WS-C2960X-24PS-L
IOS Version:15.2(7)E10

Hostname:SW2
Platform:C9200L-48PXG-4X
IOS Version:17.09.04a

Hostname:SW3
Platform:C9200L-24P-4G
IOS Version:17.06.04