Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

Example: Creating Custom Configuration Syntax with Commit Script Macros

 

A Junos OS configuration can contain apply-macro statements with custom configuration syntax. By itself, the apply-macro statement has no operational impact on the device. Commit script macros process the custom configuration syntax and expand it into standard Junos OS configuration statements, which are then added as a persistent or transient change. This example demonstrates how to use commit script macros to inspect apply-macro statements and generate Junos OS configuration statements.

Requirements

This example uses the following hardware and software components:

  • Device running Junos OS.

  • Junos OS Release 16.1R3 or later release when using a Python script.

Overview and Commit Script

Table 1 shows a macro containing custom syntax and the corresponding expansion to standard Junos OS command-line interface (CLI) syntax.

Table 1: Sample Macro and Junos OS CLI Expansion

Custom Macro Syntax

Expanded Junos OS CLI Syntax

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;
}
}
}
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;
}
}
}

In this example, the Junos OS management (mgd) process inspects the configuration, looking for apply-macro statements. For each apply-macro statement with the color parameter included at the [edit protocols mpls] hierarchy level, the script generates a transient change, using the data provided within the apply-macro statement to expand the macro into a standard Junos OS administrative group for LSPs.

For this example to work, an apply-macro statement must be included at the [edit protocols mpls] hierarchy level with a set of addresses, a color, and a group-value parameter. The commit script converts each address to an LSP configuration, and the script converts the color parameter into an administrative group.

Following are the commit script instructions that expand the macro in Table 1 and a line-by-line explanation of the script:

XSLT Syntax

Lines 1 through 8 (and Lines 43 and 44) are the boilerplate that you include in every XSLT commit script. For brevity, Lines 1 through 8 are omitted here.

Line 9 assigns the [edit protocols mpls] hierarchy level to a variable called mpls.

Line 10 selects every apply-macro statement at the [edit protocols mpls] hierarchy level that contains the color parameter. The sample configuration in Table 1 contains only one apply-macro statement. Therefore, this <xsl:for-each> programming instruction takes effect only once.

Line 11 assigns the value of the color parameter, in this case blue, to a variable called color.

Line 12 assigns the value of the group-value parameter, in this case 0, to a variable called group-value.

Lines 13 through 15 generate a transient change at the [edit protocols mpls] hierarchy level.

Lines 16 through 23 add the admin-groups statement to the configuration and assign the value of the color variable to the group name and the value of the group-value variable to the group value.

The resulting configuration statements are as follows:

Line 24 selects the name of every parameter that does not have a value assigned to it, which in this case are the four IP addresses. This <xsl:for-each> programming instruction uses recursion through the macro and selects each IP address in turn. The color and group-value parameters each have a value assigned (blue and 0, respectively), so this line does not apply to them.

Line 25 adds the label-switched-path statement in the configuration.

Lines 26 through 28 assign the label-switched-path a name that concatenates the value of the color variable, the text -lsp-, and the current IP address currently selected by Line 24 (represented by the “ .” ).

Line 29 adds the to statement to the configuration and sets its value to the IP address currently selected by Line 24.

Lines 30 through 34 add the admin-group include-any statement to the configuration and set its value to the value of the color variable.

The resulting configuration statements (for one pass) are as follows:

Lines 35 through 42 are closing tags.

SLAX Syntax

The equivalent SLAX script is:

Python Syntax

The following Python syntax produces the identical configuration changes:

For more information about this example, see Example: Configuring Administrative Groups for LSPs.

Configuration

Step-by-Step Procedure

To download, enable, and test the script:

  1. Copy the script into a text file, name the file lsp-admin.xsl, lsp-admin.slax, or lsp-admin.py as appropriate, and copy it to the /var/db/scripts/commit directory on the device.Note

    Unsigned Python scripts must be owned by either root or a user in the Junos OS super-user login class, and only the file owner can have write permission for the file.

  2. If the script is written in Python, enable the execution of unsigned Python scripts.

    Note

    The language python statement executes scripts using Python 2.7. To use Python 3 to execute Python scripts on devices running Junos OS Release 19.4R1 or later, configure the language python3 statement instead.

  3. Select the following test configuration stanzas, and press Ctrl+c to copy them to the clipboard. If you are using the SLAX or Python version of the script, update the filename at the [edit system scripts commit file] hierarchy level.

  4. In configuration mode, issue the load merge terminal command to merge the stanzas into your device configuration.

    1. At the prompt, paste the contents of the clipboard by using the mouse and the paste icon.

    2. Press Enter.

    3. Press Ctrl+d.

  5. Commit the configuration.

Verification

Verifying Script Execution

Purpose

Verify that the script behaves as expected.

Action

To display the configuration statements created by the script, issue the show protocols mpls | display commit-scripts command.