Blogs

Scripting How-To: Configuration changes in set format within a Junos OS script

By Erdem posted 08-08-2015 00:00

  

Overview

 

Making set changes within a Junos OS script. This applies to SLAX version 1.0 and higher.

 

Feature

 

Starting with Junos OS Release 11.3, it is possible for a Junos OS script to load a configuration in set format. However, because the jcs:load-configuration template currently only works in XML format, changes in set format must be performed by invoking each RPC individually: <lock-configuration>, <load-configuration>, <commit-configuration>, and then <unlock-configuration>.

 

In addition, there are three requirements specific to loading the configuration in set format:

 

  • The <load-configuration> RPC must have these attribute values: format="text" and action="set".
    • It actually works without format="text", but it is probably best practice to include it anyway.
  • <configuration-set> must be the child element of <load-configuration>, and it must be assigned the set format changes as its string value.
  • Multiple set or delete commands must be separated by a newline.

Below is an example op script that demonstrates how this can be accomplished. Because there are multiple ways to encode the <configuration-set>element, the script contains two different strategies that can be followed.

 

01    version 1.0;
02    ns junos = "http://xml.juniper.net/junos/*/junos";
03    ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
04    ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
05    import "../import/junos.xsl";
06    match / {
07        <op-script-results> {
08            var $connection = jcs:open();
09        var $lock-result = jcs:execute( $connection, "lock-configuration" );
10            if( $lock-result//self::xnm:error ) {
11                <xsl:message terminate="yes"> "Couldn't lock database";
12            }
13            /* Each set/delete command must be separated by a newline, but you can do it
14             * in difference ways:
15             * 1: Using expr: */
16            var $load-rpc1 = {
17                <load-configuration action="set" format="text"> {
18                    <configuration-set> {
19                        expr "set system host-name testing\n";
20                        expr "delete system services telnet\n";
21                        expr "set routing-options static route 0/0 discard";
22                    }
23                }
24            }
25            /* 2: Using a run-on string */
26            var $load-rpc2 = {
27                <load-configuration action="set" format="text"> {
28                <configuration-set> "
29    set routing-options static route 192/8 discard
30    set snmp community location";
31                }
32            }
33            var $load-result1 = jcs:execute( $connection, $load-rpc1 );
34            var $load-result2 = jcs:execute( $connection, $load-rpc2 );
35        if( $load-result1//self::xnm:error | $load-result2//self::xnm:error ) {
36                copy-of jcs:execute( $connection, "unlock-configuration");
37                <xsl:message terminate="yes"> "Error loading the configuration";
38            }
39            var $commit-result = jcs:execute( $connection, "commit-configuration" );
40            if( $commit-result//self::xnm:error ) {
41                copy-of jcs:execute( $connection, "unlock-configuration");
42                <xsl:message terminate="yes"> "Error committing the configuration";
43            }
44            copy-of jcs:execute( $connection, "unlock-configuration" );
45            expr jcs:close( $connection );
46        }
47    }

 

The script successfully performs the following configuration changes:

 

01    admin@testing> show configuration | compare rollback 1
02    [edit system]
03    -  host-name DAL-3200-1;
04    +  host-name testing;
05    [edit system services]
06    -    telnet;
07    [edit]
08    +  snmp {
09    +      community location;
10    +  }
11    [edit routing-options static]
12         route 172.16.0.0/12 { ... }
13    +    route 0.0.0.0/0 discard;
14    +    route 192.0.0.0/8 discard;

#opscript
#JunosOS
#Slax
#How-To
#ScriptingHow-To