Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 

Using Ansible to Retrieve Facts from Devices Running Junos OS

 

Juniper Networks provides support for using Ansible to manage devices running Junos OS. Ansible for Junos OS requires all tasks to run locally on the Ansible control machine and uses NETCONF and the Junos XML API to interface with managed devices running Junos OS. As a result, playbooks default to gathering facts from the local Ansible control machine rather than the managed node. The juniper_junos_facts module in the Juniper.junos role and the junos_facts Ansible core module enable you to gather the device facts as well as the committed configuration from devices running Junos OS.

This topic discusses using the juniper_junos_facts module to retrieve information from devices running Junos OS. For information about the junos_facts Ansible core module, see https://docs.ansible.com/ansible/latest/junos_facts_module.html.

Note

Starting in Ansible for Junos OS Release 2.0.0, the juniper_junos_facts module replaces the functionality of the junos_get_facts module.

The juniper_junos_facts module uses the Junos PyEZ fact gathering system to retrieve the device facts from devices running Junos OS. For more information about the Junos PyEZ fact gathering system and the complete list of returned dictionary keys, see jnpr.junos.facts.

When you use the juniper_junos_facts module to retrieve the device facts, the module returns the facts in the ansible_facts.junos dictionary. The juniper_junos_facts module also enables you to save the returned data in a file on the local Ansible control machine. To specify the directory on the local Ansible control machine in which to save the retrieved information, include the savedir module argument, and define the path to the target directory. The JSON data for each device is stored in a separate file named hostname-facts.json, where hostname is the value of the hostname fact retrieved from the device, which might be different from the hostname passed to the module. In addition, hardware inventory information for each device is stored as XML in a separate file named hostname-inventory.xml.

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 machine. Because the playbook requires connection: local to run the Ansible for Junos OS modules locally, the playbook defaults to gathering facts from the Ansible control machine. The playbook uses the juniper_junos_facts module to retrieve the facts from the managed device and includes the gather_facts: no argument to prevent the playbook from gathering facts from the Ansible control machine.

By default, the juniper_junos_facts module does not return the device configuration. To return the committed 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. If the savedir option is included, the configuration data is not written to the file.

Tip

To use Ansible to retrieve configuration data from a device running Junos OS and save the data to a file, use the juniper_junos_config module instead of the juniper_junos_facts module. For more information, see Using Ansible to Retrieve or Compare Junos OS Configurations.

The following playbook retrieves the device facts and committed 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 machine, and prints the configuration for each device to standard output.

Release History Table
Release
Description
Starting in Ansible for Junos OS Release 2.0.0, the juniper_junos_facts module replaces the functionality of the junos_get_facts module.