Using Junos PyEZ to Compare the Candidate Configuration and a Previously Committed Configuration

 

Devices running Junos OS 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. The pdiff() method prints the difference directly to standard output, whereas the diff() method returns the difference as an object. The methods are equivalent to issuing the show | compare rollback n configuration mode command in the Junos OS command-line interface (CLI).

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 current 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. Statements that exist only in the candidate configuration are prefixed with a plus sign (+), and statements that exist only in the comparison configuration and not in the candidate configuration are prefixed with a minus sign (-). If there is no difference between the configurations, the methods return or print None.

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: