Create and Execute Ansible Playbooks to Manage Junos Devices
You can create Ansible playbooks that execute Juniper Networks modules to perform operational and configuration tasks on devices running Junos OS and devices running Junos OS Evolved.
Juniper Networks provides Ansible modules that enable you to perform operational and configuration tasks on Junos devices. This topic outlines how to create a simple Ansible playbook to execute Juniper Networks modules on Junos devices.
Playbook Overview
You create Ansible playbooks to handle more complex management tasks. Playbooks consist of one or more plays, or groups of tasks, that operate on a set of defined hosts. Each playbook play must specify:
-
The hosts on which the tasks operate
-
The list of tasks to execute on each host
-
Any required variables or module parameters, including authentication parameters, if you have not defined these elsewhere
Any Ansible hosts that a playbook references must be defined in the Ansible inventory file. The default location for the Ansible inventory file is /etc/ansible/hosts.
A playbook executes tasks on a host by calling modules. The Juniper Networks Ansible
modules are distributed through the juniper.device collection,
which is hosted on Ansible Galaxy. To use the Juniper
Networks modules in your playbook, you must install the collection on the Ansible
control node. For more information about the collection and modules, see Understanding the Ansible for Junos OS Collections and Modules.
The Juniper Networks modules do not require Python on Junos devices because they use
the Junos XML API and NETCONF to interface with the device. Thus, to perform
operations on Junos devices, you must run modules locally on the Ansible control
node, where Python is installed. You can run the modules locally by defining the
appropriate Ansible connection and network OS parameters. For information about the
connection parameters and values for modules in the juniper.device
collection, see How to Execute Modules on Junos Devices.
By default, Ansible plays automatically gather system facts from the remote host.
However, when you execute the plays locally, Ansible gathers the facts from the
Ansible control node instead of the remote host. To avoid gathering facts for the
control node, include gather_facts: no in the playbook.
When you execute the Juniper Networks modules using a NETCONF session over SSH, the Junos device must have NETCONF enabled. We recommend that you create a simple task in the playbook that explicitly tests whether NETCONF is enabled on each device before executing other tasks. If this task fails for any host, by default, Ansible does not execute the remaining tasks for this host. Without this test, you might get a generic connection error during playbook execution that does not indicate whether this issue or another issue is the cause of any failures.
You write playbooks in YAML format. YAML is white-space sensitive and indentation is
significant. Therefore, your playbooks should always use spaces rather than tabs. In
YAML, items preceded by a hyphen (-) are list items, and the
key: value notation represents a hash. For detailed information
about creating Ansible playbooks, refer to the official Ansible documentation at
https://docs.ansible.com/projects/ansible/latest/playbook_guide/playbooks.html.
Create an Ansible Playbook
Before you begin:
-
Choose the module set that you will use to manage Junos devices. The
juniper.devicecollection includes thejuniper.deviceoriginal modules and thejunipernetworks.junosmodules. -
Update your inventory file with the appropriate Ansible connection and network OS variables for your module set as described in Understanding the Ansible Inventory File When Managing Junos Devices.
To create a simple playbook to perform tasks on Junos devices:
Execute the Playbook
The playbook in this example uses the following Ansible inventory file, which
defines the connection as local.
[dc1] dc1a.example.com ansible_host=198.51.100.1 [dc1:vars] ansible_connection=local
To execute the playbook:
-
Issue the
ansible-playbookcommand on the control node, and provide the playbook path and any required options.user@ansible-cn:~$ ansible-playbook junos-get-facts.yaml PLAY [Get Device Facts] *************************************************** TASK [Check NETCONF connectivity] ***************************************** ok: [dc1a.example.net] TASK [Retrieve information from Junos devices] **************************** ok: [dc1a.example.net] TASK [Print version] ****************************************************** ok: [dc1a.example.net] => { "junos.version": "19.4R1.10" } PLAY RECAP **************************************************************** dc1a.example.net : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0