Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

Example: Use Junos PyEZ to Roll Back the Configuration

The Junos PyEZ library enables you to perform operational and configuration tasks on Junos devices. This example uses the Junos PyEZ jnpr.junos.utils.config.Config utility to roll back the configuration on a Junos device.

Requirements

This example uses the following hardware and software components:

  • Configuration management server running Python 3.5 or later and Junos PyEZ Release 2.0 or later

  • Junos device 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 Junos device

Overview

This example presents a Python application that uses the Junos PyEZ Config utility to roll back the configuration on the specified device. Junos devices 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 application imports the Device class, which handles the connection with the Junos device; 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 Junos devices.

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 application calls the close() method to terminate the NETCONF session and connection. The application 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 application also includes code to handle any additional exceptions that might occur.

Configuration

Create the Junos PyEZ Application

Step-by-Step Procedure

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

  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 application. If the application does not display the intended code, repeat the instructions in this example to correct the application.

Execute the Junos PyEZ Code

Execute the Application

To execute the Junos PyEZ code:

  • On the configuration management server, execute the application.

Verification

Verify the Configuration

Purpose

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

Action

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

Meaning

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