Debian Codegraph @

ansible_collections/cisco/mso/plugins/modules/ndo_l3out_node_routing_policy.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2024, Sabari Jaganathan (@sajagana) <[email protected]>
5
6 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
7
8 from __future__ import absolute_import, division, print_function
9
10 __metaclass__ = type
11
12 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"}
13
14 DOCUMENTATION = r"""
15 ---
16 module: ndo_l3out_node_routing_policy
17 short_description: Manage L3Out Node Routing Policies on Cisco Nexus Dashboard Orchestrator (NDO).
18 description:
19 - Manage L3Out Node Routing Policies on Cisco Nexus Dashboard Orchestrator (NDO).
20 - This module is only supported on ND v3.1 (NDO v4.3) and later.
21 author:
22 - Sabari Jaganathan (@sajagana)
23 options:
24 template:
25 description:
26 - The name of the template.
27 - The template must be a Tenant Policy Template.
28 type: str
29 required: true
30 name:
31 description:
32 - The name of the L3Out Node Routing Policy.
33 type: str
34 aliases: [ l3out_node_routing_policy_name ]
35 uuid:
36 description:
37 - The UUID of the L3Out Node Routing Policy.
38 - This parameter is required when the O(name) needs to be updated.
39 type: str
40 aliases: [ l3out_node_routing_policy_uuid ]
41 description:
42 description:
43 - The description of the L3Out Node Routing Policy.
44 type: str
45 bfd_multi_hop_settings:
46 description:
47 - The BFD MultiHop Settings configuration of the L3Out Node Routing Policy.
48 type: dict
49 suboptions:
50 state:
51 description:
52 - Use C(enabled) to configure the BFD MultiHop Settings.
53 - Use C(disabled) to remove the BFD MultiHop Settings.
54 type: str
55 choices: [ enabled, disabled ]
56 admin_state:
57 description:
58 - The administrative state of the BFD MultiHop Settings.
59 - Defaults to enabled when unset during creation.
60 type: str
61 choices: [ enabled, disabled ]
62 detection_multiplier:
63 description:
64 - The detection multiplier of the BFD MultiHop Settings.
65 - Defaults to 3 when unset during creation.
66 - The value must be between 1 and 50.
67 type: int
68 min_receive_interval:
69 description:
70 - The minimum receive interval of the BFD MultiHop Settings.
71 - Defaults to 250 when unset during creation.
72 - The value must be between 250 and 999 microseconds.
73 type: int
74 min_transmit_interval:
75 description:
76 - The minimum transmit interval of the BFD MultiHop Settings.
77 - Defaults to 250 when unset during creation.
78 - The value must be between 250 and 999 microseconds.
79 type: int
80 bgp_node_settings:
81 description:
82 - The BGP Node Settings configuration of the L3Out Node Routing Policy.
83 type: dict
84 suboptions:
85 state:
86 description:
87 - Use C(enabled) to configure the BGP Node Settings.
88 - Use C(disabled) to remove the BGP Node Settings.
89 type: str
90 choices: [ enabled, disabled ]
91 graceful_restart_helper:
92 description:
93 - The graceful restart helper of the BGP Node Settings.
94 - Defaults to enabled when unset during creation.
95 type: str
96 choices: [ enabled, disabled ]
97 keep_alive_interval:
98 description:
99 - The keep alive interval of the BGP Node Settings.
100 - Defaults to 60 when unset during creation.
101 - The value must be between 0 and 3600 seconds.
102 type: int
103 hold_interval:
104 description:
105 - The hold interval of the BGP Node Settings.
106 - Defaults to 180 when unset during creation.
107 - The value must be 0 or between 3 and 3600 seconds.
108 type: int
109 stale_interval:
110 description:
111 - The stale interval of the BGP Node Settings.
112 - Defaults to 300 when unset during creation.
113 - The value must be between 1 and 3600 seconds.
114 type: int
115 max_as_limit:
116 description:
117 - The max as limit of the BGP Node Settings.
118 - Defaults to 0 when unset during creation.
119 - The value must be between 0 and 2000.
120 type: int
121 as_path_multipath_relax:
122 description:
123 - The BGP Best Path Control of the L3Out Node Routing Policy.
124 - Providing an empty string will remove the O(as_path_multipath_relax="") from the L3Out Node Routing Policy.
125 type: str
126 choices: [ enabled, disabled, "" ]
127 state:
128 description:
129 - Use C(absent) for removing.
130 - Use C(query) for listing an object or multiple objects.
131 - Use C(present) for creating or updating.
132 type: str
133 choices: [ absent, query, present ]
134 default: query
135 notes:
136 - The O(template) must exist before using this module in your playbook.
137 Use M(cisco.mso.ndo_template) to create the Tenant Policy Template.
138 seealso:
139 - module: cisco.mso.ndo_template
140 extends_documentation_fragment: cisco.mso.modules
141 """
142
143 EXAMPLES = r"""
144 - name: Create a new L3Out Node Routing Policy with empty bgp_node and bfd_multi_hop settings
145 cisco.mso.ndo_l3out_node_routing_policy:
146 host: mso_host
147 username: admin
148 password: SomeSecretPassword
149 template: ansible_tenant_template
150 name: nrp_1
151 bfd_multi_hop_settings:
152 state: enabled
153 bgp_node_settings:
154 state: enabled
155 as_path_multipath_relax: disabled
156 state: present
157
158 - name: Create a new L3Out Node Routing Policy with full config of bgp_node and bfd_multi_hop settings
159 cisco.mso.ndo_l3out_node_routing_policy:
160 host: mso_host
161 username: admin
162 password: SomeSecretPassword
163 template: ansible_tenant_template
164 name: nrp_2
165 bfd_multi_hop_settings:
166 admin_state: enabled
167 detection_multiplier: 10
168 min_receive_interval: 450
169 min_transmit_interval: 550
170 bgp_node_settings:
171 graceful_restart_helper: enabled
172 keep_alive_interval: 15
173 hold_interval: 115
174 stale_interval: 215
175 max_as_limit: 25
176 as_path_multipath_relax: disabled
177 state: present
178 register: nrp_2
179
180 - name: Update L3Out Node Routing Policy name and bgp_node_settings attributes
181 cisco.mso.ndo_l3out_node_routing_policy:
182 host: mso_host
183 username: admin
184 password: SomeSecretPassword
185 template: ansible_tenant_template
186 uuid: "{{ nrp_2.current.uuid }}"
187 name: nrp_2_updated
188 bgp_node_settings:
189 graceful_restart_helper: disabled
190 keep_alive_interval: 20
191 state: present
192
193 - name: Query a L3Out Node Routing Policies with name
194 cisco.mso.ndo_l3out_node_routing_policy:
195 host: mso_host
196 username: admin
197 password: SomeSecretPassword
198 template: ansible_tenant_template
199 name: nrp_1
200 state: query
201 register: query_with_name
202
203 - name: Query a L3Out Node Routing Policies with UUID
204 cisco.mso.ndo_l3out_node_routing_policy:
205 host: mso_host
206 username: admin
207 password: SomeSecretPassword
208 template: ansible_tenant_template
209 uuid: "{{ query_with_name.current.uuid }}"
210 state: query
211 register: query_with_uuid
212
213 - name: Query all L3Out Node Routing Policies
214 cisco.mso.ndo_l3out_node_routing_policy:
215 host: mso_host
216 username: admin
217 password: SomeSecretPassword
218 template: ansible_tenant_template
219 state: query
220 register: query_all
221
222 - name: Delete a L3Out Node Routing Policy
223 cisco.mso.ndo_l3out_node_routing_policy:
224 host: mso_host
225 username: admin
226 password: SomeSecretPassword
227 template: ansible_tenant_template
228 name: nrp_1
229 state: absent
230
231 - name: Delete a L3Out Node Routing Policy using UUID
232 cisco.mso.ndo_l3out_node_routing_policy:
233 host: mso_host
234 username: admin
235 password: SomeSecretPassword
236 template: ansible_tenant_template
237 uuid: "{{ query_with_name.current.uuid }}"
238 state: absent
239 """
240
241 RETURN = r"""
242 """
243
244
245 import copy
246 from ansible.module_utils.basic import AnsibleModule
247 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec, ndo_bfd_multi_hop_settings_spec
248 from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate
249
250
251 def main():
252 argument_spec = mso_argument_spec()
253 argument_spec.update(
254 template=dict(type="str", required=True),
255 name=dict(type="str", aliases=["l3out_node_routing_policy_name"]),
256 uuid=dict(type="str", aliases=["l3out_node_routing_policy_uuid"]),
257 description=dict(type="str"),
258 bfd_multi_hop_settings=ndo_bfd_multi_hop_settings_spec(),
259 bgp_node_settings=dict(
260 type="dict",
261 options=dict(
262 state=dict(type="str", choices=["enabled", "disabled"]),
263 graceful_restart_helper=dict(type="str", choices=["enabled", "disabled"]),
264 keep_alive_interval=dict(type="int"), # sec
265 hold_interval=dict(type="int"), # sec
266 stale_interval=dict(type="int"), # sec
267 max_as_limit=dict(type="int"),
268 ),
269 ),
270 as_path_multipath_relax=(dict(type="str", choices=["enabled", "disabled", ""])),
271 state=dict(type="str", default="query", choices=["absent", "query", "present"]),
272 )
273
274 module = AnsibleModule(
275 argument_spec=argument_spec,
276 supports_check_mode=True,
277 required_if=[
278 ["state", "absent", ["name", "uuid"], True],
279 ["state", "present", ["name", "uuid"], True],
280 ],
281 )
282
283 mso = MSOModule(module)
284
285 template = module.params.get("template")
286 name = module.params.get("name")
287 uuid = module.params.get("uuid")
288 description = module.params.get("description")
289 bfd_multi_hop_settings = module.params.get("bfd_multi_hop_settings")
290 bgp_node_settings = module.params.get("bgp_node_settings")
291 as_path_multipath_relax = module.params.get("as_path_multipath_relax")
292 state = module.params.get("state")
293
294 mso_template = MSOTemplate(mso, "tenant", template)
295 mso_template.validate_template("tenantPolicy")
296
297 path = "/tenantPolicyTemplate/template/l3OutNodePolGroups"
298
299 l3out_node_routing_policy = mso_template.get_l3out_node_routing_policy_object(uuid, name)
300
301 if uuid or name:
302 if l3out_node_routing_policy:
303 mso.existing = mso.previous = copy.deepcopy(l3out_node_routing_policy.details) # Query a specific object
304 elif l3out_node_routing_policy:
305 mso.existing = l3out_node_routing_policy # Query all objects
306
307 ops = []
308 if state == "present":
309 if mso.existing:
310 proposed_payload = copy.deepcopy(mso.existing)
311 update_path = "{0}/{1}".format(path, l3out_node_routing_policy.index)
312
313 if name and proposed_payload.get("name") != name:
314 ops.append(dict(op="replace", path="{0}/name".format(update_path), value=name))
315 proposed_payload["name"] = name
316
317 if description is not None and proposed_payload.get("description") != description:
318 ops.append(dict(op="replace", path="{0}/description".format(update_path), value=description))
319 proposed_payload["description"] = description
320
321 if as_path_multipath_relax is not None and as_path_multipath_relax != "":
322 if not proposed_payload.get("asPathPol"):
323 proposed_payload["asPathPol"] = dict()
324 ops.append(dict(op="replace", path="{0}/asPathPol".format(update_path), value=dict()))
325
326 if proposed_payload.get("asPathPol").get("asPathMultipathRelax") is not True if as_path_multipath_relax == "enabled" else False:
327 proposed_payload["asPathPol"]["asPathMultipathRelax"] = True if as_path_multipath_relax == "enabled" else False
328 ops.append(
329 dict(
330 op="replace",
331 path="{0}/asPathPol/asPathMultipathRelax".format(update_path),
332 value=True if as_path_multipath_relax == "enabled" else False,
333 )
334 )
335 elif as_path_multipath_relax == "":
336 proposed_payload.pop("asPathPol", None)
337 ops.append(dict(op="remove", path="{0}/asPathPol".format(update_path)))
338
339 if bfd_multi_hop_settings is not None:
340 if bfd_multi_hop_settings.get("state") == "disabled" and proposed_payload.get("bfdMultiHopPol"):
341 proposed_payload.pop("bfdMultiHopPol", None)
342 ops.append(dict(op="remove", path="{0}/bfdMultiHopPol".format(update_path)))
343
344 elif bfd_multi_hop_settings.get("state") != "disabled":
345 if not proposed_payload.get("bfdMultiHopPol"):
346 proposed_payload["bfdMultiHopPol"] = dict()
347 ops.append(dict(op="replace", path="{0}/bfdMultiHopPol".format(update_path), value=dict()))
348
349 if bfd_multi_hop_settings.get("admin_state") is not None and proposed_payload.get("bfdMultiHopPol").get(
350 "adminState"
351 ) != bfd_multi_hop_settings.get("admin_state"):
352 proposed_payload["bfdMultiHopPol"]["adminState"] = bfd_multi_hop_settings.get("admin_state")
353 ops.append(
354 dict(
355 op="replace",
356 path="{0}/bfdMultiHopPol/adminState".format(update_path),
357 value=bfd_multi_hop_settings.get("admin_state"),
358 )
359 )
360
361 if bfd_multi_hop_settings.get("detection_multiplier") is not None and proposed_payload.get("bfdMultiHopPol").get(
362 "detectionMultiplier"
363 ) != bfd_multi_hop_settings.get("detection_multiplier"):
364 proposed_payload["bfdMultiHopPol"]["detectionMultiplier"] = bfd_multi_hop_settings.get("detection_multiplier")
365 ops.append(
366 dict(
367 op="replace",
368 path="{0}/bfdMultiHopPol/detectionMultiplier".format(update_path),
369 value=bfd_multi_hop_settings.get("detection_multiplier"),
370 )
371 )
372
373 if bfd_multi_hop_settings.get("min_receive_interval") is not None and proposed_payload.get("bfdMultiHopPol").get(
374 "minRxInterval"
375 ) != bfd_multi_hop_settings.get("min_receive_interval"):
376 proposed_payload["bfdMultiHopPol"]["minRxInterval"] = bfd_multi_hop_settings.get("min_receive_interval")
377 ops.append(
378 dict(
379 op="replace",
380 path="{0}/bfdMultiHopPol/minRxInterval".format(update_path),
381 value=bfd_multi_hop_settings.get("min_receive_interval"),
382 )
383 )
384
385 if bfd_multi_hop_settings.get("min_transmit_interval") is not None and proposed_payload.get("bfdMultiHopPol").get(
386 "minTxInterval"
387 ) != bfd_multi_hop_settings.get("min_transmit_interval"):
388 proposed_payload["bfdMultiHopPol"]["minTxInterval"] = bfd_multi_hop_settings.get("min_transmit_interval")
389 ops.append(
390 dict(
391 op="replace",
392 path="{0}/bfdMultiHopPol/minTxInterval".format(update_path),
393 value=bfd_multi_hop_settings.get("min_transmit_interval"),
394 )
395 )
396
397 if bgp_node_settings is not None:
398 if bgp_node_settings.get("state") == "disabled" and proposed_payload.get("bgpTimerPol"):
399 proposed_payload.pop("bgpTimerPol", None)
400 ops.append(dict(op="remove", path="{0}/bgpTimerPol".format(update_path)))
401
402 elif bgp_node_settings.get("state") != "disabled":
403 if not proposed_payload.get("bgpTimerPol"):
404 proposed_payload["bgpTimerPol"] = dict()
405 ops.append(dict(op="replace", path="{0}/bgpTimerPol".format(update_path), value=dict()))
406
407 if bgp_node_settings.get("graceful_restart_helper") is not None and (
408 proposed_payload.get("bgpTimerPol").get("gracefulRestartHelper") is not True
409 if bgp_node_settings.get("graceful_restart_helper") == "enabled"
410 else False
411 ):
412 proposed_payload["bgpTimerPol"]["gracefulRestartHelper"] = (
413 True if bgp_node_settings.get("graceful_restart_helper") == "enabled" else False
414 )
415 ops.append(
416 dict(
417 op="replace",
418 path="{0}/bgpTimerPol/gracefulRestartHelper".format(update_path),
419 value=True if bgp_node_settings.get("graceful_restart_helper") == "enabled" else False,
420 )
421 )
422
423 if bgp_node_settings.get("keep_alive_interval") is not None and proposed_payload.get("bgpTimerPol").get(
424 "keepAliveInterval"
425 ) != bgp_node_settings.get("keep_alive_interval"):
426 proposed_payload["bgpTimerPol"]["keepAliveInterval"] = bgp_node_settings.get("keep_alive_interval")
427 ops.append(
428 dict(
429 op="replace",
430 path="{0}/bgpTimerPol/keepAliveInterval".format(update_path),
431 value=bgp_node_settings.get("keep_alive_interval"),
432 )
433 )
434
435 if bgp_node_settings.get("hold_interval") is not None and proposed_payload.get("bgpTimerPol").get("holdInterval") != bgp_node_settings.get(
436 "hold_interval"
437 ):
438 proposed_payload["bgpTimerPol"]["holdInterval"] = bgp_node_settings.get("hold_interval")
439 ops.append(
440 dict(
441 op="replace",
442 path="{0}/bgpTimerPol/holdInterval".format(update_path),
443 value=bgp_node_settings.get("hold_interval"),
444 )
445 )
446
447 if bgp_node_settings.get("stale_interval") is not None and proposed_payload.get("bgpTimerPol").get(
448 "staleInterval"
449 ) != bgp_node_settings.get("stale_interval"):
450 proposed_payload["bgpTimerPol"]["staleInterval"] = bgp_node_settings.get("stale_interval")
451 ops.append(
452 dict(
453 op="replace",
454 path="{0}/bgpTimerPol/staleInterval".format(update_path),
455 value=bgp_node_settings.get("stale_interval"),
456 )
457 )
458
459 if bgp_node_settings.get("max_as_limit") is not None and proposed_payload.get("bgpTimerPol").get("maxAslimit") != bgp_node_settings.get(
460 "max_as_limit"
461 ):
462 proposed_payload["bgpTimerPol"]["maxAslimit"] = bgp_node_settings.get("max_as_limit")
463 ops.append(
464 dict(
465 op="replace",
466 path="{0}/bgpTimerPol/maxAslimit".format(update_path),
467 value=bgp_node_settings.get("max_as_limit"),
468 )
469 )
470
471 mso.sanitize(proposed_payload)
472 else:
473 if not (bfd_multi_hop_settings or bgp_node_settings or as_path_multipath_relax):
474 mso.fail_json(
475 msg="At least one of the following attributes must be specified when creating L3Out Node Routing Policy:"
476 + " 'bfd_multi_hop_settings', 'bgp_node_settings', or 'as_path_multipath_relax'."
477 )
478
479 payload = dict(name=name)
480 if description:
481 payload["description"] = description
482
483 if as_path_multipath_relax: # initially ignores None and "" (empty string)
484 # enabled = true
485 # disabled = false
486 # "" = remove the "BGP Best Path Control" settings from the object
487 payload["asPathPol"] = dict(asPathMultipathRelax=True if as_path_multipath_relax == "enabled" else False)
488
489 if bfd_multi_hop_settings is not None:
490 bfd_multi_hop_pol = dict()
491 if bfd_multi_hop_settings.get("admin_state"):
492 bfd_multi_hop_pol["adminState"] = bfd_multi_hop_settings.get("admin_state")
493
494 if bfd_multi_hop_settings.get("detection_multiplier"):
495 bfd_multi_hop_pol["detectionMultiplier"] = bfd_multi_hop_settings.get("detection_multiplier")
496
497 if bfd_multi_hop_settings.get("min_receive_interval"):
498 bfd_multi_hop_pol["minRxInterval"] = bfd_multi_hop_settings.get("min_receive_interval")
499
500 if bfd_multi_hop_settings.get("min_transmit_interval"):
501 bfd_multi_hop_pol["minTxInterval"] = bfd_multi_hop_settings.get("min_transmit_interval")
502
503 if bfd_multi_hop_pol or bfd_multi_hop_settings.get("state") == "enabled":
504 payload["bfdMultiHopPol"] = bfd_multi_hop_pol
505
506 if bgp_node_settings is not None:
507 bgp_timer_pol = dict()
508 if bgp_node_settings.get("graceful_restart_helper"):
509 bgp_timer_pol["gracefulRestartHelper"] = True if bgp_node_settings.get("graceful_restart_helper") == "enabled" else False
510
511 if bgp_node_settings.get("keep_alive_interval"):
512 bgp_timer_pol["keepAliveInterval"] = bgp_node_settings.get("keep_alive_interval")
513
514 if bgp_node_settings.get("hold_interval"):
515 bgp_timer_pol["holdInterval"] = bgp_node_settings.get("hold_interval")
516
517 if bgp_node_settings.get("stale_interval"):
518 bgp_timer_pol["staleInterval"] = bgp_node_settings.get("stale_interval")
519
520 if bgp_node_settings.get("max_as_limit"):
521 bgp_timer_pol["maxAslimit"] = bgp_node_settings.get("max_as_limit")
522
523 if bgp_timer_pol or bgp_node_settings.get("state") == "enabled":
524 payload["bgpTimerPol"] = bgp_timer_pol
525
526 ops.append(dict(op="add", path="{0}/-".format(path), value=copy.deepcopy(payload)))
527
528 mso.sanitize(payload)
529 elif state == "absent":
530 if mso.existing:
531 ops.append(dict(op="remove", path="{0}/{1}".format(path, l3out_node_routing_policy.index)))
532
533 if not module.check_mode and ops:
534 mso_template.template = mso.request(mso_template.template_path, method="PATCH", data=ops)
535 l3out_node_routing_policy = mso_template.get_l3out_node_routing_policy_object(uuid, name)
536 if l3out_node_routing_policy:
537 mso.existing = l3out_node_routing_policy.details # When the state is present
538 else:
539 mso.existing = {} # When the state is absent
540 elif module.check_mode and state != "query": # When the state is present/absent with check mode
541 mso.existing = mso.proposed if state == "present" else {}
542
543 mso.exit_json()
544
545
546 if __name__ == "__main__":
547 main()