# Cisco ios\_command output command to text file

**URL:** <https://forum.ansible.com/t/cisco-ios-command-output-command-to-text-file/39927>\
**Category:** Get Help\
**Tags:** cisco\
**Created:** [January 30, 2025, 1:23pm UTC](https://forum.ansible.com/t/cisco-ios-command-output-command-to-text-file/39927 "2025-01-30T13:23:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![baily](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@baily](https://forum.ansible.com/u/baily)\
**Post date:** [January 30, 2025, 1:23pm UTC](https://forum.ansible.com/t/cisco-ios-command-output-command-to-text-file/39927/1 "2025-01-30T13:23:54Z")

</div>

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

Any help gratefully appreciated.

---

<div class="post-metadata">

**Author:** ![Jonathan](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jonathan/32/4215_2.png) [@Jonathan](https://forum.ansible.com/u/Jonathan)\
**Post date:** [January 30, 2025, 4:10pm UTC](https://forum.ansible.com/t/cisco-ios-command-output-command-to-text-file/39927/2 "2025-01-30T16:10:33Z")

</div>

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:

```auto
  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:

```auto
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:

```auto
---
- 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:

```auto
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

```
