ON THIS PAGE
Example: Retrieve the Pre-Inheritance Candidate Configuration in a Commit Script
This example shows how to construct a SLAX commit script to retrieve the pre-inheritance candidate configuration for either a normal or private configuration session.
Requirements
Device running Junos OS or device running Junos OS Evolved.
Overview
When you commit the candidate configuration, each active commit script inspects the configuration. The normal input to a commit script is the post-inheritance candidate configuration. In the post-inheritance candidate configuration, all configuration group values have been inherited by their targets and the inactive portions of the configuration have been removed.
A commit script might require access to the pre-inheritance candidate configuration rather than the post-inheritance configuration it receives by default. Commit scripts can reference the pre-inheritance candidate configuration. However, the candidate database location depends on the session type. Normal configuration sessions use the regular candidate database and private configuration sessions use a dedicated, private candidate database.
Commit scripts can use the <get-configuration> remote
procedure call (RPC) to retrieve the pre-inheritance candidate configuration. If the
commit script invokes the <get-configuration> RPC with the
database="candidate" attribute, the script retrieves the normal
pre-inheritance candidate configuration. The commit script can also invoke the
<get-configuration> RPC with the
database-path attribute and specify the path of the
pre-inheritance configuration database for either a normal or private configuration
session.
Junos OS and Junos OS Evolved provide the path of the pre-inheritance configuration database as
input to the commit script. The $junos-context global variable
contains the commit-context/database-path element, which stores the
location of that session’s pre-inheritance candidate configuration.
In a normal configuration session, the <database-path> element
contains the location of the normal candidate database.
<junos-context>
<commit-context>
<database-path>/var/run/db/juniper.db</database-path>
</commit-context>
</junos-context>In a private configuration session, the <database-path> element contains the location of
the session-specific, private candidate database. For example:
<junos-context>
<commit-context>
<commit-private/>
<database-path>/var/run/db/private/juniper-1396.db</database-path>
</commit-context>
</junos-context>To construct a commit script that retrieves the pre-inheritance candidate configuration specific
to that session, include the <get-configuration> RPC, and set
the <database-path> attribute to
$junos-context/commit-context/database-path. For normal
configuration sessions, the commit-script retrieves the normal pre-inheritance
candidate configuration. For private configuration sessions, the commit-script
retrieves the private, pre-inheritance candidate configuration.
If a commit script includes both the database and the database-path attributes in the <get-configuration> tag, the database attribute takes precedence.
Configuration
Configuring the Commit Script
Step-by-Step Procedure
To construct a SLAX commit script that retrieves the pre-inheritance candidate configuration specific to that session:
In a text editor, add the SLAX commit script boilerplate to a file.
version 1.0; ns junos = "http://xml.juniper.net/junos/*/junos"; ns xnm = "http://xml.juniper.net/xnm/1.1/xnm"; ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0"; import "../import/junos.xsl"; match configuration { }Create a variable that stores the
<get-configuration>RPC with thedatabase-pathattribute set to$junos-context/commit-context/database-path.var $rpc = <get-configuration database-path=$junos-context/commit-context/database-path>;
Add a statement that invokes the
<get-configuration>RPC and stores the resulting configuration in a variable.var $config = jcs:invoke( $rpc );
-
Refer to the required hierarchy levels and statements in the pre-inheritance candidate configuration using normal XPath constructs, for example:
var $hostname = $config/system/host-name;
Include any statements required to enforce your custom configuration rules during the commit process.
Copy the script to the /var/db/scripts/commit directory on the device.
In configuration mode, configure the
filestatement to enable the commit script.[edit system scripts commit] user@host# set file script-name.slax
Commit the configuration.
[edit] user@host# commit
The device executes the commit script during the commit operation.
Results
version 1.0;
ns junos = "http://xml.juniper.net/junos/*/junos";
ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
import "../import/junos.xsl";
match configuration {
var $rpc = <get-configuration database-path=$junos-context/commit-context/database-path>;
var $config = jcs:invoke( $rpc );
...
<!-- commit script rules -->
}