Simple cisco IOS show version

5,117 views
Skip to first unread message

Bharath Bharadwaj

unread,
Aug 10, 2016, 10:32:23 AM8/10/16
to Ansible Project
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"

pixel fairy

unread,
Aug 10, 2016, 11:12:25 AM8/10/16
to Ansible Project
start with

- hosts: routers
  vars:
    ...
  tasks:
    ...

Peter Sprygada

unread,
Aug 10, 2016, 11:36:41 AM8/10/16
to ansible...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/0e2c7491-cd53-4d07-8b20-c6870c600fc4%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Bharath Bharadwaj

unread,
Aug 11, 2016, 11:11:53 AM8/11/16
to Ansible Project
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,

Bharath Bharadwaj

unread,
Aug 11, 2016, 11:11:53 AM8/11/16
to Ansible Project
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

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

John Barker

unread,
Aug 24, 2016, 3:20:15 AM8/24/16
to Ansible Project
I've added a comment with the the corrected playbook sample


Regards,
John Barker

Bharath Bharadwaj

unread,
Aug 24, 2016, 3:44:15 AM8/24/16
to Ansible Project
Thank you!

Valérie P

unread,
Sep 2, 2016, 1:38:16 PM9/2/16
to Ansible Project
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:

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:

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

Thanks in advance!
Valerie

Peter Sprygada

unread,
Sep 2, 2016, 11:28:39 PM9/2/16
to ansible...@googlegroups.com
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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/6383da04-b68b-4a33-85e9-87318eb0a5b8%40googlegroups.com.

Surjeet Singh

unread,
Jan 20, 2018, 11:57:38 AM1/20/18
to Ansible Project
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
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

Claudia de Luna

unread,
Jan 22, 2018, 7:09:13 AM1/22/18
to Ansible Project
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.


---
- hosts: cisco
  connection
: local
  gather_facts
: False
  ignore_errors
: yes


  vars
:
    cli
:
        host
: "{{ host }}"
        username
: "{{ username }}"
        password
: "{{ password }}"


  tasks
:
   
- name: Gather NX-OS Facts
      ios_facts
:
        provider
: "{{ cli }}"


     
register: facts_output


   
- debug: var=facts_output
   
- debug: var=facts_output.ansible_facts.ansible_net_version


   
- local_action: copy content="{{ facts_output }}" dest="./{{ inventory_hostname }}.txt"







TASK
[debug] ********************************************************************************************************************************************************
ok
: [arctic-3650] => {
   
"facts_output.ansible_facts.ansible_net_version": "03.06.06E"
}

Claudia de Luna

unread,
Jan 22, 2018, 7:09:13 AM1/22/18
to Ansible Project
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/ 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]

<snip>

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!




 

Surjeet Singh

unread,
Jan 22, 2018, 11:52:54 PM1/22/18
to ansible...@googlegroups.com
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.  

Regards,
Surjeet Singh
Technical Specialist – Networks DATA
CCNA, CCNP(R&S)
Cell : +917838707047

To become bigger person,need to walk with bigger Vision !!!!


--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/Ul5D-gAzRrg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/826a69bb-d730-48f6-a7ce-103808c1f0f0%40googlegroups.com.

Claudia de Luna

unread,
Jan 23, 2018, 10:50:15 AM1/23/18
to ansible...@googlegroups.com
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/ 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 nice little summary 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

2.  Use the newish jinja2 filters regex_findall and regex_search

Ivan Pepelnjak has a nice summary of 1 and 2 here.

3.  Look at the textfsm modules and filters 

4.  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).


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) :D

Good Luck!

Claudia



get_ios_cmd_filter.yml

Surjeet Singh

unread,
Jan 24, 2018, 1:51:47 AM1/24/18
to ansible...@googlegroups.com
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:

