Use Named Templates in Junos OS Automation Scripts
Junos OS provides several named templates that can be used in commit, op, event, and SNMP scripts to more easily accomplish scripting tasks on devices running Junos OS. The following sections outline how to import and use the templates for different script languages:
Using Named Templates in SLAX and XSLT Scripts
To use the named templates within SLAX and XSLT scripts, the script must import the
junos.xsl file and also declare the appropriate
namespace Uniform Resource Identifier (URI) in the style sheet
declaration. The Junos OS named templates are defined in the namespace with the
associated URI http://xml.juniper.net/junos/commit-scripts/1.0
.
Import the junos.xsl file into
the script by including the <xsl:import/>
tag element in XSLT scripts or the import
statement in SLAX scripts and specifying the junos.xsl file location.
SLAX and XSLT scripts generally map the jcs
prefix to the URI to avoid name conflicts with standard XSLT or
user-defined templates. The scripts then qualify the named templates
with the appropriate prefix, which is expanded into its associated
URI reference during processing. Map the jcs
prefix to the URI by including the xmlns:jcs
attribute in the opening <xsl:stylesheet>
tag element for XSLT scripts or by including the ns
jcs
statement in SLAX scripts.
To call a named template in a script, include the <xsl:call-template name="template-name">
element in XSLT scripts or the call
statement in SLAX scripts and pass along any required or optional
parameters. Template parameters are assigned by name and can appear
in any order. This differs from functions where the arguments must
be passed into the function in the precise order specified by the
function definition.
The following example imports the junos.xsl file into a script and maps the jcs
prefix
to the namespace identified by the URI http://xml.juniper.net/junos/commit-scripts/1.0.
The script demonstrates a call to the jcs:edit-path
template.
XSLT Syntax
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:jcs="http://xml.juniper.net/junos/commit-scripts/1.0"> <xsl:import href="../import/junos.xsl"/> ... <xsl:for-each select="interfaces/interface[starts-with(name, 'so-')]"> <xnm:warning> <xsl:call-template name="jcs:edit-path"/> <message>interface configured</message> </xnm:warning> </xsl:for-each> ... </xsl: stylesheet>
SLAX Syntax
version 1.2; ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0"; import "../import/junos.xsl"; ... for-each ( interfaces/interface[starts-with(name,'so-') ) { <xnm:warning> { call jcs:edit-path(); <message> “interface configured”; } } ...
Using Named Template Functionality in Python Scripts
Python scripts that import the jcs
module can execute functions that are the Python equivalent of the
named templates used in SLAX and XSLT scripts. The functions provide
essentially the same functionality in the Python script as the corresponding
named template does in a SLAX or XSLT script. To determine which named
templates have functionality that is supported in Python scripts,
see Understanding Named Templates in Junos OS Automation Scripts.
To call the function equivalent to a named template within a Python script, include any required variable declarations, call the function, and pass along any required or optional arguments. Note that in Python scripts, the function names must use underscores instead of hyphens. For example:
Python Syntax
import jcs if __name__ == '__main__': script = "system-check.py" change_xml = """<system><scripts><op> <file><name>{0}</name></file></op> </scripts></system>""".format(script) jcs.emit_change(change_xml, "change", "xml")