ON THIS PAGE
Example: Retrieve the Pre-Inheritance Candidate Configuration in a Commit Script
This example shows how to construct a commit script to retrieve the pre-inheritance candidate configuration for either a normal or private configuration session.
Requirements
Routing, switching, or security device running Junos OS Release 12.2 or later.
Overview
In Junos OS, when a candidate configuration is committed, it is inspected by each active commit script. The normal input for a commit script is the post-inheritance candidate configuration, in which all configuration group values have been inherited by their targets and the inactive portions of the configuration have been removed.
At times, a commit script requires access to the pre-inheritance candidate configuration rather than the post-inheritance configuration it receives by default. Since normal configuration sessions use the regular candidate database, and private configuration sessions use a dedicated, private candidate database, the candidate database location depends on the session type.
Within a commit script, invoking the <get-configuration> remote procedure call (RPC) with the database="candidate" attribute retrieves the normal pre-inheritance candidate configuration.
The <get-configuration> RPC also has
a database-path attribute, which is used
to specify the location of the pre-inheritance configuration database
for either a normal or private configuration session. This attribute
is an alternative to the database attribute
and indicates which database file to load. Commit scripts can invoke
the <get-configuration> RPC with the database-path attribute to retrieve the pre-inheritance
candidate configuration specific to that session.
The global variable, $junos-context contains the commit-context/database-path element, which stores the location of the 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 in the commit script, 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, and 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 commit script that retrieves the pre-inheritance candidate configuration specific to that session:
In a text editor, add the 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 desired 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/run/scripts/commit directory on the device.
In configuration mode, configure the
filestatement to enable the commit script.[edit system scripts commit] user@R1# set file script-name.slax
Issue the
commitcommand to commit the configuration.[edit] user@R1# commit
The commit script is executed 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 -->
}