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.
|
Collection |
Module Set |
Module Name |
|---|---|---|
|
|
||
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
- Example: Retrieve Device Facts and Save to a File
- Example: Retrieve Device Facts and Configuration
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:
- name: Retrieve device facts and configuration
juniper.device.facts:
config_format: jsonWhen 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.
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.
---
- 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 }}"
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
---
- 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
When you execute the playbook, the output displays the configuration.
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 juniper.device.junos_facts Module
- Module Overview
- Example: Retrieve Device Facts and Available Network Resources
- Example: Retrieve Device Facts and the Active Configuration
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:
- name: Get Hardware Facts and JSON Configuration
juniper.device.junos_facts:
gather_subset:
- config
- hardware
config_format: json
register: responseWhen 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.
- name: Get Facts and Available Network Resources
juniper.device.junos_facts:
available_network_resources: trueExample: 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.
---
- name: Get facts with juniper.device.junos_facts
hosts: junos
gather_facts: false
tasks:
- name: Get Facts and Available Network Resources
juniper.device.junos_facts:
available_network_resources: true
register: response
- name: Print model
ansible.builtin.debug:
var: response.ansible_facts.ansible_net_model
- name: Print available network resources
ansible.builtin.debug:
var: ansible_facts.available_network_resources
run_once: trueThe output displays the model and available network resources.
user@ansible-cn:~$ ansible-playbook junos_facts.yaml
PLAY [Get facts with juniper.device.junos_facts] ***********************************
TASK [Get Facts and Available Resources] *******************************************
ok: [router1.example.com]
ok: [router2.example.com]
TASK [Print model] *****************************************************************
ok: [router1.example.com] => {
"response.ansible_facts.ansible_net_model": "mx960"
}
ok: [router2.example.com] => {
"response.ansible_facts.ansible_net_model": "mx240"
}
TASK [Print available network resources] *******************************************
ok: [router1.example.com] => {
"ansible_facts.available_network_resources": [
"acl_interfaces",
"acls",
"bgp_address_family",
"bgp_global",
"hostname",
"interfaces",
"l2_interfaces",
"l3_interfaces",
"lacp",
"lacp_interfaces",
"lag_interfaces",
"lldp_global",
"lldp_interfaces",
"logging_global",
"ntp_global",
"ospf_interfaces",
"ospfv2",
"ospfv3",
"prefix_lists",
"routing_instances",
"routing_options",
"security_policies",
"security_policies_global",
"security_zones",
"snmp_server",
"static_routes",
"vlans"
]
}
PLAY RECAP *************************************************************************
router1.example.com : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
router2.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 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.
---
- name: Get facts with juniper.device.junos_facts
hosts: dc1
gather_facts: false
tasks:
- name: Get Facts and JSON Configuration
juniper.device.junos_facts:
gather_subset: config
config_format: json
register: response
- name: Print model
ansible.builtin.debug:
var: response.ansible_facts.ansible_net_config
When you execute the playbook, it prints the JSON configuration data.
user@ansible-cn:~$ ansible-playbook junos_facts_with_config.yaml
PLAY [Get facts with juniper.device.junos_facts] ************************************
TASK [Get Facts and JSON Configuration] *********************************************
ok: [dc1a.example.com]
TASK [Print model] ******************************************************************
ok: [dc1a.example.com] => {
"response.ansible_facts.ansible_net_config": {
"configuration": {
"@": {
"junos:changed-localtime": "2026-02-04 16:53:02 PST",
"junos:changed-seconds": "1770252782",
"xmlns": "http://xml.juniper.net/xnm/1.1/xnm"
},
"apply-groups": [
"global",
"re0",
"re1"
],
[...output truncated...]
}
PLAY RECAP **************************************************************************
dc1a.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0