Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

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.device collection includes the juniper.device original modules and the junipernetworks.junos modules.

  • 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:

  1. In your favorite editor, create a new file with a descriptive playbook name that uses the .yaml file extension.
  2. Include three dashes to indicate the start of the YAML document.
  3. Provide a descriptive name for the play.
  4. Define a comma-delimited (or colon-delimited) list of the hosts or groups of hosts on which the modules will operate; specify all to indicate all hosts in the inventory file.

    The Ansible inventory file must define any hosts or groups of hosts that you reference in the playbook.

  5. (Optional) Include gather_facts: no to prevent fact gathering for the target host, which for local connections is the Ansible control node.
  6. (Optional) Reference the collection.

    You can define the collections key in the playbook and then reference just the module name in the task. However, the recommended method is to omit the collections key and instead reference collection content by its fully qualified collection name (FQCN).

    This example omits the collections key and uses the fully qualified collection name.

  7. Define a tasks section, and include one or more tasks as list items.
  8. (Optional) As an additional check for NETCONF sessions over SSH, create a task to verify NETCONF connectivity for each Junos device.
  9. Create tasks that use the Juniper Networks modules, and provide any necessary connection and authentication parameters.

    This example uses SSH keys loaded in an SSH key agent and does not explicitly provide credentials for the facts module in the playbook.

  10. (Optional) Define additional tasks or additional plays as needed.

Execute the Playbook

The playbook in this example uses the following Ansible inventory file, which defines the connection as local.

To execute the playbook:

  • Issue the ansible-playbook command on the control node, and provide the playbook path and any required options.