Example: Using Junos PyEZ to Roll Back the Configuration

 

Juniper Networks provides support for using Python to manage devices running Junos OS. The Junos PyEZ library provides simple yet powerful methods to perform operational and configuration tasks on devices running Junos OS. This example outlines how to use the Junos PyEZ jnpr.junos.utils.config.Config utility to roll back the configuration on a device running Junos OS.

Requirements

This example uses the following hardware and software components:

  • Configuration management server running Python 2.7 or 3.4 and Junos PyEZ Release 2.0 or a later release

  • Device running Junos OS with NETCONF enabled and a user account configured with appropriate permissions

  • SSH public/private key pair configured for the appropriate user on the server and device running Junos OS

Overview

This example presents a Python program that uses the Junos PyEZ Config utility to roll back the configuration on the specified device. Devices running Junos OS store a copy of the most recently committed configuration and up to 49 previous configurations. You can roll back to any of the stored configurations. This is useful when configuration changes cause undesirable results, and you want to revert back to a known working configuration. Rolling back the configuration is similar to the process for making configuration changes on the device, but instead of loading configuration data, you perform a rollback, which replaces the entire candidate configuration with a previously committed configuration.

The Python program imports the Device class, which handles the connection with the device running Junos OS; the Config class, which is used to perform configuration mode commands on the target device; and required exceptions from the jnpr.junos.exception module, which contains exceptions encountered when managing devices running Junos OS.

After creating the Device instance for the target device, the open() method establishes a connection and NETCONF session with the device. The Config utility methods then lock, roll back, commit, and unlock the candidate configuration.

The rollback() method has a single parameter, rb_id, which is the rollback ID specifying the stored configuration to load. Valid values are 0 (zero, for the most recently committed configuration) through one less than the number of stored previous configurations (maximum is 49). If you omit this parameter in the method call, it defaults to 0. This example loads the configuration with rollback ID 1, which is the configuration committed just prior to the active configuration. The rollback() method loads the configuration into the candidate configuration, which is then committed to make it active by calling the commit() method.

After rolling back and committing the configuration, the NETCONF session and connection are terminated using the close() method. The Python program includes code for handling exceptions such as LockError for errors that occur when locking the configuration and CommitError for errors that occur during the commit operation. The program also includes code to handle any additional exceptions that might occur.

Configuration

Creating the Junos PyEZ Program

Step-by-Step Procedure

To create a Python program that uses Junos PyEZ to roll back the configuration on a device running Junos OS:

  1. Import any required modules, classes, and objects.

  2. Include any required variables, which for this example includes the hostname of the managed device.

  3. Create a main() function definition and function call, and place the remaining statements within the definition.

  4. Create an instance of the Device class, and supply the hostname and any parameters required for that specific connection.

  5. Open a connection and establish a NETCONF session with the device.

  6. Create an instance of the Config utility.

  7. Lock the configuration.

  8. Roll back and commit the configuration, and handle any errors.

  9. Unlock the configuration, and then end the NETCONF session and close the connection with the device.

Results

On the configuration management server, review the completed program. If the program does not display the intended code, repeat the instructions in this example to correct the program.

Executing the Junos PyEZ Code

Step-by-Step Procedure

To execute the Junos PyEZ code:

  • On the configuration management server, execute the program.

    user@server:~$ python junos-pyez-config-rollback.py

Verification

Verifying the Configuration

Purpose

Verify that the configuration was correctly rolled back on the device running Junos OS.

Action

Log in to the device running Junos OS and view the configuration or configuration differences and the log file. For example:

user@dc1a> show configuration | compare rollback 1
user@dc1a> show log messages

Meaning

The configuration differences and the log file contents indicate that the configuration was successfully rolled back and committed on the device.