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

Use the juniper.device.facts or juniper.device.junos_facts Ansible module to retrieve device facts from Junos devices.

Juniper Networks provides Ansible modules that you can use to manage devices running Junos OS and devices running Junos OS Evolved. The modules do not require Python on the managed device because they use the Junos XML API and 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 where Python is installed. As a result, Ansible defaults to gathering facts from the Ansible control node instead of the managed node.

Juniper Networks provides modules that you can use to gather device facts, including the active configuration, from Junos devices. Table 1 outlines the available modules. If you are already using a given set of modules from the juniper.device collection, use the module for that set.

Table 1: Modules to Gather Facts

Collection

Module Set

Module Name

juniper.device

juniper.device

juniper.device.facts

junipernetworks.junos juniper.device.junos_facts

Both the juniper.device.facts module and the juniper.device.junos_facts module enable you to gather device facts and to retrieve the active configuration in different formats. The juniper.device.facts module also enables you to save the device facts to a file. The juniper.device.junos_facts module enables you to retrieve different subsets of device facts. The following sections provide an overview of each module.

juniper.device.facts Module

Module Overview

The juniper.device.facts 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. The module returns the device facts in the ansible_facts.junos dictionary.

The juniper.device.facts module enables you to save the returned data in a file on the local Ansible control node. To save the data, include the savedir module argument and define the path to the target directory. The playbook generates the following files for each device, where hostname is the value of the hostname fact retrieved from the device. The hostname value might differ 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

By default, the juniper.device.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. Acceptable format values are json, set, text, and xml. For example:

When you request the configuration, the module response includes the ansible_facts.junos.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.

Tip:

To use Ansible to retrieve configuration data from a Junos device and save the data to a file, use the juniper.device.config module instead of the juniper.device.facts module. For more information, see Use the juniper.device.config Ansible Module to Retrieve or Compare Junos OS Configurations.

Example: Retrieve Device Facts and Save to a File

The following playbook uses the juniper.device.facts module to retrieve the device facts for each device in the inventory group. The module saves the facts for each device in separate files in the playbook directory.

In this case, Ansible runs the network module locally. As a result, 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. The juniper.device.facts module retrieves the facts from the managed devices.

Example: Retrieve Device Facts and Configuration

The playbook in this 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

When you execute the playbook, the output displays the configuration.

juniper.device.junos_facts Module

Module Overview

By default, the juniper.device.junos_facts module returns a subset of facts that corresponds to setting gather_subset: default. You can set the gather_subset and gather_network_resources arguments to various values to return different subsets of the device facts. See the module documentation for accepted values. The module returns device facts in the ansible_facts dictionary.

The juniper.device.junos_facts module does not return the device configuration by default. To return the active configuration for a device, in addition to the device facts, include the config value for gather_subset. You can optionally include the config_format argument to specify the format. Acceptable format values are json, set, text, and xml. The default is text. For example:

When you request the configuration, the module response includes the ansible_facts.ansible_net_config key with the configuration in the specified format.

The juniper.device.junos_facts module also enables you to return a list of available network resources. Ansible network resource modules are standardized templates that enable you to manage the configuration of network devices in a consistent, repeatable way, even in a multi-vendor environment. The resources use simple key-value pairs and declare the required state of the resource.

To return available network resources, set available_network_resources to true. The list of returned resources corresponds to all of the available configuration template modules that you can use to configure Junos devices. You can use the configuration templates to configure common statements and options for a resource on devices that support configuring that resource.

Example: Retrieve Device Facts and Available Network Resources

The following playbook uses the juniper.device.junos_facts module to retrieve the default device facts from the hosts in the inventory group. The playbook also returns the available network resources. The playbook prints the model for each host and the available network resources. Since all Junos devices use the same set of network resource templates, the task includes run_once: true to only print the network resources once.

The output displays the model and available network resources.

Example: Retrieve Device Facts and the Active Configuration

The following playbook uses the juniper.device.junos_facts module to retrieve the default device facts and the active configuration in JSON format.

When you execute the playbook, it prints the JSON configuration data.