---
- name: collect device facts and display OS version
  hosts: "{{ inventory | default('all') }}"
  gather_facts: false
  connection: local
  ignore_errors: yes
  
  vars:
    cli:
      host: "{{ inventory_hostname }}"
      username: cisco
      password: cisco
    
  tasks:
    - ios_facts:
      gather_subset: all
      provider: "{{ cli }}" 
      register: facts_output      
    
    - debug: var=facts_output.ansible_facts.ansible_net_hostname
    - debug: var=facts_output.ansible_facts.ansible_net_version
    - debug: var=facts_output.ansible_facts.ansible_net_model   

    - name: write the inventory in into file      
      copy: content="{{ facts_output.ansible_facts.ansible_net_hostname, facts_output.ansible_facts.ansible_net_version , facts_output.ansible_facts.ansible_net_model }}" dest="facts/iosfacts.txt"

now my question is there any way to copy this information excel for all devices because currently i am getting information for only one device.since information is getting overwrite everytime instead of append.

can we use loop to achieve above task ?



Regards,
Surjeet Singh
Technical Specialist – Networks DATA
CCNA, CCNP(R&S)
Cell : +917838707047

To become bigger person,need to walk with bigger Vision !!!!


Claudia de Luna

unread,
Jan 24, 2018, 1:58:23 PM1/24/18
to ansible...@googlegroups.com
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



Surjeet Singh

unread,
Jan 24, 2018, 2:02:47 PM1/24/18
to ansible...@googlegroups.com

Surjeet Singh

unread,
Jan 27, 2018, 9:29:25 AM1/27/18
to ansible...@googlegroups.com
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)

Regards,
Surjeet Singh
Technical Specialist – Networks DATA
CCNA, CCNP(R&S)
Cell : +917838707047

To become bigger person,need to walk with bigger Vision !!!!


Claudia de Luna

unread,
Jan 28, 2018, 4:21:03 PM1/28/18
to Ansible Project
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.

# 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)


To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/Ul5D-gAzRrg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/Ul5D-gAzRrg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/Ul5D-gAzRrg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/Ul5D-gAzRrg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.

Surjeet Singh

unread,
Jan 28, 2018, 11:46:27 PM1/28/18
to ansible...@googlegroups.com
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

To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/77eca948-46c4-4acf-ba10-d46420a39317%40googlegroups.com.

Surjeet Singh

unread,
Jan 31, 2018, 1:52:32 AM1/31/18
to ansible...@googlegroups.com
Hi,

I would like to share that i managed to collect inventory information with only minor changes in ansible playbook. 

sharing just to help other people who new learner like me.

    # write facts in inventory
    - name: write the inventory in into file
      copy:
        content: |
          MGMT_IP;HOSTNAME;MODEL;VERSION;IMAGE;
          {% for host in ansible_play_hosts %}
          {{ hostvars[host].inventory_hostname}};{{ hostvars[host].facts_output.ansible_facts.ansible_net_hostname }};{{ hostvars[host].facts_output.ansible_facts.ansible_net_model }};{{ hostvars[host].facts_output.ansible_facts.ansible_net_version }};{{ hostvars[host].facts_output.ansible_facts.ansible_net_image }}
          {% endfor %}

        dest: /etc/ansible/facts/systems.csv  
        backup: yes    

output will look like below after minor changes in excel.

MGMT_IP HOSTNAME MODEL VERSION IMAGE
1.1.1.1 R1 3725 (R7000) processor 12.4(15)T14 tftp://255.255.255.255/unknown
2.2.2.2 R2 3725 (R7000) processor 12.4(15)T14 tftp://255.255.255.255/unknown
3.3.3.3 R3 3725 (R7000) processor 12.4(15)T14 tftp://255.255.255.255/unknown
4.4.4.4 R4 3725 (R7000) processor 12.4(15)T14 tftp://255.255.255.255/unknown
5.5.5.5 router_r5 3725 (R7000) processor 12.4(15)T14 tftp://255.255.255.255/unknown
6.6.6.6 R6 3725 (R7000) processor 12.4(15)T14 tftp://255.255.255.255/unknown
7.7.7.7 R7 3725 (R7000) processor 12.4(15)T14 tftp://255.255.255.255/unknown
8.8.8.8 router_r8 3725 (R7000) processor 12.4(15)T14 tftp://255.255.255.255/unknown


Regards,
Surjeet Singh
Technical Specialist – Networks DATA
CCNA, CCNP(R&S)
Cell : +917838707047

To become bigger person,need to walk with bigger Vision !!!!


Reply all
Reply to author
Forward
0 new messages