# Simple cisco IOS show version

**URL:** <https://forum.ansible.com/t/simple-cisco-ios-show-version/22264>\
**Category:** Ansible Project\
**Created:** [August 10, 2016, 1:58pm UTC](https://forum.ansible.com/t/simple-cisco-ios-show-version/22264 "2016-08-10T13:58:07Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bharath\_Bharadwaj](https://avatars.discourse-cdn.com/v4/letter/b/8edcca/32.png) [@Bharath\_Bharadwaj](https://forum.ansible.com/u/Bharath_Bharadwaj)\
**Post date:** [August 10, 2016, 1:58pm UTC](https://forum.ansible.com/t/simple-cisco-ios-show-version/22264/1 "2016-08-10T13:58:07Z")

</div>

Hi All,

I’m new to Ansible and yml, my goal is to automate a part of network operation, I just want to start with a very simple output, copied below is my playbook, I’m trying to run a show version, but i’m getting error when executing the output, yet when i try the same yml script through yml validator, there is no errors.

My Playbook

vars:  
cli:  
host: “{{ network }}”  
username: admin  
password: test@123  
transport: cli

tasks:

- name: run multiple commands on remote nodes  
ios\_command:
- commands: show version
- provider: “{{ cli }}”
- transport: cli

Error:  
"ERROR! playbooks must be a list of plays

The error appears to have been in ‘/etc/ansible/playbooks/cisco\_ios.yml’: line 1, column 1, but may  
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

vars:  
^ here"

---

<div class="post-metadata">

**Author:** ![pixel\_fairy](https://avatars.discourse-cdn.com/v4/letter/p/6f9a4e/32.png) [@pixel\_fairy](https://forum.ansible.com/u/pixel_fairy)\
**Post date:** [August 10, 2016, 3:12pm UTC](https://forum.ansible.com/t/simple-cisco-ios-show-version/22264/2 "2016-08-10T15:12:25Z")

</div>

start with

- hosts: routers  
vars:  
…  
tasks:  
…

---

<div class="post-metadata">

**Author:** ![psprygada](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@psprygada](https://forum.ansible.com/u/psprygada)\
**Post date:** [August 10, 2016, 3:36pm UTC](https://forum.ansible.com/t/simple-cisco-ios-show-version/22264/3 "2016-08-10T15:36:35Z")

</div>

See example here: [https://gist.github.com/privateip/11b042e569585ee9248a](https://gist.github.com/privateip/11b042e569585ee9248a)

---

<div class="post-metadata">

**Author:** ![Bharath\_Bharadwaj](https://avatars.discourse-cdn.com/v4/letter/b/8edcca/32.png) [@Bharath\_Bharadwaj](https://forum.ansible.com/u/Bharath_Bharadwaj)\
**Post date:** [August 11, 2016, 7:36am UTC](https://forum.ansible.com/t/simple-cisco-ios-show-version/22264/4 "2016-08-11T07:36:21Z")

</div>

Tried your example ,it returns with syntax error at line 11, column 9

The offending line appears to be:

- show version  
hosts: “{{ inventory\_hostname }}”  
^ here

---

<div class="post-metadata">

**Author:** ![Bharath\_Bharadwaj](https://avatars.discourse-cdn.com/v4/letter/b/8edcca/32.png) [@Bharath\_Bharadwaj](https://forum.ansible.com/u/Bharath_Bharadwaj)\
**Post date:** [August 11, 2016, 7:55am UTC](https://forum.ansible.com/t/simple-cisco-ios-show-version/22264/5 "2016-08-11T07:55:18Z")

</div>

I tried with your example, this also appears to be having syntax error, error as followed.

The error appears to have been in ‘/etc/ansible/playbooks/cisco\_ios.yml’: line 11, column 9, but may  
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

- show version  
hosts: “{{ inventory\_hostname}}”  
^ here

Regards,

---

<div class="post-metadata">

**Author:** ![gundalow](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/gundalow/32/99_2.png) [@gundalow](https://forum.ansible.com/u/gundalow)\
**Post date:** [August 24, 2016, 7:20am UTC](https://forum.ansible.com/t/simple-cisco-ios-show-version/22264/6 "2016-08-24T07:20:15Z")

</div>

I’ve added a comment with the the corrected playbook sample

> <https://gist.github.com/privateip/11b042e569585ee9248a>

Regards,  
John Barker

---

<div class="post-metadata">

**Author:** ![Bharath\_Bharadwaj](https://avatars.discourse-cdn.com/v4/letter/b/8edcca/32.png) [@Bharath\_Bharadwaj](https://forum.ansible.com/u/Bharath_Bharadwaj)\
**Post date:** [August 24, 2016, 7:44am UTC](https://forum.ansible.com/t/simple-cisco-ios-show-version/22264/7 "2016-08-24T07:44:14Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![Valerie\_P](https://avatars.discourse-cdn.com/v4/letter/v/f475e1/32.png) [@Valerie\_P](https://forum.ansible.com/u/Valerie_P)\
**Post date:** [September 2, 2016, 11:55am UTC](https://forum.ansible.com/t/simple-cisco-ios-show-version/22264/8 "2016-09-02T11:55:21Z")

</div>

Hello John,

I’ve had a little bit of trouble with the ios\_\* modules and thanks to the source found in this sample it is now functional, anyhow I do not understand every line of it, and particularly the “connection: local” one. What is it used for? The ansible documentation refer to the connection: local as a way to make the playbook play locally.

"It may be useful to use a playbook locally, rather than by connecting over SSH. This can be useful for assuring the configuration of a system by putting a playbook in a crontab. This may also be used to run a playbook inside an OS installer, such as an Anaconda kickstart.

To run an entire playbook locally, just set the “hosts:” line to “hosts: 127.0.0.1” and then run the playbook like so:

```auto
ansible-playbook playbook.yml --connection=local

```

Alternatively, a local connection can be used in a single playbook play, even if other plays in the playbook use the default remote connection type:

```auto
- hosts: 127.0.0.1
  connection: local

```

"  
Why is the connection: local parameters a must for it to work?

Thanks in advance!  
Valerie

---

<div class="post-metadata">

**Author:** ![psprygada](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@psprygada](https://forum.ansible.com/u/psprygada)\
**Post date:** [September 3, 2016, 3:28am UTC](https://forum.ansible.com/t/simple-cisco-ios-show-version/22264/9 "2016-09-03T03:28:12Z")

</div>

Hi Valerie,

Since network devices such as IOS do not provide a shell environment nor the ability to download and run arbitrary executables, we are fairly constrained from using the current connection plugin module implemented in core. So in order to build modules that work with network devices, we build an integration that effectively treats SSH or more appropriately said, CLI over SSH like an API. During module execute, we build an SSH session to the remote device for the purposes of sending and receiving commands and output. That is way we must specify connection=local.

Peter

---

<div class="post-metadata">

**Author:** ![Surjeet\_Singh](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@Surjeet\_Singh](https://forum.ansible.com/u/Surjeet_Singh)\
**Post date:** [January 20, 2018, 4:57pm UTC](https://forum.ansible.com/t/simple-cisco-ios-show-version/22264/10 "2018-01-20T16:57:38Z")

</div>

Hi Peter,

I am facing another issue with same i am trying to debug version only from the results of show version using below method but it gives me variable error. however when i run playbook -vvv i can see the show version command is excuted and they are mapped to stdout and stdout\_lines as you mention in document. can you please help me with that or redirect me towards of link.

register: version

- debug: var = version.stdout[0].Version

Regards/surjeet

---

<div class="post-metadata">

**Author:** ![Claudia\_de\_Luna](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@Claudia\_de\_Luna](https://forum.ansible.com/u/Claudia_de_Luna)\
**Post date:** [January 21, 2018, 5:56pm UTC](https://forum.ansible.com/t/simple-cisco-ios-show-version/22264/11 "2018-01-21T17:56:32Z")

</div>

Hi Surjeet,

It might help to understand if you think of your variable “version” as a dictionary of lists of dictionaries. Think of it as a data structure that you need to decompose to understand.

If you take the output you get when you run your playbook and paste it into something like [https://jsoneditoronline.org/](https://jsoneditoronline.org/) you can start to understand the structure and that will make it much easier to pick out the information you need.

for example: I have a playbook that runs “show version” and “show inv”

once I save the output into a variable I called output (equivalent to your “version” variable) I can see that my output structure is a dictionary with three key value pairs: {“changed”: false, “msg”: “all items completed”, results: \<this is a list of dictionaries with the first element [0] having data to do with show version and the second element [1] having data to do with show inv}

You can also see that results[0] has keys like item, stdout, stdout\_lines (which is a list), changed, failed, etc..

If you decompose output.results[0].item you get the command that was run.

`TASK [debug] ******************************************************************************************************************************************************** ok: [arctic-3650] => { "output.results[0].item": "show version" }`

This is the part of my playbook where I try to decompose the data so you see me printing out various parts of the data structure.

# save the command output in a variable called “output”

register: output

# print the contents of the variable “output” which is a dictionary of lists of dictionaries

- debug: var=output

# print the first command that was run

- debug: var=output.results[0].item

# print the command results with line feeds

- debug: var=output.results[0].stdout\_lines

# print the list item #72

- debug: var=output.results[0].stdout\_lines[0][72]

`TASK [debug] ******************************************************************************************************************************************************** ok: [arctic-3650] => { "output.results[0].stdout_lines[0][72]": "* 1 28 WS-C3650-24TS 03.06.06E cat3k_caa-universalk9 INSTALL" }`

Having said all of this because its key to be able to figure out what you get back from the playbook run, give the ios\_facts module a try!

[![](https://us1.discourse-cdn.com/ansible/original/2X/f/f3ea789e4660aacdb98842fd83f776799dcd5a0f.png)](https://lh3.googleusercontent.com/-rKkNzUTPlHg/WmTQutIoufI/AAAAAAAAF0Y/1XK1a-fp8YgaNcGoFGvSR27Wd7EEPFcaACLcBGAs/s1600/2018-01-21_09-37-14.png)

---

<div class="post-metadata">

**Author:** ![Claudia\_de\_Luna](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@Claudia\_de\_Luna](https://forum.ansible.com/u/Claudia_de_Luna)\
**Post date:** [January 21, 2018, 6:29pm UTC](https://forum.ansible.com/t/simple-cisco-ios-show-version/22264/12 "2018-01-21T18:29:18Z")

</div>

Using Ansible ios\_facts module

Here is a simple playbook to gather ios facts.

I took the output and pasted it into one of the may JSON lint/editors on line to easily see the structure and then added a debug statement for the key:value pair I wanted. Version in this case.

`

---

<div class="post-metadata">

**Author:** ![Surjeet\_Singh](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@Surjeet\_Singh](https://forum.ansible.com/u/Surjeet_Singh)\
**Post date:** [January 23, 2018, 4:52am UTC](https://forum.ansible.com/t/simple-cisco-ios-show-version/22264/13 "2018-01-23T04:52:42Z")

</div>

Hi Claudia de Luna,

thank you for your response.

**following your**  **instruction i tried to collect the host name using**  **regular**  **expression used by peter and i can collect ios version.**

**another question is triggered here after looking on your playbook** \*\*:-\*\*

register: facts\_output

- debug: var=facts\_output
- debug: var=facts\_output.ansible\_facts.ansible\_net\_version

question is regarding last line here, in your last line you add ansible\_facts, can you please let me know what is purpose of this and where it came. rest i know about ansible\_net\_version.

also can you please refer me any document how to use regular expression ansible playbook.i have used these in python so i have small understanding in python.

---

<div class="post-metadata">

**Author:** ![Claudia\_de\_Luna](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@Claudia\_de\_Luna](https://forum.ansible.com/u/Claudia_de_Luna)\
**Post date:** [January 23, 2018, 3:50pm UTC](https://forum.ansible.com/t/simple-cisco-ios-show-version/22264/14 "2018-01-23T15:50:01Z")

</div>

Hi Surjeet,

My intent with the second message was to show you the power of the ios/nxos facts modules. These modules return device information in a structured way so that you don’t have to mine your output with regular expressions if it returns the data you are looking for. With the ios facts module you get version, serial number, ip addresses (ipv4/6), hostname, etc…

If you take the output and paste it into [http://jsoneditoronline.org/](http://jsoneditoronline.org/) you can “decompose” the ansible\_facts object. Here is the first part so you can see that ansible\_facts is a dictionary and the first key is ansible\_net\_all\_ipv4\_addresses and the value is a list with two IPs (the two IPv4 ips this switch has configured). The next key would hold all the IPv6 IPs in list but as you can see the list is empty because I don’t have ipv6 configured on this switch.

> “ansible\_facts”: {  
> “ansible\_net\_all\_ipv4\_addresses”: [  
> “10.1.10.25”,  
> “192.0.2.33”  
> ],  
> “ansible\_net\_all\_ipv6\_addresses”: ,  
> “ansible\_net\_filesystems”: [  
> “flash:”  
> ],

In this playbook I use the ios\_facts module to get the version of code in the ansible\_net\_version key value pair that is part of the ansible\_facts “dictionary”.

> tasks:
> 
> - name: Gather IOS Facts  
> ios\_facts:

so the last line

> debug: var=facts\_output.ansible\_facts.ansible\_net\_version

is just printing the value of the ansible\_net\_version key from the ansible\_facts dictionary returned by the ios\_facts module that I stuffed in a varialbe called “facts\_output”

Having said all of that, there are many scenarios where you will need to parse your output for data that has not been nicely packaged up for us in these ansible modules so it is a valuable skill with many approaches. You want to use the one you are most comfortable with but you also don’t want to work any harder than you have to!

Here are some of the approaches I’m aware of and maybe others can chime in with what works for them.

**1. embed regexp in the command you send with the ios\_command module.** Here is sample output from the attached playbook. I’m just sending the regexp as part of the command as you would if you were in the CLI.  
Ethan Banks at Packet Pushers does a n[ice little summary](http://packetpushers.net/rocking-your-show-commands-with-regex/)and I’m sure you can Google a bunch more

> root@e8d7daa45b5b:/ansible/ansible2\_4\_base# ansible-playbook -i hosts get\_ios\_cmd\_filter.yml  
> PLAY [cisco] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> TASK [Show command with embedded regexp inc connected for all Conneced interfaces] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> ok: [arctic-3650] =\> (item=show int status | inc connected)  
> TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> ok: [arctic-3650] =\> {  
> “output.results[0].stdout\_lines”: [  
> [  
> “Gi1/0/4 connected 1 a-full a-1000 10/100/1000BaseTX”  
> ]  
> ]  
> }  
> TASK [Show command with embedded regexp for all IPs] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> ok: [arctic-3650] =\> (item=show ip interface brief | inc .[0-9]++YES)  
> TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> ok: [arctic-3650] =\> {  
> “output.results[0].stdout\_lines”: [  
> [  
> "Vlan1 192.0.2.33 YES manual up up ",  
> “GigabitEthernet0/0 10.1.10.25 YES DHCP up up”  
> ]  
> ]  
> }  
> PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> arctic-3650 : ok=4 changed=0 unreachable=0 failed=0

1. Use the newish jinja2 filters regex\_findall and regex\_search

Ivan Pepelnjak has a nice summary of 1 and 2 [here.](http://automation.ipspace.net/Example:Parsing_Text_Printouts_within_Ansible_Playbooks)

1. Look at the textfsm modules and filters

2. look at napalm getters which return data for a variety of network hardware in a structured way so that you can abstract out your actions in your playbooks across many device types.

Kirk Byer has a very good Ansible series and he covers jinja filters and using the textfsm parsing modules. That is my favorite and I’ve moved most of my parsing scripts to TextFSM these days. Check out the ntc modules from Network to Code (Jason Edelman).

[http://docs.networktocode.com/en/latest/ntc-ansible%20Modules%20(multi-vendor)/modules\_list.html](http://docs.networktocode.com/en/latest/ntc-ansible%20Modules%20(multi-vendor)/modules_list.html)

You can use the jinja2 based filters to do all kinds of things in Ansible but they make my head hurt! (which means I don’t understand them well enough yet) 😃

Good Luck!

Claudia

> **(attachments)**
>
> [get\_ios\_cmd\_filter.yml](https://forum.ansible.com/uploads/short-url/swI4TTUt3bOhn4Ubvo6OTXbggLp.yml) (966 Bytes)

---

<div class="post-metadata">

**Author:** ![Surjeet\_Singh](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@Surjeet\_Singh](https://forum.ansible.com/u/Surjeet_Singh)\
**Post date:** [January 24, 2018, 6:51am UTC](https://forum.ansible.com/t/simple-cisco-ios-show-version/22264/15 "2018-01-24T06:51:35Z")

</div>

Thank you for your response. Please excuse me for my multiple question i am new to this programming world.

now i manage to collect the facts using the ansible get\_fact module. i will further spend time today for regular expression.

my playbook looks like below:

---

<div class="post-metadata">

**Author:** ![Claudia\_de\_Luna](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@Claudia\_de\_Luna](https://forum.ansible.com/u/Claudia_de_Luna)\
**Post date:** [January 24, 2018, 6:57pm UTC](https://forum.ansible.com/t/simple-cisco-ios-show-version/22264/16 "2018-01-24T18:57:56Z")

</div>

Hi Surjeet,

There are a couple of ways to approach this.

What I typically do is save a file of show commands for each device and then process those files using TextFSM.

- local\_action: copy content=“{{ output }}” dest=“./FACTs/{{ inventory\_hostname }}-facts.txt”

To basically concatenate to one file I suspect it would have to involve set\_fact and some processing…maybe a template.

If I get some time I’ll see if I can come up with something that makes sense.

Claudia

---

<div class="post-metadata">

**Author:** ![Surjeet\_Singh](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@Surjeet\_Singh](https://forum.ansible.com/u/Surjeet_Singh)\
**Post date:** [January 24, 2018, 7:02pm UTC](https://forum.ansible.com/t/simple-cisco-ios-show-version/22264/17 "2018-01-24T19:02:27Z")

</div>

Thank you …

---

<div class="post-metadata">

**Author:** ![Surjeet\_Singh](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@Surjeet\_Singh](https://forum.ansible.com/u/Surjeet_Singh)\
**Post date:** [January 27, 2018, 2:29pm UTC](https://forum.ansible.com/t/simple-cisco-ios-show-version/22264/18 "2018-01-27T14:29:10Z")

</div>

Hi Claudia,

i am strugling with with issue in putting textfsm in loop. i can read all my file end with .txt but i have issue in moving for loop so i can run textfsml on all files instead of last file.

can you please help me with that.

import jtextfsm as textfsm  
import glob, os

os.chdir(“/etc/ansible/facts/”)  
for file in glob.glob(“\*\_iosfacts.txt”):  
input\_file = open(file)  
raw\_text\_data = input\_file.read()  
input\_file.close()

# Run the text through the FSM.

# The argument ‘template’ is a file handle and ‘raw\_text\_data’ is a

# string with the content from the show\_inventory.txt file

template = open(“/etc/ansible/parse-inventory-with-textfsm/show\_inventory\_multiple.textfsm”)  
re\_table = textfsm.TextFSM(template)  
fsm\_results = re\_table.ParseText( raw\_text\_data)

# the results are written to a CSV file

outfile\_name = open(“/etc/ansible/parse-inventory-with-textfsm//outfile.csv”, “w+”)  
outfile = outfile\_name

# Display result as CSV and write it to the output file

# First the column headers…

print(re\_table.header)  
for s in re\_table.header:  
outfile.write(“%s;” % s)  
outfile.write(“\n”)

# …now all row’s which were parsed by TextFSM

counter = 0  
for row in fsm\_results:  
print(row)  
for s in row:  
outfile.write(“%s;” % s)  
outfile.write(“\n”)  
counter += 1  
print(“Write %d records” % counter)

---

<div class="post-metadata">

**Author:** ![Claudia\_de\_Luna](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@Claudia\_de\_Luna](https://forum.ansible.com/u/Claudia_de_Luna)\
**Post date:** [January 28, 2018, 9:21pm UTC](https://forum.ansible.com/t/simple-cisco-ios-show-version/22264/19 "2018-01-28T21:21:03Z")

</div>

Hi Surjeet,

I have a script that is very similar to yours. I pass it either a file or a path to a directory full of individual files per device with all the show command output.

I take the output of each file and append it to a list, fsm\_all\_results. Once done processing either one file or a directory full of files, i save fsm\_all\_results to a CSV file.

Hope this helps!

Good luck.

```auto
# Make sure we have files to process (at least 1)
if len(file_list) > 0:

    # Open the CSV file to store results
    csv_results_fh = open_file(results_dir, 'wb')
    csv_writer = csv.writer(csv_results_fh, quoting=csv.QUOTE_MINIMAL)

    # Iterate through the valid file list. If the script was passed a filename it will be a file_list of 1
    # If the script was passed a directory it will be a list of files with a valid extension
    for fil in file_list:

        print("Processing device file: " + fil)

        # open_file function returns a file handle
        fh = open_file(fil, 'r')

        # Read the file contents into a variable for parsing
        file_contents = fh.read()
        # Close file
        fh.close()

        # Send TextFSM Template name and data to parse to text_fsm_parsing function
        # file_results returns the parsed results and table returns the header
        fil_results, table = text_fsm_parse(textfsm_template, file_contents)
        print("file results of lenght {} from text_fsm_parse:\n{}".format(str(len(fil_results)),fil_results[1]))

        # Append fil_results list of results to list of all results
        fsm_all_results.append(fil_results)

        # Keep track of files without parser output in the no_output list so it can be printed later
        if len(fil_results) == 0:
            no_output.append(fil)

    # Write the header row in the CSV file
    if table:
        csv_writer.writerow(table.header)
    else:
        sys.exit("Parsing Error. Execution aborted.")

    # Write each row in the fsm_all_results list to the CSV file
    for re_row in fsm_all_results:
        for single_row in re_row:
            csv_writer.writerow(single_row)
            # print(single_row)

```

---

<div class="post-metadata">

**Author:** ![Surjeet\_Singh](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@Surjeet\_Singh](https://forum.ansible.com/u/Surjeet_Singh)\
**Post date:** [January 29, 2018, 4:46am UTC](https://forum.ansible.com/t/simple-cisco-ios-show-version/22264/20 "2018-01-29T04:46:11Z")

</div>

Thank you for your response.

I also managed to fix this by modifying my code little bit  
.I will share my updated code with you…

Regards/Surjeet

[Next page](https://forum.ansible.com/t/simple-cisco-ios-show-version/22264.md?page=2)
