Gathering Network Information with Ansible
After completing this section, you should be able to:
Gather data about network devices and networks using facts, ad hoc commands, and playbooks.
Selectively target and extract information.
Naming of Common Network Device Modules
Ansible provides many platform-specific modules engineered to make it easier to automate the management of networking devices.
The names of these modules generally follow a common pattern.
These are four common families of modules:
Gather facts from networking devices using the *os_facts family of Ansible modules.
Issue commands to networking devices using the *os_command family of Ansible modules.
Configure networking devices using the *os_config family of Ansible modules.
Configure layer 3 interfaces using the *os_l3_interface family of Ansible modules.
Naming of Other Network Device Modules
The *os_facts, *os_commands, *os_config, and *os_l3_interface families of modules represent a small part of the very large collection of *os_* modules that make it easier to manage networking devices.
Use the ansible-doc -l command to explore what is available.
Modules that Gather Facts on Network Devices
The modules in the *os_facts family are used to gather facts from networking devices:
cnos_facts
edgeos_facts
enos_facts
eos_facts
ios_facts
junos_facts
nxos_facts
vyos_facts
Modules that Run Commands on Network Devices
The modules in the *os_command family are used to issue commands to networking devices:
aireos_command
cnos_command
edgeos_command
enos_command
eos_command
ios_command
junos_command
nxos_command
sros_command
vyos_command
Modules that Configure Network Devices
The modules in the *os_config family are used to configure networking devices:
aireos_config
edgeos_config
enos_config
eos_config
fortios_config
ios_config
junos_config
nxos_config
sros_config
vyos_config
Modules that Configure Layer 3 Interfaces
The modules in the *os_l3_interface family are used to configure layer 3 interfaces on networking devices:
eos_l3_interface
ios_l3_interface
junos_l3_interface
nxos_l3_interface
vyos_l3_interface
Each networking platform has an *os_facts module.
The Lab Network has VyOS and IOS devices, so you will use the vyos_facts and ios_facts modules.
Exploring vyos_facts and ios_facts
You can use an ad hoc command to show which facts are returned by default.
Now that you are somewhat familiar with Ansible Vault, you can use encrypted files to store all sensitive data, including passwords.
The following command displays the default set of facts returned by the vyos_facts module:
[user@host ~]$ ansible -i inventory --ask-vault-pass -m vyos_facts vyos
The following command displays the facts provided by default by the ios_facts module:
[user@host ~]$ ansible -i inventory --ask-vault-pass -m ios_facts ios
How many facts can you gather using vyos_facts and ios_facts?
The following command displays all facts supported by the vyos_facts module:
$ ansible -i inventory --ask-vault-pass -m vyos_facts spine01 \
> -a 'gather_subset=all'
The following command displays all facts supported by the ios_facts module:
$ ansible -i inventory --ask-vault-pass -m ios_facts cs01 -a 'gather_subset=all'
The gather_subset=all option returns a lot of information.
The following playbook displays only specific pieces of information when run.
The vyos_facts module was used.
Note that variables are automatically registered by the facts module.
[user@host ~]$ cat vyos-some-facts1.yml
---
- name: a play that gathers some facts and displays them
hosts: vyos
tasks:
- name: invoke vyos_facts
vyos_facts:
gather_subset: default
- debug:
msg:
- "Host name: {{ ansible_net_hostname }}"
- " Model: {{ ansible_net_model }}"
- " Version: {{ ansible_net_version }}"
- "SerialNum: {{ ansible_net_serialnum }}"
Investigating Fact Selection
The following example runs the playbook to gather some facts.
[user@host proj]$ ansible-playbook -l leaf01 vyos-some-facts1.yml
PLAY [a play that gathers some facts and displays them] **********************
TASK [invoke vyos_facts] *****************************************************
ok: [leaf01]
TASK [debug] *****************************************************************
ok: [leaf01] => {
"msg": [
"Host name: vyos"
" Model: VMware"
" Version: VyOS"
"SerialNum: VMware-56"
]
}
PLAY RECAP *******************************************************************
leaf01 : ok=2 changed=0 unreachable=0 failed=0