Use Ansible to Retrieve Facts from Junos Devices
Juniper Networks provides Ansible modules that you can use to manage Junos devices and perform operational and configuration tasks on the devices. The modules do not require Python on the managed device because they use Junos PyEZ and the Junos XML API over NETCONF to interface with the device. Therefore, when you use Ansible to perform operations on Junos devices, you must execute the Ansible modules locally on the control node. As a result, Ansible defaults to gathering facts from the Ansible control node instead of the managed node.
Juniper Networks provides a module that you can use to gather device facts, including the active configuration, from Junos devices. Table 1 outlines the available module. The module uses the Junos PyEZ fact gathering system to retrieve the device facts. For more information about the Junos PyEZ fact gathering system and the complete list of returned dictionary keys, see jnpr.junos.facts.
Content Set |
Module Name |
---|---|
|
The facts
module returns the device facts in the
ansible_facts.junos
dictionary. The module also enables you to
save the returned data in a file on the local Ansible control node. To specify the
directory in which to save the retrieved information, include the
savedir
module argument, and define the path to the target
directory. When you include the savedir
argument, the playbook
generates the following files for each device, where hostname is
the value of the hostname
fact retrieved from the device, which
might be different from the hostname passed to the module:
-
hostname-facts.json—Device facts in JSON format
-
hostname-inventory.xml—Device’s hardware inventory in XML format
For example, the following playbook retrieves the device facts for each device in the
inventory group and saves the data for each device in separate files in the playbook
directory on the Ansible control node. Because the playbook runs the
facts
module locally, Ansible defaults to gathering facts from
the control node. The playbook includes the gather_facts: no
argument to prevent Ansible from gathering facts from the control node and instead
uses the juniper.device.facts
module to retrieve the facts from the
managed device. To authenticate with the device, the example uses existing SSH keys
in the default location and thus does not explicitly provide credentials for the
facts
module in the playbook.
--- - name: Get device facts hosts: dc1 connection: local gather_facts: no tasks: - name: Retrieve device facts and save to file juniper.device.facts: savedir: "{{ playbook_dir }}"
By default, the facts
module does not return the device
configuration. To return the active configuration for a device, in addition to the
device facts, include the config_format
option, and specify the
format in which to return the configuration. Acceptable format values are
'json'
, 'set'
, 'text'
and
'xml'
. The requested format must be supported by the Junos OS
release running on the device.
When you include the config_format
option, the
ansible_facts.junos
dictionary in the module response includes
the config
key with the configuration in the specified format in a
single multi-line string. However, even if the playbook includes the
savedir
option, the configuration data is not written to
a file.
To use Ansible to retrieve configuration data from a Junos device and save the
data to a file, use the config
module instead of the
facts
module. For more information, see Use Ansible to Retrieve or Compare Junos OS Configurations.
The playbook in the next example performs the following operations:
-
Retrieves the device facts and active configuration for each device in the inventory group
-
Saves the facts and hardware inventory for each device in separate files in the playbook directory on the Ansible control node
-
Prints the configuration for each device to standard output
--- - name: Get device facts and configuration hosts: dc1 connection: local gather_facts: no tasks: - name: Retrieve device facts and configuration and save facts to file juniper.device.facts: savedir: "{{ playbook_dir }}" config_format: "xml" register: result - name: Print configuration ansible.builtin.debug: var: result.ansible_facts.junos.config
user@ansible-cn:~$ ansible-playbook facts.yaml PLAY [Get device facts and configurations] ********************************** TASK [Retrieve device facts and configuration and save facts to file] ******* ok: [dc1a.example.net] TASK [Print configuration] ************************************************** ok: [dc1a.example.net] => { "result.ansible_facts.junos.config": "<configuration commit-seconds=\"1605564153\" commit-localtime=\"2020-11-16 14:02:33 PST\" commit-user=\"admin\">\n <version>20191212.201431_builder.r1074901</version>\n [...output truncated...] </configuration>\n" } PLAY RECAP ****************************************************************** dc1a.example.net : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0