Ansible network facts

Prior to 2.5, Ansible networking shipped with a number of network-specific fact modules. The network fact modules exist, but the naming and usage was different between vendors. Starting with version 2.5, Ansible started to standardize its network fact module usage. The Ansible network fact modules gather information from the system and store the results in facts prefixed with ansible_net_. The data collected by these modules is documented in the return values in the module documentation. This is a pretty big milestone for Ansible networking modules, as it does a lot of the heavy lifting for you to abstract the fact-gathering process by default.

Let's use the same structure we saw in Chapter 7, The Python Automation Framework – Ansible Basics, Ansible 2.5 best practices, but expand upon it to see how the ios_facts module was used to gather facts. As a review, our inventory file contains two iOS hosts with the host variables residing in the host_vars directory: 

$ cat hosts
[ios-devices]
iosv-1
iosv-2

$ cat host_vars/iosv-1
---
ansible_host: 172.16.1.20
ansible_user: cisco
ansible_ssh_pass: cisco
ansible_connection: network_cli
ansible_network_os: ios
ansbile_become: yes
ansible_become_method: enable
ansible_become_pass: cisco

Our playbook will have three tasks. The first task will use the ios_facts module to gather facts for both of our network devices. The second task will display certain facts gathered and stored for each of the two devices. You will see that the facts we displayed were the default ansible_net facts, as opposed to a registered variable from the first task. The third task will display all the facts we collected for the iosv-1 host:

$ cat my_playbook.yml
---
- name: Chapter 5 Ansible 2.5 network facts
connection: network_cli
gather_facts: false
hosts: all
tasks:
- name: Gathering facts via ios_facts module
ios_facts:
when: ansible_network_os == 'ios'

- name: Display certain facts
debug:
msg: "The hostname is {{ ansible_net_hostname }} running {{ ansible_net_version }}"

- name: Display all facts for a host
debug:
var: hostvars['iosv-1']

When we run the playbook, you can see that the result for the first two tasks were what we would have expected: 

$ ansible-playbook -i hosts my_playbook.yml

PLAY [Chapter 5 Ansible 2.5 network facts] *************************************

TASK [Gathering facts via ios_facts module] ************************************
ok: [iosv-2]
ok: [iosv-1]

TASK [Display certain facts] ***************************************************
ok: [iosv-2] => {
"msg": "The hostname is iosv-2 running 15.6(3)M2"
}
ok: [iosv-1] => {
"msg": "The hostname is iosv-1 running 15.6(3)M2"
}

The third task will display all the network device facts gathered for iOS devices. There is a ton of information that has been gathered for iOS devices that can help with your networking automation needs:

TASK [Display all facts for a host] ********************************************
ok: [iosv-1] => {
"hostvars['iosv-1']": {
"ansbile_become": true,
"ansible_become_method": "enable",
"ansible_become_pass": "cisco",
"ansible_check_mode": false,
"ansible_connection": "network_cli",
"ansible_diff_mode": false,
"ansible_facts": {
"net_all_ipv4_addresses": [
"10.0.0.5",
"172.16.1.20",
"192.168.0.1"
],
"net_all_ipv6_addresses": [],
"net_filesystems": [
"flash0:"
],
"net_gather_subset": [
"hardware",
"default",
"interfaces"
],
"net_hostname": "iosv-1",
"net_image": "flash0:/vios-adventerprisek9-m",
"net_interfaces": {
"GigabitEthernet0/0": {
"bandwidth": 1000000,
"description": "OOB Management",
"duplex": "Full",
"ipv4": [
{
"address": "172.16.1.20",
"subnet": "24"
}
[skip]

The network facts module in Ansible 2.5 was a big step forward in streamlining your workflow and brought it on par with other server modules.  

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset