Global Parameters and Variables in Junos OS Automation Scripts
Junos OS automatically provides input to automation scripts when they are executed. The script can reference this input, which includes device-specific information about the script execution environment such as the device hostname, the script type, and the user executing the script. This information is useful for creating scripts that respond to a variety of complex scenarios.
SLAX and XSLT scripts that import the junos.xsl file can reference
this information using the $junos-context
global variable, which is a
node-set. The junos.xsl import file also declares several
predefined global parameters that enable the scripts to more easily reference a subset
of this information. Python scripts can reference this information through the
junos.Junos_Context
dictionary, which must be imported into the
script.
To use the pre-defined parameters or global variable in SLAX and XSLT scripts, you must
import the junos.xsl file by including the
<xsl:import>
tag in the style sheet declaration of an
XSLT script or by including the import
statement in a
SLAX script and specifying the junos.xsl file location as shown in
the following sample code:
XSLT Syntax
<?xml version="1.0"?> <xsl:stylesheet version="1.0"> <xsl:import href="../import/junos.xsl"/> ... </xsl: stylesheet>
SLAX Syntax
version 1.2; import "../import/junos.xsl";
Python
To reference the information in Python scripts, import the Junos_Context
dictionary.
from junos import Junos_Context
The script input is described in detail in the following sections:
Global Parameters Available in SLAX and XSLT Scripts
Several predefined global parameters are available for use in SLAX and XSLT automation scripts that import the junos.xsl file. The parameters provide information about the Junos OS environment. Table 1 describes the built-in arguments.
Name |
Description |
Example |
---|---|---|
|
Hostname of the local device |
Tokyo |
|
Local time when the script is executed |
Fri Dec 10 11:42:21 2010 |
|
Local time, in ISO format, when the script is executed |
2010-12-10 11:42:21 PST |
|
Model of the local device |
m10i |
|
Filename of the executing script |
test.slax |
|
Local name of the user executing the script |
root |
The predefined global parameters are declared in the junos.xsl file. You do not need to declare these parameters in a script in order to use them. Access the value of the global parameters in a script by prefixing the parameter name with the dollar sign ($), as shown in the following example:
SLAX syntax:
if ($user != "root") { var $script-message = $user _ " does not have permission to execute " _ $script; expr jcs:output($script-message); }
XSLT syntax:
<xsl:if test="$user != 'root'"> <xsl:variable name="script-message" select="concat($user, ' does not have permission to execute ', $script)"/> <xsl:value-of select="jcs:output($script-message)"/> </xsl:if>
Global Variable Available in Automation Scripts
Commit, event, and op scripts can access specific environment information that is
provided to the script upon execution. To access this information, Python scripts
must import and reference the junos.Junos_Context
dictionary, and
SLAX and XSLT scripts that import the junos.xsl file can
reference the $junos-context
global variable.
$junos-context
and Junos_Context
contain
identical information but in a format that is suitable for the respective script
language.
The $junos-context
variable is a node-set that contains the
<junos-context>
node and the following hierarchy, which
is common to and embedded in the source tree of all scripts:
<junos-context> <chassis></chassis> <hostname></hostname> <localtime></localtime> <localtime-iso></localtime-iso> <pid></pid> <product></product> <re-master/> <routing-engine-name></routing-engine-name> <sw-upgrade-in-progress></sw-upgrade-in-progress> <script-type></script-type> <tty></tty> <user-context> <class-name></class-name> <login-name></login-name> <uid></uid> <user></user> </user-context> </junos-context>
Additionally, script-specific information is available depending on the type of
script executed. For op scripts, the <op-context>
element is
also included in the source tree provided to an op script.
<junos-context> <op-context> <via-url/> </op-context> </junos-context>
For commit scripts, the <commit-context>
element is also
included in the source tree provided to a commit script.
<junos-context> <commit-context> <commit-comment>"This is a test commit"</commit-comment> <commit-boot/> <commit-check/> <commit-sync/> <commit-confirm/> <database-path/> </commit-context> </junos-context>
Table 2 identifies each node of the $junos-context
variable node-set,
provides a brief description of the node, and gives examples of values for any
elements that are not input to a script as an empty tag.
Parent Node |
Node |
Description |
Example Content |
---|---|---|---|
|
|
Specifies whether the script is executed on a component of a routing matrix, the Root System Domain (RSD), or a Protected System Domain (PSD) |
scc, lcc (TX Matrix) |
|
Hostname of the local device |
Tokyo |
|
|
Local time when the script is executed |
Fri Dec 10 11:42:21 2010 |
|
|
Local time, in ISO format, when the script is executed |
2010-12-10 11:42:21 PST |
|
|
cscript process ID |
5257 |
|
|
Model of the local device |
m10i |
|
|
Empty element included if the script is executed on the primary Routing Engine |
– |
|
|
Routing Engine on which the script is executed |
re0 |
|
|
Type of script being executed |
op |
|
|
Element that enables a commit script to check if the commit occurs during the first reboot after a software install. The tag value is |
yes |
|
|
TTY of the user’s session |
/dev/ttyp1 |
|
|
|
Login class of the user executing the script |
superuser |
|
Login name of the user executing the script. For AAA access, this is the RADIUS/TACACS username. |
jsmith |
|
|
User ID of the user executing the script as defined in the device configuration |
2999 |
|
|
Local name of the user executing the script. Junos OS uses the
local name for authentication. It might differ from the
|
root |
|
(op scripts only) |
|
Empty element included if the remote op script is executed using
the |
– |
(commit scripts only) |
|
Empty element included when the commit occurs at boot time |
– |
|
Empty element included when a |
– |
|
|
User comment regarding the commit |
Commit to fix forwarding issue |
|
|
Empty element included when a |
– |
|
|
Empty element included when a |
– |
|
|
Element specifying the location of the session’s pre-inheritance
candidate configuration. For normal configuration sessions, the
value of the element is the location of the normal candidate
database. For private configuration sessions, the value of the
element is the location of the private candidate database. When
the |
– |
The $junos-context
variable is a node-set. Therefore, you can access
the child elements throughout a script by including the proper XPath
expression. The following SLAX commit script writes a message to the system log file
if the commit is performed during initial boot-up. The message is given a
facility value of daemon
and a severity value of
info
. For more information, see syslog().
version 1.2; 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 { if ($junos-context/commit-context/commit-boot) { expr jcs:syslog("daemon.info", "This is boot-time commit"); } else { /* Do this ... */ } }
Python scripts must import the Junos_Context
dictionary from the
junos
module to access the environment information provided to
scripts. The names of the keys in the Junos_Context
dictionary are
identical to the names of the $junos-context
nodes outlined in
Table 2. Nodes with child elements that are nested under the
junos-context
node such as user-context
,
op-context
, and commit-context
map to items in
Junos_Context
, where the key is the node name and the value is
a dictionary of the node’s child elements. For example:
'user-context': {'login-name': 'bsmith', 'user': 'bsmith', 'class-name': 'j-superuser', 'uid': '9999'}
The following example output displays the Junos_Context
dictionary
for an op script that was executed locally. Note that the op script input contains
the op-context
key, which in this scenario is empty.
{'product': 'm7i', 'user-context': {'login-name': 'bsmith', 'user': 'bsmith', 'class-name': 'j-superuser', 'uid': '9999'}, 'routing-engine-name': 're0', 'script-type': 'op', 're-master': None, 'hostname': 'R1', 'pid': '7136', 'tty': '/dev/ttyp1', 'chassis': 'others', 'op-context': ' ', 'localtime': 'Thu Jan 22 11:45:47 2015', 'localtime-iso': '2015-01-22 11:45:47 PST'}
The following example output displays the Junos_Context
dictionary
for a commit script that was executed during a commit check
operation. Note that the commit script input contains the
commit-context
key.
{'product': 'm7i', 'user-context': {'login-name': 'bsmith', 'user': 'bsmith', 'class-name': 'j-superuser', 'uid': '9999'}, 'routing-engine-name': 're0', 'script-type': 'commit', 're-master': None, 'hostname': 'R1', 'pid': '11201', 'tty': '/dev/ttyp1', 'commit-context': {'database-path': '/var/run/db/juniper.db', 'commit-check': None}, 'chassis': 'others', 'localtime': 'Thu Jan 22 16:23:55 2015', 'localtime-iso': '2015-01-22 16:23:55 PST'}
To access individual values in the dictionary, specify the key name. For example:
Junos_Context['hostname'] Junos_Context['user-context']['class-name']