Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

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

SLAX Syntax

Python

To reference the information in Python scripts, import the Junos_Context dictionary.

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.

Table 1: Predefined Parameters Available in SLAX and XSLT Scripts

Name

Description

Example

$hostname

Hostname of the local device

Tokyo

$localtime

Local time when the script is executed

Fri Dec 10 11:42:21 2010

$localtime-iso

Local time, in ISO format, when the script is executed

2010-12-10 11:42:21 PST

$product

Model of the local device

m10i

$script

Filename of the executing script

test.slax

$user

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:

XSLT syntax:

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:

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.

For commit scripts, the <commit-context> element is also included in the source tree provided to a commit script.

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.

Table 2: Global Variable $junos-context Available to SLAX and XSLT Scripts

Parent Node         

Node             

Description

Example Content

<junos-context>

<chassis>

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)
psd, rsd (JCS)
others

<hostname>

Hostname of the local device

Tokyo

<localtime>

Local time when the script is executed

Fri Dec 10 11:42:21 2010

<localtime-iso>

Local time, in ISO format, when the script is executed

2010-12-10 11:42:21 PST

<pid>

cscript process ID

5257

<product>

Model of the local device

m10i

<re-master/>

Empty element included if the script is executed on the primary Routing Engine

<routing-engine-name>

Routing Engine on which the script is executed

re0

<script-type>

Type of script being executed

op

<sw-upgrade-in-progress>

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 if the commit takes place during the first reboot after a software upgrade, software downgrade, or rollback. The tag value is no if the device is booting normally.

yes

<tty>

TTY of the user’s session

/dev/ttyp1

<junos-context>
    <user-context>

<class-name>

Login class of the user executing the script

superuser

<login-name>

Login name of the user executing the script. For AAA access, this is the RADIUS/TACACS username.

jsmith

<uid>

User ID of the user executing the script as defined in the device configuration

2999

<user>

Local name of the user executing the script. Junos OS uses the local name for authentication. It might differ from the login-name used for AAA authentication.

root

<junos-context>
    <op-context>

(op scripts only)

<via-url>

Empty element included if the remote op script is executed using the op url command

<junos-context>
    <commit-context>

(commit scripts only)

<commit-boot/>

Empty element included when the commit occurs at boot time

<commit-check/>

Empty element included when a commit check is performed

<commit-comment>

User comment regarding the commit

Commit to fix forwarding issue

<commit-confirm/>

Empty element included when a commit confirmed is performed

<commit-sync/>

Empty element included when a commit synchronize is performed

<database-path/>

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 <get-configuration> database-path attribute is set to this value, the commit script retrieves the corresponding pre-inheritance candidate configuration.

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().

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:

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.

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.

To access individual values in the dictionary, specify the key name. For example: