Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

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.

Table 1: Junos PyEZ Methods to Compare Configurations

Method   

Description

diff()

Compare the candidate configuration to the specified rollback configuration and return the difference as an object.

pdiff()

Compare the candidate configuration to the specified rollback configuration and print the difference directly to standard output.

Note:

The ephemeral configuration database only stores 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 or most recently committed 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.

When you execute the code, it prints the differences to standard output. For example:

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:

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:

The following example compares the candidate configuration to the configuration with rollback ID 5 but does not make any changes to the configuration: