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 the scripts 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 you must import into the script.

To use the pre-defined parameters or global variable in SLAX and XSLT scripts, you must import the junos.xsl file as shown in the following examples:

XSLT Syntax

SLAX Syntax

Python

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

This topic describes the script input in detail.

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 at which the script executes

Fri Dec 10 11:42:21 2024

$localtime-iso

Local time, in ISO format, at which the script executes

2024-12-10 11:42:21 PST

$product

Model of the local device

mx960

$script

Filename of the executing script

test.slax

$user

Local name of the user executing the script

root

The junos.xsl file declares the predefined global parameters. Thus, you do not need to declare these parameters in your 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 examples:

SLAX syntax:

XSLT syntax:

Global Variable Available in Automation Scripts

Commit, event, and op scripts can access specific environment information upon execution. To access this information, Python scripts must import and reference the junos.Junos_Context dictionary. 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. The source tree provided to an op script includes the <op-context> element.

The source tree provided to a commit script includes the <commit-context> element.

Table 2 identifies each node of the $junos-context variable node-set. For each node, the table also provides a brief description and gives examples for those elements that have values.

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 the Root System Domain (RSD) or a Protected System Domain (PSD)


psd, rsd (JCS)
others

<hostname>

Hostname of the local device

Tokyo

<localtime>

Local time when the script executes

Fri Dec 10 11:42:21 2024

<localtime-iso>

Local time, in ISO format, when the script executes

2024-12-10 11:42:21 PST

<pid>

cscript process ID

5257

<product>

Model of the local device

mx960

<re-master/>

Empty element included if the script executes on the primary Routing Engine

<routing-engine-name>

Routing Engine on which the script executes

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 value is the RADIUS/TACACS username.

admin

<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 you use the op url command to execute the remote op script

<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 the script executes during a commit check operation

<commit-comment>

User comment regarding the commit

Commit to fix forwarding issue

<commit-confirm/>

Empty element included when the script executes during a commit confirmed operation

<commit-sync/>

Empty element included when the script executes during a commit synchronize operation

<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 occurs during the initial boot-up. The script assigns a facility value of daemon and a severity value of info to the message. For more information, see syslog().

Python scripts must import the Junos_Context dictionary from the junos module to access the environment-specific information provided to scripts. 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 a locally executed op script. 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: