Debian Codegraph @

ansible_collections/cisco/mso/plugins/modules/ndo_l3out_template.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_template
17 short_description: Manage L3Outs on Cisco Nexus Dashboard Orchestrator (NDO).
18 description:
19 - Manage L3Outs 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 L3Out template.
27 type: str
28 aliases: [ l3out_template ]
29 required: true
30 name:
31 description:
32 - The name of the L3Out.
33 type: str
34 uuid:
35 description:
36 - The UUID of the L3Out.
37 - This parameter is required when the L3Out O(name) needs to be updated.
38 type: str
39 description:
40 description:
41 - The description of the L3Out.
42 - Providing an empty string will remove the O(description="") from the L3Out.
43 type: str
44 vrf:
45 description:
46 - The VRF associated with the L3Out.
47 type: dict
48 suboptions:
49 name:
50 description:
51 - The name of the VRF.
52 required: true
53 type: str
54 schema:
55 description:
56 - The name of the schema.
57 required: true
58 type: str
59 template:
60 description:
61 - The name of the template.
62 required: true
63 type: str
64 l3_domain:
65 description:
66 - The name of the L3 Domain.
67 type: str
68 target_dscp:
69 description:
70 - The DSCP Level of the L3Out.
71 type: str
72 choices:
73 - af11
74 - af12
75 - af13
76 - af21
77 - af22
78 - af23
79 - af31
80 - af32
81 - af33
82 - af41
83 - af42
84 - af43
85 - cs0
86 - cs1
87 - cs2
88 - cs3
89 - cs4
90 - cs5
91 - cs6
92 - cs7
93 - expedited_forwarding
94 - unspecified
95 - voice_admit
96 pim:
97 description:
98 - The protocol independent multicast (PIM) flag of the L3Out.
99 - By default, PIM is disabled. To enable the PIM, Layer 3 Multicast must be enabled on the O(vrf).
100 type: bool
101 interleak:
102 description:
103 - The name of the Route Map Policy for Route Control that needs to be associated with Interleak route map.
104 - Providing an empty string will remove the O(interleak="") from the L3Out.
105 type: str
106 static_route_redistribution:
107 description:
108 - The name of the Route Map Policy for Route Control that needs to be associated with Static Route Redistribution route map.
109 - Providing an empty string will remove the O(static_route_redistribution="") from the L3Out.
110 type: str
111 aliases: [ static_route ]
112 connected_route_redistribution:
113 description:
114 - The name of the Route Map Policy for Route Control that needs to be associated with Connected Route Redistribution route map.
115 - Providing an empty string will remove the O(connected_route_redistribution="") from the L3Out.
116 type: str
117 aliases: [ connected_route ]
118 attached_host_route_redistribution:
119 description:
120 - The name of the Route Map Policy for Route Control that needs to be associated with Attached Host Route Redistribution route map.
121 - Providing an empty string will remove the O(attached_host_route_redistribution="") from the L3Out.
122 type: str
123 aliases: [ attached_host_route ]
124 bgp:
125 description:
126 - The BGP routing protocol configuration of the L3Out.
127 type: dict
128 suboptions:
129 state:
130 description:
131 - Use C(enabled) to configure the BGP routing protocol.
132 - Use C(disabled) to remove the BGP routing protocol.
133 type: str
134 default: enabled
135 choices: [enabled, disabled]
136 inbound_route_map:
137 description:
138 - The name of the Route Map Policy for Route Control that needs to be associated with inbound route map.
139 - Providing an empty string will remove the O(bgp.inbound_route_map="") from the L3Out.
140 type: str
141 aliases: [ import_route, inbound_route ]
142 outbound_route_map:
143 description:
144 - The name of the Route Map Policy for Route Control that needs to be associated with outbound route map.
145 - Providing an empty string will remove the O(bgp.outbound_route_map="") from the L3Out.
146 type: str
147 aliases: [ export_route, outbound_route ]
148 route_dampening_ipv4:
149 description:
150 - The name of the Route Map Policy for Route Control that needs to be associated with Route Dampening IPv4 route map.
151 - Providing an empty string will remove the O(bgp.route_dampening_ipv4="") from the L3Out.
152 type: str
153 aliases: [ dampening_ipv4 ]
154 route_dampening_ipv6:
155 description:
156 - The name of the Route Map Policy for Route Control that needs to be associated with Route Dampening IPv6 route map.
157 - Providing an empty string will remove the O(bgp.route_dampening_ipv6="") from the L3Out.
158 type: str
159 aliases: [ dampening_ipv6 ]
160 ospf:
161 description:
162 - The OSPF routing protocol configuration of the L3Out.
163 type: dict
164 suboptions:
165 state:
166 description:
167 - Use C(enabled) to configure the OSPF routing protocol.
168 - Use C(disabled) to remove the OSPF routing protocol.
169 type: str
170 default: enabled
171 choices: [enabled, disabled]
172 area_id:
173 description:
174 - The area id of the OSPF area.
175 - This option is required when the O(ospf.state=enabled).
176 type: str
177 area_type:
178 description:
179 - The area type of the OSPF area.
180 - This option is required when the O(ospf.state=enabled).
181 type: str
182 choices: [regular, stub, nssa]
183 cost:
184 description:
185 - The cost of the OSPF area.
186 - Defaults to 1 when unset during creation.
187 type: int
188 originate_summary_lsa:
189 description:
190 - This option is for OSPF NSSA (not-so-stubby area) or Stub area.
191 - When this option is disabled, not only Type 4 and 5, but also Type 3 LSAs are not sent into the NSSA or Stub area by the border leaf.
192 - Instead, the border leaf creates and sends a default route to the area.
193 - If there is no Type 3 LSA in this area in the first place, a default route is not created.
194 type: bool
195 aliases: [ originate_lsa ]
196 send_redistributed_lsas:
197 description:
198 - This option is for the OSPF NSSA (not-so-stubby area).
199 - When this option is disabled, the redistributed routes are not sent into this NSSA area from the border leaf.
200 - This is typically used when the O(ospf.originate_summary_lsa=false).
201 - Because disabling the O(ospf.originate_summary_lsa) option creates and sends a default route to the NSSA or Stub area.
202 type: bool
203 aliases: [ redistributed_lsas ]
204 suppress_forwarding_addr_translated_lsa:
205 description:
206 - This option is for OSPF NSSA (not-so-stubby area).
207 - When an OSPF NSSA ABR (Area Border Router) translates a Type-7 LSA into a Type-5 LSA to send it across non-NSSA areas.
208 - It typically includes the IP address of the originator ASBR (Autonomous System Boundary Router) as a forwarding address.
209 - However, if an OSPF router receiving the Type-5 LSA lacks a route to this forwarding address.
210 - The route may not be installed in the router's route table.
211 - Enabling this option prevents the ABR from adding a forwarding address during the Type-7 to Type-5 translation, thereby avoiding this issue.
212 type: bool
213 aliases: [ suppress_fa_lsa ]
214 originate_default_route:
215 description:
216 - The Originate Default Route option in an L3Out configuration allows the ACI fabric to advertise a default route (0.0.0.0/0) to external networks.
217 - Providing an empty string will remove the O(ospf.originate_default_route) from the L3Out.
218 type: str
219 choices: [ only, in_addition, "" ]
220 originate_default_route_always:
221 description:
222 - Enabling this option will set the O(ospf.originate_default_route=only) when the O(ospf.originate_default_route) unset during creation.
223 - This option is applicable only if OSPF is configured on the L3Out.
224 type: bool
225 aliases: [ always ]
226 state:
227 description:
228 - Use C(absent) for removing.
229 - Use C(query) for listing an object or multiple objects.
230 - Use C(present) for creating or updating.
231 type: str
232 choices: [ absent, query, present ]
233 default: query
234 notes:
235 - The O(template) must exist before using this module in your playbook.
236 Use M(cisco.mso.ndo_template) to create the L3Out template.
237 - The O(vrf) must exist before using this module in your playbook.
238 Use M(cisco.mso.mso_schema_template_vrf) to create the VRF.
239 - The O(l3_domain) must exist before using this module in your playbook.
240 Use M(cisco.mso.ndo_l3_domain) to create the L3Out domain.
241 seealso:
242 - module: cisco.mso.mso_schema_template_vrf
243 - module: cisco.mso.ndo_template
244 - module: cisco.mso.ndo_l3_domain
245 extends_documentation_fragment: cisco.mso.modules
246 """
247
248 EXAMPLES = r"""
249 - name: Create a new L3Out object without BGP and OSPF
250 cisco.mso.ndo_l3out_template:
251 host: mso_host
252 username: admin
253 password: SomeSecretPassword
254 l3out_template: l3out_template
255 name: "l3out_1"
256 vrf:
257 name: "VRF1"
258 schema: "Schema1"
259 template: "Template1"
260 state: "present"
261
262 - name: Query a L3Out object with name
263 cisco.mso.ndo_l3out_template:
264 host: mso_host
265 username: admin
266 password: SomeSecretPassword
267 l3out_template: l3out_template
268 name: "l3out_1"
269 state: "query"
270 register: query_l3out_name
271
272 - name: Update a L3Out object name with UUID
273 cisco.mso.ndo_l3out_template:
274 host: mso_host
275 username: admin
276 password: SomeSecretPassword
277 l3out_template: l3out_template
278 uuid: "{{ query_l3out_name.current.uuid }}"
279 name: "l3out_1_updated"
280 description: "updated description"
281 state: "present"
282
283 - name: Create a new L3Out object with BGP and OSPF
284 cisco.mso.ndo_l3out_template:
285 host: mso_host
286 username: admin
287 password: SomeSecretPassword
288 l3out_template: l3out_template
289 name: "l3out_1_new"
290 vrf:
291 name: "VRF1"
292 schema: "Schema1"
293 template: "Template1"
294 routing_protocols: ["bgp", "ospf"]
295 bgp:
296 inbound_route_map: "ans_route_map"
297 outbound_route_map: "ans_route_map"
298 route_dampening_ipv4: "ans_route_map"
299 route_dampening_ipv6: "ans_route_map"
300 ospf:
301 area_id: "0.0.0.1"
302 area_type: "regular"
303 cost: 1
304 send_redistributed_lsas: true
305 originate_summary_lsa: true
306 suppress_forwarding_addr_translated_lsa: true
307 originate_default_route: "only"
308 originate_default_route_always: false
309 interleak: "ans_route_map"
310 static_route_redistribution: "ans_route_map"
311 connected_route_redistribution: "ans_route_map"
312 attached_host_route_redistribution: "ans_route_map"
313 state: "present"
314
315 - name: Update a L3Out object BGP and OSPF values
316 cisco.mso.ndo_l3out_template:
317 host: mso_host
318 username: admin
319 password: SomeSecretPassword
320 l3out_template: l3out_template
321 name: "l3out_1_new"
322 vrf:
323 name: "VRF1"
324 schema: "Schema1"
325 template: "Template1"
326 routing_protocols: ["bgp", "ospf"]
327 bgp:
328 route_dampening_ipv4: "ans_route_map_2"
329 route_dampening_ipv6: "ans_route_map_2"
330 ospf:
331 area_id: "0.0.0.2"
332 area_type: "stub"
333 cost: 3
334 send_redistributed_lsas: true
335 originate_summary_lsa: true
336 suppress_forwarding_addr_translated_lsa: true
337 interleak: "ans_route_map"
338 static_route_redistribution: "ans_route_map"
339 connected_route_redistribution: "ans_route_map"
340 attached_host_route_redistribution: "ans_route_map"
341 state: "present"
342
343 - name: Clear bgp and ospf routing protocol from the existing L3Out
344 cisco.mso.ndo_l3out_template:
345 host: mso_host
346 username: admin
347 password: SomeSecretPassword
348 l3out_template: l3out_template
349 name: "l3out_1_new"
350 vrf:
351 name: "VRF1"
352 schema: "Schema1"
353 template: "Template1"
354 routing_protocol: ["bgp"]
355 bgp:
356 state: disabled
357 ospf:
358 state: disabled
359 state: "present"
360
361 - name: Query a L3Out object with uuid
362 cisco.mso.ndo_l3out_template:
363 host: mso_host
364 username: admin
365 password: SomeSecretPassword
366 l3out_template: l3out_template
367 uuid: "{{ query_l3out_name.current.uuid }}"
368 state: "query"
369 register: query_l3out_name
370
371 - name: Query all L3Out objects
372 cisco.mso.ndo_l3out_template:
373 host: mso_host
374 username: admin
375 password: SomeSecretPassword
376 l3out_template: l3out_template
377 state: "query"
378 register: query_all_l3out
379
380 - name: Delete a L3Out object with name
381 cisco.mso.ndo_l3out_template:
382 host: mso_host
383 username: admin
384 password: SomeSecretPassword
385 l3out_template: l3out_template
386 name: "l3out_1"
387 state: "absent"
388
389 - name: Delete a L3Out object with uuid
390 cisco.mso.ndo_l3out_template:
391 host: mso_host
392 username: admin
393 password: SomeSecretPassword
394 l3out_template: l3out_template
395 uuid: "{{ query_l3out_name.current.uuid }}"
396 state: "absent"
397 """
398
399 RETURN = r"""
400 """
401
402 import copy
403 from ansible.module_utils.basic import AnsibleModule
404 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec
405 from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate, KVPair
406 from ansible_collections.cisco.mso.plugins.module_utils.constants import TARGET_DSCP_MAP, ORIGINATE_DEFAULT_ROUTE, L3OUT_ROUTING_PROTOCOLS
407 from ansible_collections.cisco.mso.plugins.module_utils.utils import generate_api_endpoint
408
409
410 def get_routing_protocol(existing_protocol, ospf_state, bgp_state):
411 protocols = set()
412
413 if bgp_state == "enabled" or (bgp_state == "ignore" and "bgp" in L3OUT_ROUTING_PROTOCOLS.get(existing_protocol)):
414 protocols.add("bgp")
415
416 if ospf_state == "enabled" or (ospf_state == "ignore" and "ospf" in L3OUT_ROUTING_PROTOCOLS.get(existing_protocol)):
417 protocols.add("ospf")
418
419 return "".join(protocols) if len(protocols) < 2 else L3OUT_ROUTING_PROTOCOLS.get("".join(protocols))
420
421
422 def main():
423 argument_spec = mso_argument_spec()
424 argument_spec.update(
425 template=dict(type="str", required=True, aliases=["l3out_template"]),
426 name=dict(type="str"),
427 uuid=dict(type="str"),
428 description=dict(type="str"),
429 vrf=dict(
430 type="dict",
431 options=dict(
432 name=dict(type="str", required=True),
433 schema=dict(type="str", required=True),
434 template=dict(type="str", required=True),
435 ),
436 ),
437 l3_domain=dict(type="str"),
438 target_dscp=dict(type="str", choices=list(TARGET_DSCP_MAP)),
439 pim=dict(type="bool"),
440 interleak=dict(type="str"),
441 static_route_redistribution=dict(type="str", aliases=["static_route"]),
442 connected_route_redistribution=dict(type="str", aliases=["connected_route"]),
443 attached_host_route_redistribution=dict(type="str", aliases=["attached_host_route"]),
444 ospf=dict(
445 type="dict",
446 options=dict(
447 state=dict(type="str", choices=["enabled", "disabled"], default="enabled"),
448 area_id=dict(type="str"),
449 area_type=dict(type="str", choices=["regular", "stub", "nssa"]),
450 cost=dict(type="int"),
451 send_redistributed_lsas=dict(type="bool", aliases=["redistributed_lsas"]),
452 originate_summary_lsa=dict(type="bool", aliases=["originate_lsa"]),
453 suppress_forwarding_addr_translated_lsa=dict(type="bool", aliases=["suppress_fa_lsa"]),
454 originate_default_route=dict(type="str", choices=list(ORIGINATE_DEFAULT_ROUTE)),
455 originate_default_route_always=dict(type="bool", aliases=["always"]),
456 ),
457 required_if=[
458 ["state", "enabled", ["area_id", "area_type"]],
459 ],
460 ),
461 bgp=dict(
462 type="dict",
463 options=dict(
464 state=dict(type="str", choices=["enabled", "disabled"], default="enabled"),
465 inbound_route_map=dict(type="str", aliases=["import_route", "inbound_route"]),
466 outbound_route_map=dict(type="str", aliases=["export_route", "outbound_route"]),
467 route_dampening_ipv4=dict(type="str", aliases=["dampening_ipv4"]),
468 route_dampening_ipv6=dict(type="str", aliases=["dampening_ipv6"]),
469 ),
470 ),
471 state=dict(type="str", default="query", choices=["absent", "query", "present"]),
472 )
473
474 module = AnsibleModule(
475 argument_spec=argument_spec,
476 supports_check_mode=True,
477 required_if=[
478 ["state", "absent", ["name", "uuid"], True],
479 ["state", "present", ["name", "uuid"], True],
480 ],
481 )
482
483 mso = MSOModule(module)
484
485 l3out_template = module.params.get("l3out_template")
486 name = module.params.get("name")
487 uuid = module.params.get("uuid")
488 description = module.params.get("description")
489 l3_domain = module.params.get("l3_domain")
490 pim = module.params.get("pim")
491 interleak = module.params.get("interleak")
492 static_route_redistribution = module.params.get("static_route_redistribution")
493 connected_route_redistribution = module.params.get("connected_route_redistribution")
494 attached_host_route_redistribution = module.params.get("attached_host_route_redistribution")
495 target_dscp = TARGET_DSCP_MAP.get(module.params.get("target_dscp"))
496 vrf_dict = module.params.get("vrf") if module.params.get("vrf") else {}
497 ospf = module.params.get("ospf")
498 bgp = module.params.get("bgp")
499
500 state = module.params.get("state")
501
502 mso_template = MSOTemplate(mso, "l3out", l3out_template)
503 mso_template.validate_template("l3out")
504
505 tenant_id = mso_template.template_summary.get("tenantId")
506 tenant_name = mso_template.template_summary.get("tenantName")
507
508 match = mso_template.get_l3out_object(uuid, name)
509
510 if (uuid or name) and match:
511 mso.existing = mso.previous = copy.deepcopy(insert_l3out_relation_name(match.details, mso_template)) # Query a specific object
512 elif match:
513 mso.existing = [insert_l3out_relation_name(l3out, mso_template) for l3out in match] # Query all objects
514
515 if state != "query":
516 path = "/l3outTemplate/l3outs/{0}".format(match.index if match else "-")
517
518 ops = []
519
520 if state == "present":
521 if uuid and not mso.existing:
522 mso.fail_json(msg="L3Out with the uuid: '{0}' not found".format(uuid))
523
524 templates_objects_path = "templates/objects"
525 route_map_params = {"type": "routeMap", "tenant-id": tenant_id}
526 route_map_path = generate_api_endpoint(templates_objects_path, **route_map_params)
527 route_map_objects = mso.query_objs(route_map_path)
528
529 vrf_ref = None
530 if vrf_dict:
531 vrf_object = mso_template.get_vrf_object(vrf_dict, tenant_id, templates_objects_path)
532 if pim and vrf_object.details.get("l3MCast") is False:
533 mso.fail_json(
534 msg="Invalid configuration in L3Out {0}, 'PIM' cannot be enabled while using the VRF '{1}' with L3 Multicast disabled".format(
535 "UUID: {0}".format(uuid) if uuid else "Name: {0}".format(name), vrf_dict.get("name")
536 )
537 )
538 vrf_ref = vrf_object.details.get("uuid")
539
540 existing_routing_protocols = mso.existing.get("routingProtocol", None)
541
542 ospf_state = ospf.get("state") if ospf else "ignore"
543 bgp_state = bgp.get("state") if bgp else "ignore"
544
545 res_routing_protocols = get_routing_protocol(None if existing_routing_protocols == "none" else existing_routing_protocols, ospf_state, bgp_state)
546
547 routing_protocols = res_routing_protocols if res_routing_protocols else "none"
548
549 if not mso.existing:
550 payload = dict(name=name)
551
552 payload["routingProtocol"] = routing_protocols
553
554 if vrf_ref is None:
555 mso.fail_json(msg="The O(vrf) is required during the creation.")
556
557 payload["vrfRef"] = vrf_ref
558 payload["vrfName"] = vrf_dict.get("name")
559
560 if description:
561 payload["description"] = description
562
563 if l3_domain:
564 payload["l3domain"] = l3_domain
565
566 if target_dscp:
567 payload["targetDscp"] = target_dscp
568
569 if pim is not None:
570 payload["pim"] = pim
571
572 outer_route_maps = dict()
573 if interleak:
574 outer_route_maps["interleakName"] = interleak
575 outer_route_maps["interleakRef"] = mso_template.get_route_map(
576 "interleak",
577 tenant_id,
578 tenant_name,
579 interleak,
580 route_map_objects,
581 ).get("uuid", "")
582
583 if static_route_redistribution:
584 outer_route_maps["staticRouteRedistName"] = static_route_redistribution
585 outer_route_maps["staticRouteRedistRef"] = mso_template.get_route_map(
586 "static_route_redistribution",
587 tenant_id,
588 tenant_name,
589 static_route_redistribution,
590 route_map_objects,
591 ).get("uuid", "")
592
593 if connected_route_redistribution:
594 outer_route_maps["connectedRouteRedistName"] = connected_route_redistribution
595 outer_route_maps["connectedRouteRedistRef"] = mso_template.get_route_map(
596 "connected_route_redistribution",
597 tenant_id,
598 tenant_name,
599 connected_route_redistribution,
600 route_map_objects,
601 ).get("uuid", "")
602
603 if attached_host_route_redistribution:
604 outer_route_maps["attachedHostRouteRedistName"] = attached_host_route_redistribution
605 outer_route_maps["attachedHostRouteRedistRef"] = mso_template.get_route_map(
606 "attached_host_route_redistribution",
607 tenant_id,
608 tenant_name,
609 attached_host_route_redistribution,
610 route_map_objects,
611 ).get("uuid", "")
612
613 if outer_route_maps:
614 payload["advancedRouteMapRefs"] = outer_route_maps
615
616 if bgp:
617 if bgp.get("inbound_route_map"):
618 payload["importRouteMapName"] = bgp.get("inbound_route_map")
619 payload["importRouteMapRef"] = mso_template.get_route_map(
620 "inbound_route_map",
621 tenant_id,
622 tenant_name,
623 bgp.get("inbound_route_map"),
624 route_map_objects,
625 ).get("uuid", "")
626
627 payload["importRouteControl"] = True if payload.get("importRouteMapRef") else False
628
629 if bgp.get("outbound_route_map"):
630 payload["exportRouteMapName"] = bgp.get("outbound_route_map")
631 payload["exportRouteMapRef"] = mso_template.get_route_map(
632 "outbound_route_map",
633 tenant_id,
634 tenant_name,
635 bgp.get("outbound_route_map"),
636 route_map_objects,
637 ).get("uuid", "")
638
639 if bgp.get("route_dampening_ipv4"):
640 payload["advancedRouteMapRefs"]["routeDampeningV4Name"] = bgp.get("route_dampening_ipv4")
641 payload["advancedRouteMapRefs"]["routeDampeningV4Ref"] = mso_template.get_route_map(
642 "route_dampening_ipv4",
643 tenant_id,
644 tenant_name,
645 bgp.get("route_dampening_ipv4"),
646 route_map_objects,
647 ).get("uuid", "")
648
649 if bgp.get("route_dampening_ipv6"):
650 payload["advancedRouteMapRefs"]["routeDampeningV6Name"] = bgp.get("route_dampening_ipv6")
651 payload["advancedRouteMapRefs"]["routeDampeningV6Ref"] = mso_template.get_route_map(
652 "route_dampening_ipv6",
653 tenant_id,
654 tenant_name,
655 bgp.get("route_dampening_ipv6"),
656 route_map_objects,
657 ).get("uuid", "")
658
659 if ospf:
660 payload["ospfAreaConfig"] = dict(
661 cost=ospf.get("cost"),
662 id=ospf.get("area_id"),
663 areaType=ospf.get("area_type"),
664 )
665
666 default_route_leak = dict()
667 if ospf.get("originate_default_route"):
668 default_route_leak["originateDefaultRoute"] = ORIGINATE_DEFAULT_ROUTE.get(ospf.get("originate_default_route"))
669
670 if ospf.get("originate_default_route_always") is not None:
671 default_route_leak["always"] = ospf.get("originate_default_route_always")
672
673 if default_route_leak:
674 payload["defaultRouteLeak"] = default_route_leak
675
676 redistribute = ospf.get("send_redistributed_lsas")
677 originate = ospf.get("originate_summary_lsa")
678 suppress_fa = ospf.get("suppress_forwarding_addr_translated_lsa")
679
680 control = dict()
681 if redistribute is not None:
682 control["redistribute"] = redistribute
683
684 if originate is not None:
685 control["originate"] = originate
686
687 if suppress_fa is not None:
688 control["suppressFA"] = suppress_fa
689
690 if control:
691 payload["ospfAreaConfig"]["control"] = control
692
693 mso.sanitize(payload)
694 ops.append(dict(op="add", path=path, value=payload))
695 elif mso.existing:
696 proposed_payload = copy.deepcopy(match.details)
697
698 if name is not [None, ""] and mso.existing.get("name") != name:
699 ops.append(dict(op="replace", path=path + "/name", value=name))
700 proposed_payload["name"] = name
701
702 if mso.existing.get("routingProtocol") != routing_protocols:
703 ops.append(dict(op="replace", path=path + "/routingProtocol", value=routing_protocols))
704 proposed_payload["routingProtocol"] = routing_protocols
705
706 if vrf_ref is not None and mso.existing.get("vrfRef") != vrf_ref:
707 ops.append(dict(op="replace", path=path + "/vrfRef", value=vrf_ref))
708 proposed_payload["vrfRef"] = vrf_ref
709 proposed_payload["vrfName"] = vrf_dict.get("name")
710
711 if description is not None and mso.existing.get("description") != description:
712 ops.append(dict(op="replace", path=path + "/description", value=description))
713 proposed_payload["description"] = description
714
715 if l3_domain is not None and mso.existing.get("l3domain") != l3_domain:
716 ops.append(dict(op="replace", path=path + "/l3domain", value=l3_domain))
717 proposed_payload["l3domain"] = l3_domain
718
719 if target_dscp is not None and mso.existing.get("targetDscp") != target_dscp:
720 ops.append(dict(op="replace", path=path + "/targetDscp", value=target_dscp))
721 proposed_payload["targetDscp"] = target_dscp
722
723 if pim is not None and mso.existing.get("pim") != pim:
724 ops.append(dict(op="replace", path=path + "/pim", value=pim))
725 proposed_payload["pim"] = pim
726
727 if (
728 interleak is not None
729 or static_route_redistribution is not None
730 or connected_route_redistribution is not None
731 or attached_host_route_redistribution is not None
732 or (bgp is not None and (bgp.get("route_dampening_ipv4") is not None or bgp.get("route_dampening_ipv6") is not None))
733 ) and not mso.existing.get("advancedRouteMapRefs"):
734 ops.append(dict(op="add", path=path + "/advancedRouteMapRefs", value=dict()))
735 proposed_payload["advancedRouteMapRefs"] = dict()
736
737 outer_route_maps = dict()
738
739 if interleak is not None:
740 interleak_ref = mso_template.get_route_map(
741 "interleak",
742 tenant_id,
743 tenant_name,
744 interleak,
745 route_map_objects,
746 ).get("uuid", "")
747
748 if mso.existing.get("advancedRouteMapRefs", {}).get("interleakRef") != interleak_ref:
749 ops.append(dict(op="replace", path=path + "/advancedRouteMapRefs/interleakRef", value=interleak_ref))
750 outer_route_maps["interleakRef"] = interleak_ref
751 outer_route_maps["interleakName"] = interleak
752
753 if static_route_redistribution is not None:
754 static_route_redistribution_ref = mso_template.get_route_map(
755 "static_route_redistribution",
756 tenant_id,
757 tenant_name,
758 static_route_redistribution,
759 route_map_objects,
760 ).get("uuid", "")
761
762 if mso.existing.get("advancedRouteMapRefs", {}).get("staticRouteRedistRef") != static_route_redistribution_ref:
763 ops.append(dict(op="replace", path=path + "/advancedRouteMapRefs/staticRouteRedistRef", value=static_route_redistribution_ref))
764 outer_route_maps["staticRouteRedistRef"] = static_route_redistribution_ref
765 outer_route_maps["staticRouteRedistName"] = static_route_redistribution
766
767 if connected_route_redistribution is not None:
768 connected_route_redistribution_ref = mso_template.get_route_map(
769 "connected_route_redistribution",
770 tenant_id,
771 tenant_name,
772 connected_route_redistribution,
773 route_map_objects,
774 ).get("uuid", "")
775
776 if mso.existing.get("advancedRouteMapRefs", {}).get("connectedRouteRedistRef") != connected_route_redistribution_ref:
777 ops.append(dict(op="replace", path=path + "/advancedRouteMapRefs/connectedRouteRedistRef", value=connected_route_redistribution_ref))
778 outer_route_maps["connectedRouteRedistRef"] = connected_route_redistribution_ref
779 outer_route_maps["connectedRouteRedistName"] = connected_route_redistribution
780
781 if attached_host_route_redistribution is not None:
782 attached_host_route_redistribution_ref = mso_template.get_route_map(
783 "attached_host_route_redistribution",
784 tenant_id,
785 tenant_name,
786 attached_host_route_redistribution,
787 route_map_objects,
788 ).get("uuid", "")
789
790 if mso.existing.get("advancedRouteMapRefs", {}).get("attachedHostRouteRedistRef") != attached_host_route_redistribution_ref:
791 ops.append(
792 dict(
793 op="replace",
794 path=path + "/advancedRouteMapRefs/attachedHostRouteRedistRef",
795 value=attached_host_route_redistribution_ref,
796 )
797 )
798 outer_route_maps["attachedHostRouteRedistRef"] = attached_host_route_redistribution_ref
799 outer_route_maps["attachedHostRouteRedistName"] = attached_host_route_redistribution
800
801 if bgp and bgp_state != "disabled":
802 if bgp.get("inbound_route_map") is not None:
803 inbound_route_map_ref = mso_template.get_route_map(
804 "inbound_route_map",
805 tenant_id,
806 tenant_name,
807 bgp.get("inbound_route_map"),
808 route_map_objects,
809 ).get("uuid", "")
810
811 if mso.existing.get("importRouteMapRef") != inbound_route_map_ref:
812 ops.append(dict(op="replace", path=path + "/importRouteMapRef", value=inbound_route_map_ref))
813 ops.append(dict(op="replace", path=path + "/importRouteControl", value=True if inbound_route_map_ref else False))
814
815 proposed_payload["importRouteMapRef"] = inbound_route_map_ref
816 proposed_payload["importRouteMapName"] = bgp.get("inbound_route_map")
817 proposed_payload["importRouteControl"] = True if inbound_route_map_ref else False
818
819 if bgp.get("outbound_route_map") is not None:
820 outbound_route_map_ref = mso_template.get_route_map(
821 "outbound_route_map",
822 tenant_id,
823 tenant_name,
824 bgp.get("outbound_route_map"),
825 route_map_objects,
826 ).get("uuid", "")
827
828 if mso.existing.get("exportRouteMapRef") != outbound_route_map_ref:
829 ops.append(dict(op="replace", path=path + "/exportRouteMapRef", value=outbound_route_map_ref))
830 proposed_payload["exportRouteMapRef"] = outbound_route_map_ref
831 proposed_payload["exportRouteMapName"] = bgp.get("outbound_route_map")
832
833 if bgp.get("route_dampening_ipv4") is not None:
834 route_dampening_ipv4_ref = mso_template.get_route_map(
835 "route_dampening_ipv4",
836 tenant_id,
837 tenant_name,
838 bgp.get("route_dampening_ipv4"),
839 route_map_objects,
840 ).get("uuid", "")
841
842 if mso.existing.get("advancedRouteMapRefs", {}).get("routeDampeningV4Ref") != route_dampening_ipv4_ref:
843 ops.append(dict(op="replace", path=path + "/advancedRouteMapRefs/routeDampeningV4Ref", value=route_dampening_ipv4_ref))
844 outer_route_maps["routeDampeningV4Ref"] = route_dampening_ipv4_ref
845 outer_route_maps["routeDampeningV4Name"] = bgp.get("route_dampening_ipv4")
846
847 if bgp.get("route_dampening_ipv6") is not None:
848 route_dampening_ipv6_ref = mso_template.get_route_map(
849 "route_dampening_ipv6",
850 tenant_id,
851 tenant_name,
852 bgp.get("route_dampening_ipv6"),
853 route_map_objects,
854 ).get("uuid", "")
855
856 if mso.existing.get("advancedRouteMapRefs", {}).get("routeDampeningV6Ref") != route_dampening_ipv6_ref:
857 ops.append(dict(op="replace", path=path + "/advancedRouteMapRefs/routeDampeningV6Ref", value=route_dampening_ipv6_ref))
858 outer_route_maps["routeDampeningV6Ref"] = route_dampening_ipv6_ref
859 outer_route_maps["routeDampeningV6Name"] = bgp.get("route_dampening_ipv6")
860
861 elif bgp_state == "disabled":
862 ops.append(dict(op="replace", path=path + "/importRouteMapRef", value=""))
863 ops.append(dict(op="replace", path=path + "/importRouteControl", value=False))
864 proposed_payload.pop("importRouteMapRef", None)
865 proposed_payload["importRouteControl"] = False
866
867 ops.append(dict(op="replace", path=path + "/exportRouteMapRef", value=""))
868 proposed_payload.pop("exportRouteMapRef", None)
869
870 ops.append(dict(op="replace", path=path + "/advancedRouteMapRefs/routeDampeningV6Ref", value=""))
871 proposed_payload.get("advancedRouteMapRefs", {}).pop("routeDampeningV6Ref", None)
872
873 ops.append(dict(op="replace", path=path + "/advancedRouteMapRefs/routeDampeningV4Ref", value=""))
874 proposed_payload.get("advancedRouteMapRefs", {}).pop("routeDampeningV4Ref", None)
875
876 if outer_route_maps:
877 for key, value in outer_route_maps.items():
878 proposed_payload["advancedRouteMapRefs"][key] = value
879
880 if ospf and ospf_state != "disabled":
881 originate_default_route = ORIGINATE_DEFAULT_ROUTE.get(ospf.get("originate_default_route"))
882 originate_default_route_always = ospf.get("originate_default_route_always")
883
884 if originate_default_route is not None and originate_default_route == "" and mso.existing.get("defaultRouteLeak"):
885 ops.append(dict(op="remove", path=path + "/defaultRouteLeak"))
886 proposed_payload.pop("defaultRouteLeak", None)
887
888 elif (originate_default_route is not None and originate_default_route != "") or (originate_default_route_always is not None):
889 if not mso.existing.get("defaultRouteLeak"):
890 ops.append(dict(op="replace", path=path + "/defaultRouteLeak", value=dict()))
891 proposed_payload["defaultRouteLeak"] = dict()
892
893 if originate_default_route != mso.existing.get("defaultRouteLeak", {}).get("originateDefaultRoute"):
894 ops.append(
895 dict(
896 op="replace",
897 path=path + "/defaultRouteLeak/originateDefaultRoute",
898 value=originate_default_route,
899 )
900 )
901 proposed_payload["defaultRouteLeak"]["originateDefaultRoute"] = originate_default_route
902
903 if originate_default_route_always is not None and originate_default_route_always != mso.existing.get("defaultRouteLeak", {}).get("always"):
904 ops.append(dict(op="replace", path=path + "/defaultRouteLeak/always", value=originate_default_route_always))
905 proposed_payload["defaultRouteLeak"]["always"] = originate_default_route_always
906
907 if not mso.existing.get("ospfAreaConfig"):
908 ops.append(dict(op="replace", path=path + "/ospfAreaConfig", value=dict()))
909 proposed_payload["ospfAreaConfig"] = dict()
910
911 if ospf.get("cost") is not None and ospf.get("cost") != mso.existing.get("ospfAreaConfig", {}).get("cost"):
912 ops.append(dict(op="replace", path=path + "/ospfAreaConfig/cost", value=ospf.get("cost")))
913 proposed_payload["ospfAreaConfig"]["cost"] = ospf.get("cost")
914
915 if ospf.get("area_id") is not None and ospf.get("area_id") != mso.existing.get("ospfAreaConfig", {}).get("id"):
916 ops.append(dict(op="replace", path=path + "/ospfAreaConfig/id", value=ospf.get("area_id")))
917 proposed_payload["ospfAreaConfig"]["id"] = ospf.get("area_id")
918
919 if ospf.get("area_type") is not None and ospf.get("area_type") != mso.existing.get("ospfAreaConfig", {}).get("areaType"):
920 ops.append(dict(op="replace", path=path + "/ospfAreaConfig/areaType", value=ospf.get("area_type")))
921 proposed_payload["ospfAreaConfig"]["areaType"] = ospf.get("area_type")
922
923 redistribute = ospf.get("send_redistributed_lsas")
924 originate = ospf.get("originate_summary_lsa")
925 suppress_fa = ospf.get("suppress_forwarding_addr_translated_lsa")
926
927 if (redistribute is not None or originate is not None or suppress_fa is not None) and not mso.existing.get("ospfAreaConfig", {}).get(
928 "control"
929 ):
930 ops.append(dict(op="replace", path=path + "/ospfAreaConfig/control", value=dict()))
931 proposed_payload["ospfAreaConfig"]["control"] = dict()
932
933 if redistribute is not None and redistribute != mso.existing.get("ospfAreaConfig", {}).get("control", {}).get("redistribute"):
934 ops.append(dict(op="replace", path=path + "/ospfAreaConfig/control/redistribute", value=redistribute))
935 proposed_payload["ospfAreaConfig"]["control"]["redistribute"] = redistribute
936
937 if originate is not None and originate != mso.existing.get("ospfAreaConfig", {}).get("control", {}).get("originate"):
938 ops.append(dict(op="replace", path=path + "/ospfAreaConfig/control/originate", value=originate))
939 proposed_payload["ospfAreaConfig"]["control"]["originate"] = originate
940
941 if suppress_fa is not None and suppress_fa != mso.existing.get("ospfAreaConfig", {}).get("control", {}).get("suppressFA"):
942 ops.append(dict(op="replace", path=path + "/ospfAreaConfig/control/suppressFA", value=suppress_fa))
943 proposed_payload["ospfAreaConfig"]["control"]["suppressFA"] = suppress_fa
944
945 elif ospf_state == "disabled":
946 ops.append(dict(op="remove", path=path + "/ospfAreaConfig"))
947 proposed_payload.pop("ospfAreaConfig", None)
948
949 ops.append(dict(op="remove", path=path + "/defaultRouteLeak"))
950 proposed_payload.pop("defaultRouteLeak", None)
951
952 mso.sanitize(proposed_payload, collate=True)
953
954 elif state == "absent":
955 if mso.existing:
956 ops.append(dict(op="remove", path="/l3outTemplate/l3outs/{0}".format(match.index)))
957
958 if not module.check_mode and ops:
959 response_object = mso.request(mso_template.template_path, method="PATCH", data=ops)
960
961 mso_template.template = response_object
962 match = mso_template.get_l3out_object(uuid, name)
963
964 if match:
965 mso.existing = insert_l3out_relation_name(match.details, mso_template) # When the state is present
966 else:
967 mso.existing = {} # When the state is absent
968 elif module.check_mode and state != "query": # When the state is present/absent with check mode
969 mso.existing = insert_l3out_relation_name(mso.proposed, mso_template, module.check_mode) if state == "present" else {}
970
971 mso.exit_json()
972
973
974 def insert_l3out_relation_name(l3out_object, mso_template, check_mode=False):
975 l3out_object["vrfName"] = mso_template.get_template_object_name_by_uuid("vrf", l3out_object.get("vrfRef"))
976
977 if not check_mode:
978 l3out_relations = mso_template.mso.request("{0}/relations".format(mso_template.template_path), "GET")
979 route_map_policies = l3out_relations.get("relations", {}).get("identities", {}).get("routeMapPolicies", [])
980
981 if "exportRouteMapRef" in l3out_object:
982 if check_mode:
983 l3out_object["exportRouteMapName"] = mso_template.get_template_object_name_by_uuid("routeMap", l3out_object["exportRouteMapRef"])
984 else:
985 l3out_object["exportRouteMapName"] = (
986 mso_template.get_object_by_key_value_pairs(
987 "Route Map",
988 route_map_policies,
989 [KVPair("ref", l3out_object["exportRouteMapRef"])],
990 True,
991 ).details
992 or {}
993 ).get("displayName")
994
995 if "importRouteMapRef" in l3out_object:
996 if check_mode:
997 l3out_object["importRouteMapName"] = mso_template.get_template_object_name_by_uuid("routeMap", l3out_object["importRouteMapRef"])
998 else:
999 l3out_object["importRouteMapName"] = (
1000 mso_template.get_object_by_key_value_pairs(
1001 "Route Map",
1002 route_map_policies,
1003 [KVPair("ref", l3out_object["importRouteMapRef"])],
1004 True,
1005 ).details
1006 or {}
1007 ).get("displayName")
1008
1009 advanced_route_maps = l3out_object.get("advancedRouteMapRefs")
1010 if advanced_route_maps is not None:
1011 if "attachedHostRouteRedistRef" in advanced_route_maps:
1012 if check_mode:
1013 l3out_object["advancedRouteMapRefs"]["attachedHostRouteRedistName"] = mso_template.get_template_object_name_by_uuid(
1014 "routeMap", advanced_route_maps["attachedHostRouteRedistRef"]
1015 )
1016 else:
1017 l3out_object["advancedRouteMapRefs"]["attachedHostRouteRedistName"] = (
1018 mso_template.get_object_by_key_value_pairs(
1019 "Route Map",
1020 route_map_policies,
1021 [KVPair("ref", advanced_route_maps["attachedHostRouteRedistRef"])],
1022 True,
1023 ).details
1024 or {}
1025 ).get("displayName")
1026
1027 if "connectedRouteRedistRef" in advanced_route_maps:
1028 if check_mode:
1029 l3out_object["advancedRouteMapRefs"]["connectedRouteRedistName"] = mso_template.get_template_object_name_by_uuid(
1030 "routeMap", advanced_route_maps["connectedRouteRedistRef"]
1031 )
1032 else:
1033 l3out_object["advancedRouteMapRefs"]["connectedRouteRedistName"] = (
1034 mso_template.get_object_by_key_value_pairs(
1035 "Route Map",
1036 route_map_policies,
1037 [KVPair("ref", advanced_route_maps["connectedRouteRedistRef"])],
1038 True,
1039 ).details
1040 or {}
1041 ).get("displayName")
1042
1043 if "interleakRef" in advanced_route_maps:
1044 if check_mode:
1045 l3out_object["advancedRouteMapRefs"]["interleakName"] = mso_template.get_template_object_name_by_uuid(
1046 "routeMap", advanced_route_maps["interleakRef"]
1047 )
1048 else:
1049 l3out_object["advancedRouteMapRefs"]["interleakName"] = (
1050 mso_template.get_object_by_key_value_pairs(
1051 "Route Map",
1052 route_map_policies,
1053 [KVPair("ref", advanced_route_maps["interleakRef"])],
1054 True,
1055 ).details
1056 or {}
1057 ).get("displayName")
1058
1059 if "routeDampeningV4Ref" in advanced_route_maps:
1060 if check_mode:
1061 l3out_object["advancedRouteMapRefs"]["routeDampeningV4Name"] = mso_template.get_template_object_name_by_uuid(
1062 "routeMap", advanced_route_maps["routeDampeningV4Ref"]
1063 )
1064 else:
1065 l3out_object["advancedRouteMapRefs"]["routeDampeningV4Name"] = (
1066 mso_template.get_object_by_key_value_pairs(
1067 "Route Map",
1068 route_map_policies,
1069 [KVPair("ref", advanced_route_maps["routeDampeningV4Ref"])],
1070 True,
1071 ).details
1072 or {}
1073 ).get("displayName")
1074
1075 if "routeDampeningV6Ref" in advanced_route_maps:
1076 if check_mode:
1077 l3out_object["advancedRouteMapRefs"]["routeDampeningV6Name"] = mso_template.get_template_object_name_by_uuid(
1078 "routeMap", advanced_route_maps["routeDampeningV6Ref"]
1079 )
1080 else:
1081 l3out_object["advancedRouteMapRefs"]["routeDampeningV6Name"] = (
1082 mso_template.get_object_by_key_value_pairs(
1083 "Route Map",
1084 route_map_policies,
1085 [KVPair("ref", advanced_route_maps["routeDampeningV6Ref"])],
1086 True,
1087 ).details
1088 or {}
1089 ).get("displayName")
1090
1091 if "staticRouteRedistRef" in advanced_route_maps:
1092 if check_mode:
1093 l3out_object["advancedRouteMapRefs"]["staticRouteRedistName"] = mso_template.get_template_object_name_by_uuid(
1094 "routeMap", advanced_route_maps["staticRouteRedistRef"]
1095 )
1096 else:
1097 l3out_object["advancedRouteMapRefs"]["staticRouteRedistName"] = (
1098 mso_template.get_object_by_key_value_pairs(
1099 "Route Map",
1100 route_map_policies,
1101 [KVPair("ref", advanced_route_maps["staticRouteRedistRef"])],
1102 True,
1103 ).details
1104 or {}
1105 ).get("displayName")
1106 return l3out_object
1107
1108
1109 if __name__ == "__main__":
1110 main()