Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

Use the juniper.device.junos_package Ansible Module to Install Software on Junos Devices

You can use the juniper.device.junos_package Ansible module to install software on devices running Junos OS or devices running Junos OS Evolved.

Use Ansible to Install Software

Juniper Networks provides Ansible modules that enable you to install software images on Junos devices. Table 1 outlines the modules. If you are already using a given set of modules from the juniper.device collection, use the module for that set.

Table 1: Software Modules

Collection

Module Set

Module Name

juniper.device collection

juniper.device

juniper.device.software

junipernetworks.junos

juniper.device.junos_package

The following sections discuss how to use the juniper.device.junos_package module to install software on Junos devices. The sections outline how to specify the software image location, the general software installation process and module options, and how to perform more specialized upgrade scenarios on devices that support these features.

How to Specify the Software Image Location

When you use the juniper.device.junos_package module to install software on Junos devices, you can download the software package to the Ansible control node. The module, by default, copies the package to the target device before performing the installation. You can also instruct the module to install a software image that already resides on the target device.

Table 2 outlines the module arguments that you must set depending on the software package location. The module must include the src argument with the path to the software image. The no_copy argument defaults to false, which instructs the module to copy the software package from the Ansible control node to the target device's /var/tmp directory. If the software package already resides on the target device, the module's arguments must include no_copy: true.

Table 2: Module Arguments for Software Package Location

Software Package Location

no_copy Parameter

src Parameter

Ansible control node

Omit or set to false

File path, including the filename, of the software package on the local control node. File paths are relative to the playbook directory.

Target device

Set to true

File path on the target device where the software package must already reside. The default directory is /var/tmp.

Providing Connection and Authentication Parameters for the Module

The juniper.device.junos_package module uses the Junos PyEZ library to perform software installations. Because of this dependency, the module does not use the default connection and authentication parameters to connect to Junos devices.

Unlike other juniper.device.junos_* modules, the juniper.device.junos_package module requires that you pass in certain connection and authentication parameters as module arguments. At a minimum, you must include a provider dictionary as a top-level module argument and define the host variable. The dictionary can also specify the username, password, and port variables.

Note: We recommend storing and referencing credentials in an Ansible vault or using SSH keys to securely connect to devices.

For example, the provider dictionary can include the following keys.

If you do not define a provider dictionary with the host variable, your playbook might generate the following error:

For the juniper.device.junos_package module, you can also specify some connection parameters, such as the user, in your SSH config file. By default, the module queries the default SSH configuration file at ~/.ssh/config and uses SSH keys loaded in the SSH key agent or in the default location. To specify the path to a non-default SSH configuration file, include the module's ssh_config argument . To use a non-default identify file, set the module's ssh_private_key_file argument to the path of the SSH private key file.

Installation Process Overview

You can use the juniper.device.junos_package module to install a software package on Junos devices. By default, the junos_package module validates the new package against the current configuration, installs the package on both Routing Engines, if applicable, and reboots both Routing Engines. To use the module to install a software package, provide the source package location, provider dictionary, and any required arguments. For the complete list of available options, see the module documentation.

When you execute the juniper.device.junos_package module, it performs the following operations:

  1. Compares the Junos OS version specified in the version argument, if supplied, to the installed version on the managed device. If the installed and desired versions are different, the module performs the installation.
  2. If the software package is located on the Ansible control node, and the no_copy parameter is omitted or set to false, the module performs the following operations:
    • Computes the checksum of the local software package.

    • Performs a storage cleanup on the target device to create space for the software package.

    • Copies the package to the /var/tmp directory on the target device.

      Note:

      The module copies the software package even if it initially existed in the target location because the storage cleanup operation removes any existing file.

    • Computes the checksum of the remote file and compares it to the checksum of the local file.

Once the software package is on the target device, whether downloaded there initially or copied over by the module, the module then performs the following operations:

  1. Validates the new package against the current configuration unless you set validate to false.

  2. Installs the package on each individual Routing Engine.

  3. Reboots each upgraded Routing Engine, unless the reboot argument is set to false.

For example, the following playbook installs a software package that resides on the local Ansible control node:

