Debian Codegraph @

ansible_collections/dellemc/enterprise_sonic/plugins/modules/sonic_static_routes.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # Copyright 2025 Dell Inc. or its subsidiaries. All Rights Reserved.
4 # GNU General Public License v3.0+
5 # (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 """
8 The module file for sonic_static_routes
9 """
10
11 from __future__ import absolute_import, division, print_function
12 __metaclass__ = type
13
14 DOCUMENTATION = """
15 ---
16 module: sonic_static_routes
17 version_added: 2.0.0
18 notes:
19 - Supports C(check_mode).
20 short_description: Manage static routes configuration on SONiC
21 description:
22 - This module provides configuration management of static routes for devices running SONiC
23 author: "S. Talabi (@stalabi1)"
24 options:
25 config:
26 type: list
27 elements: dict
28 description:
29 - Manages 'static_routes' configurations
30 suboptions:
31 vrf_name:
32 required: True
33 type: str
34 description:
35 - Name of the configured VRF on the device.
36 static_list:
37 type: list
38 elements: dict
39 description:
40 - A list of 'static_routes' configurations.
41 suboptions:
42 prefix:
43 required: True
44 type: str
45 description:
46 - Destination prefix for the static route, either IPv4 or IPv6.
47 next_hops:
48 type: list
49 elements: dict
50 description:
51 - A list of next-hops to be utilised for the static route being specified.
52 suboptions:
53 index:
54 required: True
55 type: dict
56 description:
57 - An identifier utilised to uniquely reference the next-hop.
58 suboptions:
59 blackhole:
60 type: bool
61 default: False
62 description:
63 - Indicates that packets matching this route should be discarded.
64 interface:
65 type: str
66 description:
67 - The reference to a base interface.
68 nexthop_vrf:
69 type: str
70 description:
71 - Name of the next-hop network instance for leaked routes.
72 next_hop:
73 type: str
74 description:
75 - The next-hop that is to be used for the static route.
76 metric:
77 type: int
78 description:
79 - Specifies the preference of the next-hop entry when it is injected into the RIB.
80 track:
81 type: int
82 description:
83 - The IP SLA track ID for static route.
84 tag:
85 type: int
86 description:
87 - The tag value for the static route.
88 state:
89 description:
90 - The state of the configuration after module completion.
91 type: str
92 choices:
93 - merged
94 - deleted
95 - overridden
96 - replaced
97 default: merged
98 """
99
100 EXAMPLES = """
101
102 # Using "merged" state
103 #
104 # Before state:
105 # -------------
106 #
107 # sonic# show running-configuration | grep "ip route"
108 # (No "ip route" configuration present)
109
110 - name: Merge static routes configurations
111 dellemc.enterprise_sonic.sonic_static_routes:
112 config:
113 - vrf_name: 'default'
114 static_list:
115 - prefix: '2.0.0.0/8'
116 next_hops:
117 - index:
118 interface: 'Ethernet4'
119 metric: 1
120 tag: 2
121 track: 3
122 - index:
123 next_hop: '3.0.0.0'
124 metric: 2
125 tag: 4
126 track: 8
127 - vrf_name: 'VrfReg1'
128 static_list:
129 - prefix: '3.0.0.0/8'
130 next_hops:
131 - index:
132 interface: 'eth0'
133 nexthop_vrf: 'VrfReg2'
134 next_hop: '4.0.0.0'
135 metric: 4
136 tag: 5
137 track: 6
138 - index:
139 blackhole: true
140 metric: 10
141 tag: 20
142 track: 30
143 state: merged
144
145 # After state:
146 # ------------
147 #
148 # sonic# show running-configuration | grep "ip route"
149 # ip route 2.0.0.0/8 3.0.0.0 tag 4 track 8 2
150 # ip route 2.0.0.0/8 interface Ethernet4 tag 2 track 3 1
151 # ip route vrf VrfReg1 3.0.0.0/8 4.0.0.0 interface Management 0 nexthop-vrf VrfReg2 tag 5 track 6 4
152 # ip route vrf VrfReg1 3.0.0.0/8 blackhole tag 20 track 30 10
153 #
154 #
155 # Modifying previous merge
156
157 - name: Modify static routes configurations
158 dellemc.enterprise_sonic.sonic_static_routes:
159 config:
160 - vrf_name: 'VrfReg1'
161 static_list:
162 - prefix: '3.0.0.0/8'
163 next_hops:
164 - index:
165 blackhole: true
166 metric: 11
167 tag: 22
168 track: 33
169 state: merged
170
171 # After state:
172 # ------------
173 #
174 # sonic# show running-configuration | grep "ip route"
175 # ip route 2.0.0.0/8 3.0.0.0 tag 4 track 8 2
176 # ip route 2.0.0.0/8 interface Ethernet4 tag 2 track 3 1
177 # ip route vrf VrfReg1 3.0.0.0/8 4.0.0.0 interface Management 0 nexthop-vrf VrfReg2 tag 5 track 6 4
178 # ip route vrf VrfReg1 3.0.0.0/8 blackhole tag 22 track 33 11
179
180
181 # Using "overridden" state
182 #
183 # Before state:
184 # -------------
185 #
186 # sonic# show running-configuration | grep "ip route"
187 # ip route 4.0.0.0/8 2.0.0.0 tag 4 track 8 2
188
189 - name: Override static routes configurations
190 dellemc.enterprise_sonic.sonic_static_routes:
191 config:
192 - vrf_name: 'VrfReg2'
193 static_list:
194 - prefix: '3.0.0.0/8'
195 next_hops:
196 - index:
197 blackhole: true
198 metric: 10
199 tag: 20
200 track: 30
201 state: overridden
202
203 # After state:
204 # ------------
205 #
206 # sonic# show running-configuration | grep "ip route"
207 # ip route vrf VrfReg2 3.0.0.0/8 blackhole tag 20 track 30 10
208
209
210 # Using "replaced" state
211 #
212 # Before state:
213 # -------------
214 #
215 # sonic# show running-configuration | grep "ip route"
216 # ip route 4.0.0.0/8 2.0.0.0 tag 4 track 8 2
217
218 - name: Replace static routes configurations
219 dellemc.enterprise_sonic.sonic_static_routes:
220 config:
221 - vrf_name: 'default'
222 static_list:
223 - prefix: '4.0.0.0/8'
224 next_hops:
225 - index:
226 blackhole: true
227 metric: 5
228 tag: 10
229 track: 15
230 state: replaced
231
232 # After state:
233 # ------------
234 #
235 # sonic# show running-configuration | grep "ip route"
236 # ip route 4.0.0.0/8 blackhole tag 10 track 15 5
237
238
239 # Using "deleted" state
240 #
241 # Before state:
242 # -------------
243 #
244 # sonic# show running-configuration | grep "ip route"
245 # ip route 2.0.0.0/8 3.0.0.0 tag 4 track 8 2
246 # ip route 2.0.0.0/8 interface Ethernet4 tag 2 track 3 1
247 # ip route vrf VrfReg1 3.0.0.0/8 4.0.0.0 interface Management 0 nexthop-vrf VrfReg2 tag 5 track 6 4
248 # ip route vrf VrfReg1 3.0.0.0/8 blackhole tag 22 track 33 11
249
250 - name: Delete static routes configurations
251 dellemc.enterprise_sonic.sonic_static_routes:
252 config:
253 - vrf_name: 'default'
254 static_list:
255 - prefix: '2.0.0.0/8'
256 next_hops:
257 - index:
258 interface: 'Ethernet4'
259 - vrf_name: 'VrfReg1'
260 state: deleted
261
262 # After state:
263 # ------------
264 #
265 # sonic# show running-configuration | grep "ip route"
266 # ip route 2.0.0.0/8 3.0.0.0 tag 4 track 8 2
267 """
268
269 RETURN = """
270 before:
271 description: The configuration prior to the module invocation.
272 returned: always
273 type: list
274 sample: >
275 The configuration returned will always be in the same format
276 as the parameters above.
277 after:
278 description: The resulting configuration module invocation.
279 returned: when changed
280 type: list
281 sample: >
282 The configuration returned will always be in the same format
283 as the parameters above.
284 after_generated:
285 description: The generated configuration module invocation.
286 returned: when C(check_mode)
287 type: list
288 sample: >
289 The configuration returned will always be in the same format
290 as the parameters above.
291 commands:
292 description: The set of commands pushed to the remote device.
293 returned: always
294 type: list
295 sample: ['command 1', 'command 2', 'command 3']
296 """
297
298
299 from ansible.module_utils.basic import AnsibleModule
300 from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.argspec.static_routes.static_routes import Static_routesArgs
301 from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.config.static_routes.static_routes import Static_routes
302
303
304 def main():
305 """
306 Main entry point for module execution
307
308 :returns: the result form module invocation
309 """
310 module = AnsibleModule(argument_spec=Static_routesArgs.argument_spec,
311 supports_check_mode=True)
312
313 result = Static_routes(module).execute_module()
314 module.exit_json(**result)
315
316
317 if __name__ == '__main__':
318 main()