- play_arrow Overview
- play_arrow Junos Automation Scripts Overview
- play_arrow Junos XML Management Protocol and Junos XML API Overview
-
- play_arrow Automation Scripting Using XSLT
- play_arrow XSLT Overview
- play_arrow Standard XPath and XSLT Functions Used in Automation Scripts
- play_arrow Standard XSLT Elements and Attributes Used in Automation Scripts
-
- play_arrow Automation Scripting Using SLAX
- play_arrow SLAX Overview
- SLAX Overview
- SLAX Syntax Rules Overview
- SLAX Elements and Element Attributes Overview
- SLAX Elements as Function Arguments
- Understanding SLAX Default Namespaces
- XPath Expressions Overview for SLAX
- SLAX Templates Overview
- SLAX Functions Overview
- SLAX Parameters Overview
- SLAX Variables Overview
- SLAX Statements Overview
- XSLT Elements Without SLAX Equivalents
- SLAX Operators
- play_arrow SLAX Statements
- append
- apply-imports
- apply-templates
- attribute
- attribute-set
- call
- copy-node
- copy-of
- decimal-format
- element
- else
- else if
- expr
- fallback
- for
- for-each
- function
- if
- import
- key
- match
- message
- mode
- mvar
- number
- output-method
- param
- preserve-space
- priority
- processing-instruction
- result
- set
- sort
- strip-space
- template
- terminate
- trace
- uexpr
- use-attribute-sets
- var
- version
- while
- with
- play_arrow The libslax Distribution for Automation Scripting
- libslax Distribution Overview
- libslax Library and Extension Libraries Overview
- Download and Install the libslax Distribution
- libslax Default Extension Libraries: bit, curl, db, os, and xutil
- Understanding the SLAX Processor (slaxproc)
- How to Use the SLAX Processor (slaxproc)
- SLAX Debugger, Profiler, and callflow
-
- play_arrow Automation Scripting Using Python
- play_arrow Python Overview
- Understanding Python Automation Scripts for Junos Devices
- Requirements for Executing Python Automation Scripts on Junos Devices
- Overview of Python Modules on Junos Devices
- How to Use Python Interactive Mode on Devices Running Junos OS
- How to Use the psutil Module to Retrieve Process and System Information on Devices Running Junos OS
- How to Use the Requests Library for Python on Devices Running Junos OS
- IPv6 Support in Python Automation Scripts
- How to Specify the Routing Instance in Python 3 Applications on Devices Running Junos OS Evolved
-
- play_arrow Automation Script Input
- play_arrow Global Parameters in Automation Scripts
-
- play_arrow Extension Functions and Named Templates for Automation Scripts
- play_arrow Extension Functions for Automation Scripting
- play_arrow Extension Functions in the jcs and slax Namespaces
- base64-decode() Function (SLAX)
- base64-encode() Function (SLAX)
- break-lines() Function (SLAX and XSLT)
- close() Function (SLAX and XSLT)
- dampen() Function (Python, SLAX, and XSLT)
- document() Function (SLAX)
- emit_error() Function (Python)
- emit_snmp_attributes Function (Python)
- emit_warning() Function (Python)
- empty() Function (SLAX and XSLT)
- evaluate() Function (SLAX)
- execute() Function (SLAX and XSLT)
- first-of() Function (SLAX and XSLT)
- get-command() Function (SLAX)
- get-hello() Function (SLAX and XSLT)
- get-input() Function (SLAX and XSLT) and get_input() (Python)
- get-protocol() Function (SLAX and XSLT)
- get-secret() Function (SLAX and XSLT) and get_secret() (Python)
- get_snmp_action() Function (Python)
- get_snmp_oid() Function (Python)
- hostname() Function (Python, SLAX, and XSLT)
- invoke() Function (SLAX and XSLT)
- open() Function (SLAX and XSLT)
- output() Function (Python, SLAX, and XSLT)
- parse-ip() Function (SLAX and XSLT) and parse_ip() (Python)
- printf() Function (Python, SLAX, and XSLT)
- progress() Function (Python, SLAX, and XSLT)
- regex() Function (SLAX and XSLT)
- set_routing_instance() Function (Python)
- sleep() Function (SLAX and XSLT)
- split() Function (SLAX and XSLT)
- sysctl() Function (Python, SLAX, and XSLT)
- syslog() Function (Python, SLAX, and XSLT)
- trace() Function (Python, SLAX, and XSLT)
- play_arrow Named Templates for Automation Scripting
- play_arrow Named Templates in the jcs Namespace
-
- play_arrow Manage Automation Scripts
- play_arrow Store and Enable Scripts
- play_arrow Configure a Remote Source for Scripts
- play_arrow Configure the Session Protocol for Scripts
- play_arrow Control Execution of Scripts
- play_arrow Synchronize Scripts Between Routing Engines
- play_arrow Convert Scripts Between SLAX and XSLT
-
- play_arrow Op Scripts
- play_arrow Op Scripts Overview
- play_arrow Create and Execute Op Scripts
- Required Boilerplate for Op Scripts
- Map Operational Mode Commands and Output Fields to Junos XML Notation
- How to Use RPCs and Operational Mode Commands in Op Scripts
- Declare and Use Command-Line Arguments in Op Scripts
- Configure Help Text for Op Scripts
- Define Operational Mode Commands to Allow in an Op Script
- Enable an Op Script and Define a Script Alias
- Configure Checksum Hashes for an Op Script
- Execute an Op Script on the Local Device
- Execute an Op Script from a Remote Site
- Disable an Op Script
- play_arrow Op Script Examples
- Change the Configuration Using SLAX and XSLT Scripts
- Example: Change the Configuration Using SLAX and XSLT Op Scripts
- Example: Change the Configuration Using Python Op Scripts
- Example: Customize Output of the show interfaces terse Command Using an Op Script
- Example: Display DNS Hostname Information Using an Op Script
- Example: Find LSPs to Multiple Destinations Using an Op Script
- Example: Restart an FPC Using an Op Script
- Example: Export Files Using an Op Script
- Example: Import Files Using an Op Script
- Example: Search Files Using an Op Script
- play_arrow Provision Services Using Service Template Automation
- play_arrow Troubleshoot Op Scripts
-
- play_arrow Event Policies and Event Scripts
- play_arrow Event Policy Overview
- play_arrow Event Policy Triggers
- Use Correlated Events to Trigger an Event Policy
- Trigger an Event Policy Based on Event Count
- Example: Trigger an Event Policy Based on Event Count
- Use Regular Expressions to Refine the Set of Events That Trigger a Policy
- Example: Controlling Event Policy Using a Regular Expression
- Generate Internal Events to Trigger Event Policies
- Use Nonstandard System Log Messages to Trigger Event Policies
- Junos Logger Utility
- play_arrow Event Policy Actions
- Configure an Event Policy to Execute Operational Mode Commands
- Configure an Event Policy to Change the Configuration
- Example: Changing the Configuration Using an Event Policy
- Example: Changing the Interface Configuration in Response to an Event
- Execute Event Scripts in an Event Policy
- Change the Configuration Using an Event Script
- Configuring an Event Policy to Pass Arguments to an Event Script
- Configure Event Policies to Ignore an Event
- Example: Ignore Events Based on Receipt of Other Events
- Overview of Using Event Policies to Raise SNMP Traps
- Example: Raise an SNMP Trap in Response to an Event
- Understanding the Event System Log Priority in an Event Policy
- Example: Configuring the Event System Log Priority in an Event Policy
- Example: Limit Event Script Output Based on a Specific Event Type
- play_arrow Configure Event Policy File Archiving
- play_arrow Configure Event Policy Privileges
- play_arrow Event Scripts Overview
- play_arrow Create and Execute Event Scripts
- play_arrow Troubleshoot Event Policies and Event Scripts
-
- play_arrow SNMP Scripts
- play_arrow SNMP Scripts Overview
- play_arrow Create and Execute SNMP Scripts
- play_arrow SNMP Script Example
- play_arrow Troubleshoot SNMP Scripts
-
- play_arrow Configuration Statements and Operational Commands
Create Custom Configuration Syntax with Commit Script Macros
Commit script macros enable you to create custom configuration syntax and expand it into standard Junos OS configuration statements. Your custom syntax serves as input to a commit script. The output of the commit script is standard Junos OS configuration syntax, as shown in Figure 1. The standard Junos OS statements are added to the configuration to cause your intended operational changes.

