Required Boilerplate for Commit Scripts

 

Junos OS commit scripts can be written in Extensible Stylesheet Language Transformations (XSLT), Stylesheet Language Alternative syntaX (SLAX), or Python. Commit scripts must include the necessary boilerplate required for that script language for both basic script functionality as well as any optional functionality used within the script such as the Junos OS extension functions and named templates. This topic provides standard boilerplate that can be used in XSLT, SLAX, and Python scripts.

SLAX and XSLT commit scripts are based on Junos XML and Junos XML protocol tag elements. Like all XML elements, angle brackets enclose the name of a Junos XML or Junos XML protocol tag element in its opening and closing tags. This is an XML convention, and the brackets are a required part of the complete tag element name. They are not to be confused with the angle brackets used in the documentation to indicate optional parts of Junos OS CLI command strings.

XSLT Boilerplate for Commit Scripts

The XSLT commit script boilerplate is as follows:

Line 1 is the Extensible Markup Language (XML) processing instruction (PI). This PI specifies that the code is written in XML using version 1.0. The XML PI, if present, must be the first noncomment token in the script file.

Line 2 opens the style sheet and specifies the XSLT version as 1.0.

Lines 3 through 6 list all the namespace mappings commonly used in commit scripts. Not all of these prefixes are used in this example, but it is not an error to list namespace mappings that are not referenced. Listing all namespace mappings prevents errors if the mappings are used in later versions of the script.

Line 7 is an XSLT import statement. It loads the templates and variables from the file referenced as ../import/junos.xsl, which ships as part of the Junos OS. The junos.xsl file contains a set of named templates you can call in your scripts. These named templates are discussed in Understanding Named Templates in Junos OS Automation Scripts.

Line 8 defines a template that matches the <configuration> element, which is the node selected by the <xsl:template match="/"> template, contained in the junos.xsl import file. The <xsl:template match="configuration"> element allows you to exclude the /configuration/ root element from all XPath expressions in the script and begin XPath expressions with the top Junos OS hierarchy level. For more information, see XPath Overview.

Add your code between Lines 8 and 9.

Line 9 closes the template.

Line 10 closes the style sheet and the commit script.

SLAX Boilerplate for Commit Scripts

The corresponding SLAX commit script boilerplate is as follows:

Python Boilerplate for Commit Scripts

Python commit scripts do not have a required boilerplate, but they must import any objects that are used in the script. Python automation scripts import the Junos_Context dictionary when the script must access information about the script execution environment. The Junos_Configuration object is required when the commit script must read and manipulate the post-inheritance candidate configuration. Python automation scripts that include the import jcs statement can use Junos OS extension functions and Junos OS named template functionality in the script.

Python automation scripts do not need to include an interpreter directive line (#!/usr/bin/env python) at the start of the script. However, the program will still execute correctly if one is present.