Using Salt to Compare the Junos OS Candidate Configuration with a Previously Committed Configuration
Juniper Networks provides support for using Salt to manage devices
running Junos OS, and the Junos execution and state modules (for Salt)
define functions that enable you to perform operational and configuration
tasks on the managed devices. The salt.modules.junos.diff
execution function and
the salt.states.junos.diff
state function enable
you to compare the candidate configuration to a previously committed
configuration and return the difference.
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.
Devices running Junos OS store a copy of the most recently committed
configuration and up to 49 previous configurations. The junos.diff
function returns the difference between
the candidate configuration and a previously committed configuration,
which is referenced by the rollback ID parameter. Table 1 outlines the rollback
ID parameter to use for the junos.diff
execution
and state functions. If the rollback ID parameter is omitted, the
rollback ID defaults to 0, which corresponds to the active or most
recently committed configuration.
Function |
Rollback ID Parameter |
---|---|
|
|
|
|
The configuration differences are 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 junos.diff
function prints None
.
To compare the candidate configuration to the active configuration,
execute the junos.diff
function, and either
omit the rollback ID parameter or set it equal to 0. For example:
saltuser@salt-master:~$ sudo salt 'router1' junos.diff router1: ---------- message: [edit system scripts op] + file bgp-neighbors.slax; out: True
The previous command is equivalent to issuing the show
| compare
or show | compare rollback 0
configuration
mode command in the Junos OS command-line interface (CLI).
saltuser@router1# show | compare [edit system scripts op] + file bgp-neighbors.slax;
If you compare the candidate configuration to a previous configuration
and there are no differences, the junos.diff
function returns None
. In the following
example, the junos.diff
function compares
the candidate configuration to the active configuration and returns None
, which indicates that the there are no uncommitted
changes in the candidate configuration:
saltuser@salt-master:~$ sudo salt 'router2' junos.diff router2: ---------- message: None out: True
To compare the candidate configuration to a specific rollback
configuration, set the id
parameter to
the ID of the rollback configuration. This is equivalent to issuing
the show | compare rollback n
configuration mode command in the Junos OS CLI.
saltuser@salt-master:~$ sudo salt 'router1' junos.diff id=3 router1: ---------- message: [edit system scripts op] + file bgp-neighbors.slax; + file bgp-summary.slax; out: True
You can also define a state that returns the same information.
saltuser@salt-master:~$ cat /srv/salt/junos_diff.sls Retrieve configuration differences: junos.diff: - d_id: 0
When you apply the state to one or more targets, it returns the differences for each target.
saltuser@salt-master:~$ sudo salt 'router1' state.apply junos_diff router1: ---------- ID: Retrieve configuration differences Function: junos.diff Result: True Comment: Started: 00:09:55.359546 Duration: 3110.709 ms Changes: ---------- message: [edit system scripts op] + file bgp-neighbors.slax; out: True Summary for router1 ------------ Succeeded: 1 (changed=1) Failed: 0 ------------ Total states run: 1 Total run time: 3.111 s