Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

Use Ansible to Retrieve Facts from Junos Devices

Juniper Networks supports using Ansible to manage Junos devices and provides Ansible modules that enable you to 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 modules that enable you to gather device facts, including the active configuration, from Junos devices. Table 1 outlines the available modules. The modules use 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.

Table 1: Modules to Gather Facts

Content Set

Module Name

juniper.device collection

facts

Juniper.junos role

juniper_junos_facts

The facts and juniper_junos_facts modules return the device facts in the ansible_facts.junos dictionary. The modules also enable 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 Juniper Networks 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 facts module in the juniper.device collection 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.

By default, the facts and juniper_junos_facts modules do 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. 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 Junos device and save the data to a file, use the config or juniper_junos_config module instead of the facts or juniper_junos_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