Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

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.

Figure 1: Macro Input and OutputMacro Input and Output

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:

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:

The following <xsl:for-each> programming instruction finds apply-macro statements at the [edit protocols mpls] hierarchy level that contain a parameter named 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:

The following instruction adds the admin-groups statement to the configuration and assigns the value of the color variable to the group name:

The resulting configuration statements are as follows:

<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:

The parameters and values are rendered in Junos XML tag elements as follows:

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.

The SLAX equivalent is:

The Python equivalent, which assumes that element has selected an apply-macro element, is:

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.

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:

The SLAX equivalent is:

The Python equivalent, which spans multiple lines for readability, is:

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.

SLAX uses the underscore (_) to concatenate values.

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.

The SLAX equivalent is:

Similarly in Python:

Note:

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:

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.