Macros use either persistent or transient change elements to expand your custom syntax into standard Junos OS configuration statements. If you use persistent changes, both the custom syntax and the standard Junos OS syntax appear in the candidate configuration. If you use transient changes, the custom syntax appears in the candidate configuration, and the standard Junos OS syntax is copied to the checkout configuration only.
This section discusses the following topics:
Creating a Custom Syntax
Macros work by locating apply-macro
statements in
the candidate configuration and using the values specified in the apply-macro
statement as parameters to a set of instructions
defined in a commit script. In effect, your custom configuration syntax
serves a dual purpose. The syntax allows you to simplify your configuration
tasks, and it provides to the script the data necessary to generate
a complex configuration.
To enter custom syntax, you include the apply-macro
statement at any hierarchy level and specify any data that you want
inside the apply-macro
statement, for example:
apply-macro macro-name { parameter-name parameter-value; }
You can include the apply-macro
statement at any
level of the configuration hierarchy. In this sense, the apply-macro
statement is similar to the apply-groups
statement. Each apply-macro
statement must be uniquely named, relative to other apply-macro
statements at the same hierarchy level.
An apply-macro
statement can contain a set of parameters
with optional values. The corresponding commit script can refer to
the macro name, its parameters, or the parameters’ values. When
the script inspects the configuration and finds the data, the script
performs the actions specified by the corresponding persistent or
transient change.
For example, given the following configuration stanza, you can write script instructions to generate a standard configuration based on the name of the parameter:
protocols { mpls { apply-macro blue-type-lsp { color blue; } } }
The following <xsl:for-each>
programming
instruction finds apply-macro
statements at the [edit
protocols mpls]
hierarchy level that contain a parameter named color
:
<xsl:for-each select="protocols/mpls/apply-macro[data/name = 'color']">
The following instruction creates a variable named color
and assigns to the variable the value of the color
parameter, which in this case is blue
:
<xsl:variable name="color" select="data[name = 'color']/value"/>
The following instruction adds the admin-groups
statement
to the configuration and assigns the value of the color
variable to the group name:
<transient-change> <protocols> <mpls> <admin-groups> <name> <xsl:value-of select="$color"/> </name> </admin-groups> </mpls> </protocols> </transient-change>
The resulting configuration statements are as follows:
protocols { mpls { admin-groups { blue; } } }
<data> Element
In the XML rendering of the custom syntax within an apply-macro
statement, parameters and their values are contained in <name>
and <value>
elements, respectively. The <name>
and <value>
elements are sibling children
of the <data>
element. For example,
the apply-macro blue-type-lsp
statement contains six
parameters, as follows:
[edit protocols mpls] apply-macro blue-type-lsp { 10.1.1.1; 10.2.2.2; 10.3.3.3; 10.4.4.4; color blue; group-value 0; }
The parameters and values are rendered in Junos XML tag elements as follows:
[edit protocols mpls] user@host# show | display xml <rpc-reply xmlns:junos="http://xml.juniper.net/junos/10.0R1/junos"> <configuration> <protocols> <mpls> <apply-macro> <name>blue-type-lsp</name> <data> <name>10.1.1.1</name> </data> <data> <name>10.2.2.2</name> </data> <data> <name>10.3.3.3</name> </data> <data> <name>10.4.4.4</name> </data> <data> <name>color</name> <value>blue</value> </data> <data> <name>group-value</name> <value>0</value> </data> </apply-macro> </mpls> </protocols> </configuration> </rpc-reply>
When you write commit script macros, you can extract and manipulate
the parameters contained in apply-macro
statements by referring
to the <data>
, <name>
, and <value>
elements.
In the following example, the select
attribute’s XPath expression
extracts the text contained in the <value>
element that is a child of a <data>
element that also contains a <name>
child element with the text color
. The
variable declaration assigns the text of the <value>
element to a variable named color
.
<xsl:variable name="color" select="data[name = 'color']/value"/>
The SLAX equivalent is:
var $color = ./data[name='color']/value;
The Python equivalent, which assumes that element
has selected an apply-macro
element,
is:
color = element.find("data[name='color']/value").text
Expanding the Custom Syntax
In the corresponding commit script, you include one or more
programming instructions that inspect the configuration for the apply-macro
statement at a specified hierarchy level. Optionally,
you can use the data/name
expression to
select a parameter in the apply-macro
statement.
<xsl:for-each select="xpath-expression/apply-macro[data/name = 'parameter-name']">
For example, the following XSLT programming instruction selects
every apply-macro
statement that contains the color
parameter and that appears at the [edit
protocols mpls]
hierarchy level:
<xsl:for-each select="protocols/mpls/apply-macro[data/name = 'color']">
The SLAX equivalent is:
for-each (protocols/mpls/apply-macro[data/name = 'color']) {
The Python equivalent, which spans multiple lines for readability, is:
for element in Junos_Configuration.xpath \ ("./protocols/mpls/apply-macro[data/name='color']"):
When expanding macros, a particularly useful programming instruction
in XSLT scripts is the <xsl:value-of>
instruction. This instruction selects a parameter value and uses
it to build option values for Junos OS statements. For example, the
following instruction concatenates the value of the color
variable, the text -lsp-
, and the current context node (represented by “ .
” ) to build a name for an LSP.
<label-switched-path> <name> <xsl:value-of select="concat($color, '-lsp-', .)"/> </name> </label-switched-path>
SLAX uses the underscore (_
) to concatenate
values.
<label-switched-path> { <name> $color _ '-lsp-' _ .; }
When the script includes instructions to find the necessary data, you can provide content for a persistent or transient change that uses the data to construct a standard Junos OS configuration.
The following transient change creates an administration group
and adds the label-switched-path
statement to the configuration.
The label-switched path is assigned
a name that concatenates the value of the color
variable, the text -lsp-
, and the currently
selected IP address represented by the period (“.
”). The transient change also adds the to
statement and assigns the currently selected IP address.
Finally, the transient change adds the admin-group include-any
statement and assigns the value of the color
variable.
<transient-change> <protocols> <mpls> <admin-groups> <name><xsl:value-of select="$color"/></name> <group-value><xsl:value-of select="$group-value"/></group-value> </admin-groups> <xsl:for-each select="data[not(value)]/name"> <label-switched-path> <name><xsl:value-of select="concat($color, '-lsp-', .)"/></name> <to><xsl:value-of select="."/></to> <admin-group> <include-any><xsl:value-of select="$color"/></include-any> </admin-group> </label-switched-path> </xsl:for-each> </mpls> </protocols> </transient-change>
The SLAX equivalent is:
<transient-change> { <protocols> { <mpls> { <admin-groups> { <name> $color; <group-value> $group-value; } for-each (data[not(value)]/name) { <label-switched-path> { <name> $color _ '-lsp-' _ .; <to> .; <admin-group> { <include-any> $color; } } } } } }
Similarly in Python:
lsp_config ="" for element2 in element.xpath("data[not(value)]/name"): lsp_config = lsp_config + """ <label-switched-path> <name>{0}-lsp-{1}</name> <to>{1}</to> <admin-group> <include-any>{0}</include-any> </admin-group> </label-switched-path> """.format(color, element2.text) change_xml = """ <protocols> <mpls> <admin-groups> <name>{0}</name> <group-value>{1}</group-value> </admin-groups> {2} </mpls> </protocols> """.format(color, group_value, lsp_config).strip() jcs.emit_change(change_xml, "transient-change", "xml")
The example shown here is partial. For a full example, see Example: Creating Custom Configuration Syntax with Commit Script Macros.
After committing the configuration, the script runs, and the resulting full configuration looks like this:
[edit] protocols { mpls { admin-groups { blue 0; } label-switched-path blue-lsp-10.1.1.1 { to 10.1.1.1; admin-group include-any blue; } label-switched-path blue-lsp-10.2.2.2 { to 10.2.2.2; admin-group include-any blue; } label-switched-path blue-lsp-10.3.3.3 { to 10.3.3.3; admin-group include-any blue; } label-switched-path blue-lsp-10.4.4.4 { to 10.4.4.4; admin-group include-any blue; } } }
The previous example demonstrates how you can use a simplified custom syntax to configure label-switched paths (LSPs). If your network design requires a large number of LSPs to be configured, using a commit script macro can save time, ensure consistency, and prevent configuration errors.
Other Ways to Use Macros
The example discussed in Creating a Custom Syntax shows a macro that uses transient changes to create the intended operational impact. Alternatively, you can create a commit script that uses persistent changes to add the standard Junos OS statements to the candidate configuration and delete your custom syntax entirely. This way, a network operator who might be unfamiliar with your custom syntax can view the configuration file and see the full configuration rendered as standard Junos OS statements. Still, because the commit script macro remains in effect, you can quickly and easily create a complex configuration using your custom syntax.
In addition to the type of application discussed in Creating a Custom Syntax, you can also use macros to prevent a
commit script from performing a task. For example, a basic commit
script that automatically adds MPLS configuration to interfaces can
make an exception for interfaces you explicitly tag as not requiring
MPLS, by testing for the presence of an apply-macro
statement
named no-mpls
. For an example of this use
of macros, see Example: Control LDP Configuration.
You can use the apply-macro
statement as a place
to store external data. The commit script does not inspect the apply-macro
statement, so the apply-macro
statement
has no operational impact on the device, but the data can be carried
in the configuration file to be used by external applications.