Similarly, the following playbook installs a software package that resides in the /var/tmp directory on the target device.

How to Perform a Unified ISSU

The unified ISSU feature enables you to upgrade between two different Junos OS releases with no disruption on the control plane and with minimal disruption of traffic. For more information about the unified ISSU feature, see the software documentation for your product.

The juniper.device.junos_package module supports performing a unified ISSU on devices that support the feature and meet the necessary requirements. To perform a unified ISSU, the juniper.device.junos_package module must include the issu: true argument. For example:

Example: Use Ansible to Install Software

This example uses the juniper.device.junos_package module to install a software image on a Junos device.

Requirements

This example uses the following hardware and software components:

  • Configuration management server running Ansible 2.17 or later with the juniper.device collection installed

  • Junos device with NETCONF enabled and a user account configured with appropriate permissions

  • SSH public/private keypair configured for the appropriate user on the Ansible control node and Junos device

  • Existing Ansible inventory file that defines the required hosts and their network connection and network OS variables

Overview

This example presents an Ansible playbook that uses the juniper.device.junos_package module to upgrade Junos OS on the hosts in the specified inventory group. In this example, the software image resides on the Ansible control node, and the module copies the image to the target device before installing it. The juniper.device.junos_package module requires that you explicitly define the host variable. The module's provider dictionary defines host and username.

The playbook first defines two tasks to verify if the software image exists and to stop execution if the file doesn't exist. The Check NETCONF connectivity task utilizes the ansible.builtin.wait_for module to try to establish a NETCONF session with the Junos device using the default NETCONF port 830. If the control node fails to establish a NETCONF session with a device during playbook execution, then it skips the remaining tasks in the play for that device.

The Install Junos OS package task executes the juniper.device.junos_package module provided that the NETCONF check was successful. The version argument defines the desired Junos OS version as it would be reported by the show version command on the Junos device. During playbook execution, the module first checks that the requested version is not already installed on the device. If the requested version is different from the installed version, the module installs the requested version.

The src argument defines the path of the software package on the Ansible control node. During the installation, the module:

  • Performs a storage cleanup operation on the target device

  • Copies the software image to the /var/tmp directory on the device

  • Verifies the file’s checksum

  • Validates the new software against the active configuration

  • Installs the software on each Routing Engine on the target host

  • Reboots each Routing Engine on the target host

By default, the juniper.device.junos_package module reboots each Routing Engine after the installation completes; however, this task explicitly sets reboot: true for clarity. The task stores the module result in the response variable. The example executes the playbook with the -v option to log messages of severity level INFO or higher in the Ansible log to monitor the installation.

Create the Playbook

Create the Ansible Playbook

To create a playbook that uses the juniper.device.junos_package module to install a software image on a Junos device:

  1. Include the boilerplate for the playbook and this play.

  2. Define or import any necessary variables, which for this example, includes the desired Junos OS version, the path to the new image, and the NETCONF port.

  3. Create tasks to verify if the software package exists and stop execution if the package does not exist.

  4. (Optional) Create a task to verify NETCONF connectivity.

  5. Create the task to install the Junos OS package on the device.

  6. (Optional) Create a task to print the module response.

Results

On the Ansible control node, review the completed playbook. If the playbook does not display the intended code, repeat the instructions in this example to correct the playbook.

Execute the Playbook

The playbook in this example uses the following Ansible inventory file. To simplify the execution, the Ansible inventory group includes only one host.

To execute the playbook:

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

Verification

Purpose

Verify that the software installation was successful.

Action

The playbook output should indicate any failed tasks. However, you can also review the contents of the Ansible log file for additional details. Sample log file output is shown here. Some output has been omitted for brevity.

Meaning

The log file contents indicate that the playbook successfully copied and installed the image on both Routing Engines on the target device.

Troubleshoot Connection Errors

Problem

The playbook generates an error message for a missing host value.

Solution

The juniper.device.junos_package module uses the Junos PyEZ library to handle the software installation operations. You must define the host variable to connect to the managed device.

To specify the host variable, define a provider dictionary as a module argument. Specify host and any other required variables that you have not supplied in an SSH configuration file. For example: