Example: Use 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 3.5 or later and Junos PyEZ Release 2.0 or later
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
Create 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:
Import any required modules, classes, and objects.
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
Include any required variables, which for this example includes the hostname of the managed device.
host = 'dc1a.example.com'
Create a
main()
function definition and function call, and place the remaining statements within the definition.def main(): if __name__ == "__main__": main()
Create an instance of the
Device
class, and supply the hostname and any parameters required for that specific connection.dev = Device(host=host)
Open a connection and establish a NETCONF session with the device.
# 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
Create an instance of the
Config
utility.# Set up config object cu = Config(dev)
Lock the configuration.
# 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 the configuration, and handle any errors.
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))
Unlock the configuration, and then end the NETCONF session and close the connection with the device.
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 program. If the program does not display the intended code, repeat the instructions in this example to correct the program.
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 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.
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 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 [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; + } + } + }
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.