Simple cisco IOS show version

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"

start with

  • hosts: routers
    vars:

    tasks:

See example here: https://gist.github.com/privateip/11b042e569585ee9248a

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

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,

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

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

Regards,
John Barker

Thank you!

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

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

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

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]

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!

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.

`

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.

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

  1. Use the newish jinja2 filters regex_findall and regex_search

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

  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

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

Good Luck!

Claudia

(attachments)

get_ios_cmd_filter.yml (966 Bytes)

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:

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

Thank you …

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)

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)

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