In this recipe, we will outline how to collect device facts from Cisco devices with Ansible. This information includes the serial number, IOS version, and all the interfaces on the devices. Ansible executes several commands on managed IOS devices in order to collect this information.
Collecting IOS device facts
Getting ready
The Ansible controller must have IP connectivity with the managed network devices, and SSH must be enabled on the IOS devices.
How to do it...
- Create a new playbook called pb_collect_facts.yml in the same ch2_ios folder with the following information:
---
- name: "PLAY 1: Collect Device Facts"
hosts: core,wan
tasks:
- name: "P1T1: Gather Device Facts"
ios_facts:
register: device_facts
- debug: var=device_facts
How it works...
We run this new playbook against all nodes within the core and wan group, and we use the ios_facts module to collect the information from the managed IOS devices. In this recipe, we use the debug module to print out the information that was collected from the ios_facts module. The following is a subset of the information that was discovered:
ok: [core01 -> localhost] => {
"Ansible_facts": {
"net_all_ipv4_addresses": [
"172.20.1.20",
< ---------- Snippet ------------ >
"10.1.100.1"
],
"net_hostname": "core01",
"net_interfaces": {
< ---------- Snippet ------------ >
"Vlan10": {
"bandwidth": 1000000,
"description": null,
"duplex": null,
"ipv4": [
{
"address": "10.1.10.1",
"subnet": "24"
}
],
"lineprotocol": "up",
"macaddress": "aabb.cc80.e000",
"mediatype": null,
"mtu": 1500,
"operstatus": "up",
"type": "Ethernet SVI"
},
},
"net_iostype": "IOS",
"net_serialnum": "67109088",
"net_system": "ios",
"net_version": "15.1",
}
< ------------ Snippet ------------ >
}
From the preceding output, we can see some of the main facts that the ios_facts module has captured from the devices, including the following:
- net_all_ipv4_addresses: This list data structure contains all the IPv4 addresses that are configured on all the interfaces on the IOS device.
- net_interfaces: This dictionary data structure captures the status of all of the interfaces on this device and their operational state, as well as other important information, such as a description and their operational state.
- net_serialnum: This captures the serial number of the device.
- net_version: This captures the IOS version running on this device.
There's more...
Using the information that is collected from the ios_facts module, we can generate structured reports for the current state of the network and use these reports in further tasks. In this section, we will outline how to modify our playbook to build this report.
Add a new task to the pb_collect_facts.yml playbook, as shown in the following code:
- name: "P1T2: Write Device Facts"
blockinfile:
path: ./facts.yml
create: yes
block: |
device_facts:
{% for host in play_hosts %}
{% set node = hostvars[host] %}
{{ node.Ansible_net_hostname }}:
serial_number: {{ node.Ansible_net_serialnum }}
ios_version: {{ node.Ansible_net_version }}
{% endfor %}
all_loopbacks:
{% for host in play_hosts %}
{% set node = hostvars[host] %}
{% if node.Ansible_net_interfaces is defined %}
{% if node.Ansible_net_interfaces.Loopback0 is defined %}
- {{ node.Ansible_net_interfaces.Loopback0.ipv4[0].address }}
{% endif %}
{% endif %}
{% endfor %}
run_once: yes
delegate_to: localhost
We use the blockinfile module to build a YAML file called facts.yml. We use Jinja2 expressions within the blockinfile module to customize and select the information we want to capture from the Ansible facts that were captured from the ios_facts task. When we run the pb_collect_facts.yml playbook, we generate the facts.yml file, which has the following data:
device_facts:
wan01:
serial_number: 90L4XVVPL7V
ios_version: 16.06.01
wan02:
serial_number: 9UOFOO7FH19
ios_version: 16.06.01
core01:
serial_number: 67109088
ios_version: 15.1
core02:
serial_number: 67109104
ios_version: 15.1
all_loopbacks:
- 10.100.1.3
- 10.100.1.4
- 10.100.1.1
- 10.100.1.2
See also...
For more information regarding ios_facts and the different parameters supported by these modules, please consult the following URL:
https://docs.Ansible.com/Ansible/latest/modules/ios_facts_module.html