Blog Viewer

Scripting How-To: Change VRRP parameters

By Erdem posted 08-09-2015 09:19

  

Overview

This script changes VRRP parameters if a named apply-group is also configured.

Description


The only limitation of the pre-9.2 configuration is that there must be at least one statement inherited from the DRAIN-ME group.

Source Code

 

GitHub Links


The source code below is also available from GitHub at the following locations:

Example Configuration

 

001	This script changes VRRP parameters if a named apply-group is also configured.
002	 
003	The only limitation of the pre-9.2 configuration is that there must be at least one statement inherited from the DRAIN-ME group.
004	 
005	|---> Normal configuration
006	 
007	No DRAIN-ME group applied
008	 
009	user@router# show interfaces | display commit-scripts
010	ge-2/0/1 {
011	    unit 0 {
012	        family inet {
013	            address 1.1.1.1/24 {
014	                vrrp-group 1 {
015	                    virtual-address 1.1.1.254;
016	                    priority 200;
017	                }
018	            }
019	        }
020	    }
021	}
022	 
023	user@router# run show vrrp extensive
024	Interface: ge-2/0/1.0, Interface index :0, Groups: 1, Active :0
025	 
026	Physical interface: ge-2/0/1, Unit: 0, Address: 1.1.1.1/24
027	  Index: 69, SNMP ifIndex: 131, VRRP-Traps: disabled
028	  Interface state: down, Group: 1, State: init
029	  Priority: 200, Advertisement interval: 1, Authentication type: none
030	  Delay threshold: 100, Computed send rate: 0
031	  Preempt: yes, Accept-data mode: no, VIP count: 1, VIP: 1.1.1.254         
032	  Master router: N/A
033	  Tracking: disabled
034	 
035	|---> Add the DRAIN-ME group
036	 
037	user@router# show | compare rollback 0 
038	[edit]
039	- apply-groups [ re0 re1 anz-defaults ];
040	+ apply-groups [ re0 re1 anz-defaults DRAIN-ME ];
041	 
042	user@router# commit | display detail
043	2008-12-16 17:41:45 EST: reading commit script configuration
044	2008-12-16 17:41:45 EST: testing commit script configuration
045	2008-12-16 17:41:45 EST: opening commit script '/var/db/scripts/commit/drain-vrrp.slax'
046	2008-12-16 17:41:45 EST: reading commit script 'drain-vrrp.slax'
047	2008-12-16 17:41:45 EST: running commit script 'drain-vrrp.slax'
048	2008-12-16 17:41:45 EST: processing commit script 'drain-vrrp.slax'
049	2008-12-16 17:41:45 EST: no errors from drain-vrrp.slax
050	2008-12-16 17:41:45 EST: saving commit script changes for script drain-vrrp.slax
051	2008-12-16 17:41:45 EST: summary of script drain-vrrp.slax: changes 0, transients 1 (allowed), syslog 0 <---| Transient change
052	2008-12-16 17:41:45 EST: no commit script changes
053	2008-12-16 17:41:45 EST: finished loading commit script changes
054	<snip>
055	 
056	Config not changed directly
057	 
058	user@router# show interfaces
059	ge-2/0/1 {
060	    unit 0 {
061	        family inet {
062	            address 1.1.1.1/24 {
063	                vrrp-group 1 {
064	                    virtual-address 1.1.1.254;
065	                    priority 200;
066	                }
067	            }
068	        }
069	    }
070	}
071	 
072	Transient config change
073	 
074	user@router# show interfaces | display commit-scripts
075	ge-2/0/1 {
076	    unit 0 {
077	        family inet {
078	            address 1.1.1.1/24 {
079	                vrrp-group 1 {
080	                    virtual-address 1.1.1.254;
081	                    priority 1;                   <---| transient change
082	                }
083	            }
084	        }
085	    }
086	}
087	 
088	[edit]
089	user@router# run show vrrp extensive
090	Interface: ge-2/0/1.0, Interface index :0, Groups: 1, Active :0
091	 
092	Physical interface: ge-2/0/1, Unit: 0, Address: 1.1.1.1/24
093	  Index: 69, SNMP ifIndex: 131, VRRP-Traps: disabled
094	  Interface state: down, Group: 1, State: init
095	  Priority: 1, Advertisement interval: 1, Authentication type: none     <---| Priority change
096	  Delay threshold: 100, Computed send rate: 0
097	  Preempt: yes, Accept-data mode: no, VIP count: 1, VIP: 1.1.1.254         
098	  Master router: N/A
099	  Tracking: disabled
100	 
101	  |---> UNDRAIN the router
102	 
103	  user@router# show | compare rollback 0
104	[edit]
105	- apply-groups [ re0 re1 anz-defaults DRAIN-ME ];
106	+ apply-groups [ re0 re1 anz-defaults ];
107	 
108	user@router# commit | display detail
109	2008-12-16 17:42:54 EST: reading commit script configuration
110	2008-12-16 17:42:54 EST: testing commit script configuration
111	2008-12-16 17:42:54 EST: opening commit script '/var/db/scripts/commit/drain-vrrp.slax'
112	2008-12-16 17:42:54 EST: reading commit script 'drain-vrrp.slax'
113	2008-12-16 17:42:54 EST: running commit script 'drain-vrrp.slax'
114	2008-12-16 17:42:54 EST: processing commit script 'drain-vrrp.slax'
115	2008-12-16 17:42:54 EST: no errors from drain-vrrp.slax
116	2008-12-16 17:42:54 EST: saving commit script changes for script drain-vrrp.slax
117	2008-12-16 17:42:54 EST: summary of script drain-vrrp.slax: changes 0, transients 0 (allowed), syslog 0 <---| No transient change applied
118	2008-12-16 17:42:54 EST: no commit script changes
119	2008-12-16 17:42:54 EST: finished loading commit script changes
120	<snip>
121	 
122	user@router# show interfaces | display commit-scripts
123	ge-2/0/1 {
124	    unit 0 {
125	        family inet {
126	            address 1.1.1.1/24 {
127	                vrrp-group 1 {
128	                    virtual-address 1.1.1.254;
129	                    priority 200;               <---| priority is unaltered
130	                }
131	            }
132	        }
133	    }
134	}
135	 
136	[edit]
137	user@router# run show vrrp extensive
138	Interface: ge-2/0/1.0, Interface index :0, Groups: 1, Active :0
139	 
140	Physical interface: ge-2/0/1, Unit: 0, Address: 1.1.1.1/24
141	  Index: 69, SNMP ifIndex: 131, VRRP-Traps: disabled
142	  Interface state: down, Group: 1, State: init
143	  Priority: 200, Advertisement interval: 1, Authentication type: none   <---| priority is unaltered
144	  Delay threshold: 100, Computed send rate: 0
145	  Preempt: yes, Accept-data mode: no, VIP count: 1, VIP: 1.1.1.254         
146	  Master router: N/A
147	  Tracking: disabled
148	 
149	|---> Configuration pre-JUNOS 9.2
150	 
151	version 1.0;
152	ns junos = "http://xml.juniper.net/junos/*/junos";
153	ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
154	ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
155	import "../import/junos.xsl";
156	match configuration {
157	if (//@junos:group == "DRAIN-ME") {
158	    for-each (interfaces/interface[starts-with(name, 'ge-') or starts-with(name, 'xe-') or starts-with(name, 'fe-') or starts-with(name, 'ae') and not(gigether-options/ieee-802.3ad)]){
159	        call jcs:emit-change($tag = 'transient-change') {
160	        with $content = {
161	                        <unit> {
162	                           <name> unit/name;
163	                              <family> {
164	                                 <inet> {
165	                                    <address> {
166	                                       <name> unit/family/inet/address/name;
167	                                          <vrrp-group> {
168	                                             <name> unit/family/inet/address/vrrp-group/name;
169	                                                <priority> "1";
170	                                                }
171	                                             }
172	                                          }
173	                                       }
174	                                    }
175	                                 }
176	                              }
177	                           }
178	                        }
179	                     }
180	 
181	|---> JUNOS 9.2 and above
182	 
183	version 1.0;
184	ns junos = "http://xml.juniper.net/junos/*/junos";
185	ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
186	ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
187	import "../import/junos.xsl";
188	var $get-configuration={
189	    <get-configuration database="committed">{
190	        <configuration> {
191	           <apply-groups>;
192	        }
193	    }
194	}
195	var $config = jcs:invoke($get-configuration);
196	match configuration {
197	if (contains($config, "DRAIN-ME")) {
198	    for-each (interfaces/interface[starts-with(name, 'ge-') or starts-with(name, 'xe-') or starts-with(name, 'fe-') or starts-with(n
199	ame, 'ae') and not(gigether-options/ieee-802.3ad)]){
200	        call jcs:emit-change($tag = 'transient-change') {
201	        with $content = {
202	                        <unit> {
203	                           <name> unit/name;
204	                              <family> {
205	                                 <inet> {
206	                                    <address> {
207	                                       <name> unit/family/inet/address/name;
208	                                          <vrrp-group> {
209	                                             <name> unit/family/inet/address/vrrp-group/name;
210	                                                <priority> "1";
211	                                                }
212	                                             }
213	                                          }
214	                                       }
215	                                    }
216	                                 }
217	                              }
218	                           }
219	                        }
220	                     }
221	 
222	 
223	user@router# show system scripts
224	commit {
225	    allow-transients;
226	    file drain-vrrp.slax;
227	}

