- play_arrow Disclaimer
- play_arrow Junos PyEZ Overview
- play_arrow Install Junos PyEZ
- play_arrow Connect to and Retrieve Facts From a Device Using Junos PyEZ
- play_arrow Use Junos PyEZ to Manage Device Operations
- Use Junos PyEZ to Execute RPCs on Junos Devices
- Suppress RpcError Exceptions Raised for Warnings in Junos PyEZ Applications
- Use Junos PyEZ to Halt, Reboot, or Shut Down Junos Devices
- Use Junos PyEZ to Install Software on Junos Devices
- Use Junos PyEZ to Perform File System Operations
- Transfer Files Using Junos PyEZ
- Specify the XML Parser for a Junos PyEZ Session
- play_arrow Create and Use Junos PyEZ Tables and Views
- Understanding Junos PyEZ Tables and Views
- Predefined Junos PyEZ Operational Tables (Structured Output)
- Load Inline or External Tables and Views in Junos PyEZ Applications
- Define Junos PyEZ Operational Tables for Parsing Structured Output
- Define Views for Junos PyEZ Operational Tables that Parse Structured Output
- Use Junos PyEZ Operational Tables and Views that Parse Structured Output
- Define Junos PyEZ Operational Tables for Parsing Unstructured Output
- Define Views for Junos PyEZ Operational Tables that Parse Unstructured Output
- Use Junos PyEZ Tables with TextFSM Templates
- Use Junos PyEZ Operational Tables and Views that Parse Unstructured Output
- Define Junos PyEZ Configuration Tables
- Define Views for Junos PyEZ Configuration Tables
- Use Junos PyEZ Configuration Tables to Retrieve Configuration Data
- Overview of Using Junos PyEZ Configuration Tables to Define and Configure Structured Resources
- Use Junos PyEZ Configuration Tables to Configure Structured Resources on Junos Devices
- Save and Load Junos PyEZ Table XML to and from Files
- play_arrow Troubleshoot Junos PyEZ
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:
Import any required modules, classes, and objects.
content_copy zoom_out_mapfrom 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.
content_copy zoom_out_maphost = 'dc1a.example.com'
Create a
main()
function definition and function call, and place the remaining statements within the definition.content_copy zoom_out_mapdef main(): if __name__ == "__main__": main()
Create an instance of the
Device
class, and supply the hostname and any parameters required for that specific connection.content_copy zoom_out_mapdev = Device(host=host)
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
Create an instance of the
Config
utility.content_copy zoom_out_map# Set up config object cu = Config(dev)
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
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))
Unlock the configuration, and then end the NETCONF session and close the connection with the device.
content_copy zoom_out_mapfinally: 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.
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_mapuser@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:
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.