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
- Use the newish jinja2 filters regex_findall and regex_search
Ivan Pepelnjak has a nice summary of 1 and 2 here.
-
Look at the textfsm modules and filters
-
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)
Good Luck!
Claudia
(attachments)
get_ios_cmd_filter.yml (966 Bytes)