SLAX Script Contents

 

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	 
07	/*
08	 * This commit script changes VRRP parameters if a named apply-group is also configured.
09	 * Ths only limitation of the pre-9.2 configuration is that there must be at least one
10	 * statement inherited from the DRAIN-ME group.
11	 */
12	 
13	/*
14	 * |--> Configuration pre-JUNOS 9.2
15	 *
16	 * match configuration {
17	 * if (//@junos:group == "DRAIN-ME") {
18	 *     for-each (interfaces/interface[starts-with(name, 'ge-') or starts-with(name, 'xe-') or starts-with(name, 'fe-') or starts-with(name, 'ae') and not(gigether-options/ieee-802.3as)]){
19	 *         call jcs:emit-change($tag = 'transient-change') {
20	 *         with $content = {
21	 *                         <unit> {
22	 *                             <name> unit/name;
23	 *                                 <family> {
24	 *                                     <inet> {
25	 *                                         <address> {
26	 *                                            <name> unit/family/inet/address/name;
27	 *                                                <vrrp-group> {
28	 *                                                    <name> unit/family/inet/address/vrrp-gropu/name;
29	 *                                                        <priority> "1";
30	 *                                                        }
31	 *                                                     }
32	 *                                                }
33	 *                                          }
34	 *                                     }
35	 *                               }
36	 *                          }
37	 *                      } 
38	 */
39	 
40	 
41	/*
42	 * |---> JUNOS 9.2 and above
43	 */
44	var $get-configuration={
45	    <get-configuration database="committed">{
46	        <configuration> {
47	           <apply-groups>;
48	        }
49	    }
50	}
51	var $config = jcs:invoke($get-configuration);
52	match configuration {
53	if (contains($config, "DRAIN-ME")) {
54	    for-each (interfaces/interface[starts-with(name, 'ge-') or starts-with(name, 'xe-') or starts-with(name, 'fe-') or starts-with(name, 'ae') and not(gigether-options/ieee-802.3ad)]) {
55	        call jcs:emit-change($tag = 'transient-change') {
56	        with $content = {
57	                        <unit> {
58	                           <name> unit/name;
59	                              <family> {
60	                                 <inet> {
61	                                    <address> {
62	                                       <name> unit/family/inet/address/name;
63	                                          <vrrp-group> {
64	                                             <name> unit/family/inet/address/vrrp-group/name;
65	                                                <priority> "1";
66	                                                }
67	                                             }
68	                                          }
69	                                       }
70	                                    }
71	                                 }
72	                              }
73	                           }
74	                        }
75	                     }

XML Script Contents

 

01	<?xml version="1.0"?>
02	<script>
03	  <title>drain-vrrp.slax</title>
04	  <author>matthews</author>
05	  <synopsis>
06	    This script changes VRRP parameters if a named apply-group is also configured
07	  </synopsis>
08	  <coe>commit</coe>
09	  <type>protocols</type>
10	 
11	  <description>
12	The only limitation of the pre-9.2 configuration is that there must be at least one statement inherited from the DRAIN-ME group.
13	 
14	  </description>
15	 
16	  <keyword>slax</keyword>
17	  <keyword>vrrp</keyword>
18	  <example>
19	    <title>Config</title>
20	    <config>example-1.conf</config>
21	  </example>
22	 
23	  <xhtml:script xmlns:xhtml="http://www.w3.org/1999/xhtml"
24	                src="../../../../../web/leaf.js"
25	            type="text/javascript"/>
26	</script>

 


#Slax
#ScriptingHow-To
#commitscript
#protocols
#How-To
#vrrp

Permalink