Use Junos PyEZ to Compare the Candidate Configuration and a Previously Committed Configuration
SUMMARY Use the Junos PyEZ diff()
and pdiff()
methods to compare the candidate
configuration to a previously committed configuration.
Junos devices store a copy of the most recently committed configuration and up to 49 previous
configurations. The Junos PyEZ
jnpr.junos.utils.config.Config
utility enables you to compare the candidate configuration to a previously
committed configuration and print or return the difference. Table 1 outlines the methods, which are equivalent to issuing the show | compare
rollback n
configuration mode command in the
Junos OS CLI.
Method |
Description |
---|---|
Compare the candidate configuration to the specified rollback configuration and return the difference as an object. |
|
Compare the candidate configuration to the specified rollback configuration and print the difference directly to standard output. |
The ephemeral configuration database stores only the current version of the committed ephemeral configuration data, and as a result, it does not support comparing the modified ephemeral configuration to previously committed configurations.
The diff()
and pdiff()
methods retrieve the difference between
the candidate configuration and a previously committed configuration, which is
referenced by the rollback ID parameter, rb_id
, in the method call.
If the parameter is omitted, the rollback ID defaults to 0, which corresponds to the
active configuration.
The difference is returned in patch format, where:
Statements that exist only in the candidate configuration are prefixed with a plus sign (+)
Statements that exist only in the comparison configuration and not in the candidate configuration are prefixed with a minus sign (-)
The methods return or print
None
if there is no difference between the configurations.
In a Junos PyEZ application, after establishing a connection
with the device, you can call the diff()
or pdiff()
method for a Config
or Table object to compare the candidate and
rollback configurations. The following example uses the Config
class to load configuration changes into the
candidate configuration, and then calls the pdiff()
method to print the differences between the modified candidate configuration
and the active configuration before committing the changes.
from jnpr.junos import Device from jnpr.junos.utils.config import Config with Device(host='router1.example.com') as dev: with Config(dev, mode='exclusive') as cu: cu.load(path='configs/junos-config-mx.conf', merge=True) cu.pdiff() cu.commit()
When you execute the code, it prints the differences to standard output. For example:
[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; - } - } - }
To retrieve the difference between the configurations
as an object for further manipulation, call the diff()
method instead of the pdiff()
method,
and store the output in a variable. For example:
cdiff = cu.diff(rb_id=2) print (cdiff)
When you use Junos PyEZ configuration Tables and Views
to make structured configuration changes on a device, you can load
and commit the configuration data either by calling the lock()
, load()
, commit()
and unlock()
methods
individually, or by calling the set()
method,
which calls all of these methods automatically. If you use configuration
Tables to configure a device, and you want to compare the updated
candidate configuration to a previously committed configuration using
the diff()
or pdiff()
methods in your application, you must use the load()
and commit()
methods instead of the set()
method. Doing this enables you to retrieve the
differences after the configuration data is loaded into the candidate
configuration but before it is committed. For example:
from jnpr.junos import Device from myTables.ConfigTables import UserConfigTable with Device(host='router1.example.com') as dev: with UserConfigTable(dev, mode='exclusive') as userconf: userconf.user = 'user1' userconf.class_name = 'read-only' userconf.append() userconf.load(merge=True) userconf.pdiff() userconf.commit()
The following example compares the candidate configuration to the configuration with rollback ID 5 but does not make any changes to the configuration:
from jnpr.junos import Device from jnpr.junos.utils.config import Config with Device(host='router1.example.com') as dev: cu = Config(dev) cu.pdiff(rb_id=5)