Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

Announcement: Try the Ask AI chatbot for answers to your technical questions about Juniper products and solutions.

close
header-navigation
keyboard_arrow_up
close
keyboard_arrow_left
Junos PyEZ Developer Guide
Table of Contents Expand all
list Table of Contents
file_download PDF
{ "lLangCode": "en", "lName": "English", "lCountryCode": "us", "transcode": "en_US" }
English
keyboard_arrow_right

Example: Use Junos PyEZ to Roll Back the Configuration

date_range 09-May-24

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.

    content_copy zoom_out_map
    from jnpr.junos import Device
    from jnpr.junos.utils.config import Config
    from jnpr.junos.exception import ConnectError
    from jnpr.junos.exception import LockError
    from jnpr.junos.exception import RpcError
    from jnpr.junos.exception import CommitError
    from jnpr.junos.exception import UnlockError
    
  2. Include any required variables, which for this example includes the hostname of the managed device.

    content_copy zoom_out_map
    host = 'dc1a.example.com'
    
  3. Create a main() function definition and function call, and place the remaining statements within the definition.

    content_copy zoom_out_map
    def main():
    
    if __name__ == "__main__":
        main()
    
  4. Create an instance of the Device class, and supply the hostname and any parameters required for that specific connection.

    content_copy zoom_out_map
        dev = Device(host=host)
    
  5. Open a connection and establish a NETCONF session with the device.

    content_copy zoom_out_map
        # open a connection with the device and start a NETCONF session
        try:
            dev.open()
        except ConnectError as err:
            print ("Cannot connect to device: {0}".format(err))
            return
    
  6. Create an instance of the Config utility.

    content_copy zoom_out_map
        # Set up config object
        cu = Config(dev)
    
  7. Lock the configuration.

    content_copy zoom_out_map
        # Lock the configuration
        print ("Locking the configuration")
        try:
            cu.lock()
        except LockError as err:
            print ("Unable to lock configuration: {0}".format(err))
            dev.close()
            return
    
  8. Roll back and commit the configuration, and handle any errors.

    content_copy zoom_out_map
        # Roll back and commit configuration
        try:
            print ("Rolling back the configuration")
            cu.rollback(rb_id=1)
            print ("Committing the configuration")
            cu.commit()
        except CommitError as err:
            print ("Error: Unable to commit configuration: {0}".format(err))
        except RpcError as err:
            print ("Unable to roll back configuration changes: {0}".format(err))
    
  9. Unlock the configuration, and then end the NETCONF session and close the connection with the device.

    content_copy zoom_out_map
        finally:
            print ("Unlocking the configuration")
            try:
                cu.unlock()
            except UnlockError as err:
                print ("Unable to unlock configuration: {0}".format(err))
            dev.close()
            return
    

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.

content_copy zoom_out_map
from jnpr.junos import Device
from jnpr.junos.utils.config import Config
from jnpr.junos.exception import ConnectError
from jnpr.junos.exception import LockError
from jnpr.junos.exception import RpcError
from jnpr.junos.exception import CommitError
from jnpr.junos.exception import UnlockError

host = 'dc1a.example.com'

def main():
    dev = Device(host=host)
    # open a connection with the device and start a NETCONF session
    try:
        dev.open()
    except ConnectError as err:
        print ("Cannot connect to device: {0}".format(err))
        return

    # Set up config object
    cu = Config(dev)

    # Lock the configuration
    print ("Locking the configuration")
    try:
        cu.lock()
    except LockError as err:
        print ("Unable to lock configuration: {0}".format(err))
        dev.close()
        return

    # Roll back and commit configuration
    try:
        print ("Rolling back the configuration")
        cu.rollback(rb_id=1)
        print ("Committing the configuration")
        cu.commit()
    except CommitError as err:
        print ("Error: Unable to commit configuration: {0}".format(err))
    except RpcError as err:
        print ("Unable to roll back configuration changes: {0}".format(err))

    finally:
        print ("Unlocking the configuration")
        try:
            cu.unlock()
        except UnlockError as err:
            print ("Unable to unlock configuration: {0}".format(err))
        dev.close()
        return

if __name__ == "__main__":
    main()

Execute the Junos PyEZ Code

Execute the Application

To execute the Junos PyEZ code:

  • On the configuration management server, execute the application.

    content_copy zoom_out_map
    user@server:~$ python3 junos-pyez-config-rollback.py
    Locking the configuration
    Rolling back the configuration
    Committing the configuration
    Unlocking the configuration

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:

content_copy zoom_out_map
user@dc1a> show configuration | compare rollback 1
[edit system scripts op]
-     file bgp-neighbors.slax;
[edit interfaces]
-  ge-1/0/0 {
-       unit 0 {
-           family inet {
-               address 198.51.100.1/26;
-           }
-       }
-   }
+   ge-1/1/0 {
+       unit 0 {
+           family inet {
+               address 198.51.100.65/26;
+           }
+       }
+   }
content_copy zoom_out_map
user@dc1a> show log messages
Sep 19 12:42:06  dc1a sshd[5838]: Accepted publickey for user from 198.51.100.1 port 58663 ssh2: RSA 02:dd:53:3e:f9:97:dd:1f:d9:31:e9:7f:82:06:aa:67
Sep 19 12:42:10  dc1a file[5841]: UI_LOAD_EVENT: User 'user' is performing a 'rollback 1'
Sep 19 12:42:11  dc1a file[5841]: UI_COMMIT: User 'user' requested 'commit' operation (comment: none)
Sep 19 12:42:26  dc1a file[5841]: UI_COMMIT_COMPLETED: commit complete

Meaning

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

footer-navigation