Example: Configure an Interior Gateway Protocol on an Interface
This commit script example uses a macro to automatically
include an interface at the [edit protocols] hierarchy
level and to configure the proper interior gateway protocol (IGP)
on the interface.
Requirements
This example uses a device running Junos OS.
Overview and Commit Script
When you add a new interface to an OSPF or IS-IS domain, you
must configure the interface at multiple hierarchy levels, including [edit interfaces] and [edit protocols]. This
example uses a commit script and macro to automatically include the
interface at the [edit protocols] hierarchy level and to
configure the proper IGP on the interface, either OSPF or IS-IS, depending
on the content of an apply-macro statement that you include
in the interface configuration. This macro allows you to perform more
configuration tasks at a single hierarchy level.
In this example, the Junos OS management (mgd) process inspects
the configuration, looking for apply-macro statements.
For each apply-macro ifclass statement included at the [edit interfaces interface-name unit logical-unit-number] hierarchy level, the script
tests whether the role parameter is defined
as cpe. If so, the script checks the igp parameter.
If the igp parameter is defined as isis, the script includes the relevant interface name
at the [edit protocols isis interface] hierarchy level.
If the igp parameter is defined as ospf, the script includes the relevant interface name
at the [edit protocols ospf area address interface] hierarchy level. For OSPF, the script references
the area parameter to determine the correct
subnet address of the area.
The example script is shown in both XSLT and SLAX syntax:
XSLT Syntax
<?xml version="1.0" standalone="yes"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:junos="http://xml.juniper.net/junos/*/junos" xmlns:xnm="http://xml.juniper.net/xnm/1.1/xnm" xmlns:jcs="http://xml.juniper.net/junos/commit-scripts/1.0"> <xsl:import href="../import/junos.xsl"/> <xsl:template match="configuration"> <xsl:for-each select="interfaces/interface/unit/apply-macro[name = 'ifclass']"> <xsl:variable name="role" select="data[name='role']/value"/> <xsl:variable name="igp" select="data[name='igp']/value"/> <xsl:variable name="ifname"> <xsl:value-of select="../../name"/> <xsl:text>.</xsl:text> <xsl:value-of select="../name"/> </xsl:variable> <xsl:choose> <xsl:when test="$role = 'cpe'"> <change> <xsl:choose> <xsl:when test="$igp = 'isis'"> <protocols> <isis> <interface> <name><xsl:value-of select="$ifname"/></name> </interface> </isis> </protocols> </xsl:when> <xsl:when test="$igp = 'ospf'"> <protocols> <ospf> <area> <name> <xsl:value-of select="data[name='area']/value"/> </name> <interface> <name><xsl:value-of select="$ifname"/></name> </interface> </area> </ospf> </protocols> </xsl:when> </xsl:choose> </change> </xsl:when> </xsl:choose> </xsl:for-each> </xsl:template> </xsl:stylesheet>
SLAX Syntax
version 1.0;
ns junos = "http://xml.juniper.net/junos/*/junos";
ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
import "../import/junos.xsl";
match configuration {
for-each (interfaces/interface/unit/apply-macro[name = 'ifclass']) {
var $role = data[name='role']/value;
var $igp = data[name='igp']/value;
var $ifname = {
expr ../../name;
expr ".";
expr ../name;
}
if ($role = 'cpe') {
<change> {
if ($igp = 'isis') {
<protocols> {
<isis> {
<interface> {
<name> $ifname;
}
}
}
}
else if ($igp = 'ospf') {
<protocols> {
<ospf> {
<area> {
<name> data[name='area']/value;
<interface> {
<name> $ifname;
}
}
}
}
}
}
}
}
}Configuration
Procedure
Step-by-Step Procedure
To download, enable, and test the script:
Copy the script into a text file, name the file if-class.xsl or if-class.slax as appropriate, and copy it to the /var/db/scripts/commit/ directory on the device.
Select the following test configuration stanzas, and press Ctrl+c to copy them to the clipboard.
If you are using the SLAX version of the script, change the filename at the
[edit system scripts commit file]hierarchy level to if-class.slax.system { scripts { commit { file if-class.xsl; } } } interfaces { so-1/2/3 { unit 0 { apply-macro ifclass { area 10.4.0.0; igp ospf; role cpe; } } } t3-0/0/0 { unit 0 { apply-macro ifclass { igp isis; role cpe; } } } }In configuration mode, issue the
load merge terminalcommand to merge the stanzas into your device configuration.[edit] user@host# load merge terminal [Type ^D at a new line to end input] ... Paste the contents of the clipboard here ...
At the prompt, paste the contents of the clipboard by using the mouse and the paste icon.
Press Enter.
Press Ctrl+d.
Commit the configuration.
user@host# commit
Verification
Verifying the Configuration
Purpose
Verify that the script behaves as expected.
Action
View the configuration to verify that the manual changes and the script-generated changes are present.
When you issue the show interfaces configuration
mode command, the changes added by the sample configuration stanzas
should be present in the configuration.
[edit]
user@host# show interfaces
t3-0/0/0 {
unit 0 {
apply-macro ifclass {
igp isis;
role cpe;
}
}
}
so-1/2/3 {
unit 0 {
apply-macro ifclass {
area 10.4.0.0;
igp ospf;
role cpe;
}
}
}
When you issue the show protocols configuration
mode command, the script-generated changes should be present in the
configuration.
[edit]
user@host# show protocols
isis {
interface t3-0/0/0.0;
}
ospf {
area 10.4.0.0 {
interface so-1/2/3.0;
}
}