Understanding the Ansible Inventory File When Managing Junos Devices
Inventory File Overview
The Ansible inventory file defines the hosts and groups of hosts upon which commands, modules, and tasks in a playbook operate. The file can be in one of many formats depending on your Ansible environment and plugins. Common formats include INI and YAML.
The default location for the inventory file is /etc/ansible/hosts. You can also create project-specific inventory files in alternate locations, for example, in your project's playbook directory. Your Ansible configuration file can specify the path to a non-default inventory file location. You can also specify the location of the inventory file when you execute a playbook.
The inventory file can list individual hosts or user-defined groups of hosts. Thus, you can organize Junos devices with similar roles into groups. Then you can perform the same operational and configuration tasks on the devices in that group. For example, if you are managing one or more data centers, you can create groups for those switches that require the same set of operations, such as applying similar configurations and upgrading Junos OS.
In order to manage Junos devices using Ansible, you must have a Junos OS login account with appropriate access privileges on each managed device. You must ensure that usernames and passwords or access keys exist for each host in the inventory file.
The following INI-formatted sample inventory file defines an individual host,
host1, and two groups of hosts, routers and
switches. The inventory also defines a parent group
network that includes the routers and
switches groups.
host1.example.com [routers] router1.example.com router2.example.com [switches] switch1.example.com switch2.example.com [network:children] routers switches
For more information about the Ansible inventory file, see the official Ansible documentation at https://docs.ansible.com/projects/ansible/latest/inventory_guide/intro_inventory.html.
juniper.device Collection: Connection and Network OS
Variables
Starting in juniper.device Release 2.0.0, we consolidated the
Juniper Networks juniper.device collection
and the Red Hat Ansible junipernetworks.junos
collection into a single juniper.device collection. Although you
execute the modules under the same namespace, each module set uses different Ansible
connection and network OS values, so you cannot intermix the modules in the same
playbook.
You can define the Ansible connection in your playbook, your inventory file, in host
or group variable files, or in command-line arguments. If you define the connection
in the inventory file, set ansible_connection to the appropriate
value for your modules. Table 1 outlines the connection values for each module set. For the
junipernetworks.junos modules, you must also define
ansible_network_os.
| Module Set | ansible_connection Value |
ansible_network_os Value |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
Instead of defining ansible_connection in the inventory file,
you can also define connection in your playbook's play. Both
parameters take the same values.
In addition to the required connection and network OS variables, your inventory file can optionally define the username that Ansible uses to connect to the devices. It can also specify a port, if it is different from the default port. We recommend that you also define the Python interpreter path in either the Ansible configuration file or the Ansible inventory file. Specifying the Python interpreter path prevents unnecessary errors if Ansible cannot locate the Python installation that your environment uses.
The following sections provide some simplified example inventory files for each module set.
- Sample Inventory File for the
juniper.deviceModules - Sample Inventory File for the
junipernetworks.junosModules
Sample Inventory File for the juniper.device Modules
If you use the original juniper.device modules to manage Junos
devices, the inventory file should define ansible_connection as
local or juniper.device.pyez.
The following inventory file defines a group named junos that
has three hosts. The hosts use the local connection to connect
to the managed devices.
[junos] router1.example.com router2.example.com router3.example.com [junos:vars] ansible_connection=local ansible_user=username ansible_port=port [all:vars] ansible_python_interpreter=python-interpreter-path
Sample Inventory File for the junipernetworks.junos Modules
If you use the junipernetworks.junos modules
(juniper.device.junos_* modules) to manage Junos devices,
your inventory file must define the appropriate variables for that module set.
You can specify the ansible_connection value using the fully
qualified name ansible.netcommon.netconf or
ansible.netcommon.network_cli. Alternatively, you can just
specify netconf or network_cli. Check the
module documentation to verify the connection values supported for each module.
The following inventory file defines a group named junos that
has three hosts. Ansible uses the ansible.netcommon.netconf
connection to connect to the managed devices. The inventory file also includes
the required ansible_network_os value for these modules.
[junos] router1.example.com router2.example.com router3.example.com [junos:vars] ansible_connection=ansible.netcommon.netconf ansible_network_os=juniper.device.junos ansible_user=username ansible_port=port [all:vars] ansible_python_interpreter=python-interpreter-path