Debian Codegraph @

ansible_collections/cisco/aci/plugins/modules/aci_l3out.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2023, Gaspard Micol (@gmicol) <[email protected]>
5 # Copyright: (c) 2023, Akini Ross (@akinross) <[email protected]>
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 = {
13 "metadata_version": "1.1",
14 "status": ["preview"],
15 "supported_by": "certified",
16 }
17
18 DOCUMENTATION = r"""
19 ---
20 module: aci_l3out
21 short_description: Manage Layer 3 Outside (L3Out) objects (l3ext:Out)
22 description:
23 - Manage Layer 3 Outside (L3Out) on Cisco ACI fabrics.
24 options:
25 tenant:
26 description:
27 - Name of an existing tenant.
28 type: str
29 aliases: [ tenant_name ]
30 l3out:
31 description:
32 - Name of L3Out being created.
33 type: str
34 aliases: [ l3out_name, name ]
35 vrf:
36 description:
37 - Name of the VRF being associated with the L3Out.
38 - SR-MPLS Infra L3out requires the VRF to be 'overlay-1'.
39 type: str
40 aliases: [ vrf_name ]
41 domain:
42 description:
43 - Name of the external L3 domain being associated with the L3Out.
44 type: str
45 aliases: [ ext_routed_domain_name, routed_domain ]
46 dscp:
47 description:
48 - The target Differentiated Service (DSCP) value.
49 - The APIC defaults to C(unspecified) when unset during creation.
50 type: str
51 choices: [ AF11, AF12, AF13, AF21, AF22, AF23, AF31, AF32, AF33, AF41, AF42, AF43, CS0, CS1, CS2, CS3, CS4, CS5, CS6, CS7, EF, VA, unspecified ]
52 aliases: [ target ]
53 route_control:
54 description:
55 - Route Control enforcement direction. The only allowed values are export or import,export.
56 - The APIC defaults to C(export) when unset during creation.
57 type: list
58 elements: str
59 choices: [ export, import ]
60 aliases: [ route_control_enforcement ]
61 mpls:
62 description:
63 - Indicate whether MPLS (Multi-Protocol Label Switching) is enabled or not.
64 - The APIC defaults to C(no) when unset during creation.
65 - SR-MPLS is only supported in APIC v5.0 and above.
66 - The child classes C(mplsExtP), C(mplsRsLabelPol) and C(l3extProvLbl) will be displayed in output only when C(yes).
67 type: str
68 choices: [ "no", "yes" ]
69 l3protocol:
70 description:
71 - Routing protocol for the L3Out.
72 - Protocols already associated with an l3out must be provided again when the l3out is modified if the associated protocols are to be kept.
73 - The Protocols are otherwise deleted if not provided each time an l3out is modified.
74 - First example, to add BGP protocol to an l3out with OSPF protocol, the user must enter C([ bgp, ospf ]) even though "ospf" was provided before.
75 - Second example, to change the protocol from OSPF to EIGRP, the user must simply enter C([ eigrp ]) and the previous OSPF protocol will be deleted.
76 - To remove all existing protocols, the user must enter C([ static ]).
77 - SR-MPLS Infra L3out requires the l3protocol to be 'bgp'.
78 type: list
79 elements: str
80 choices: [ bgp, eigrp, ospf, pim, static ]
81 ospf:
82 description:
83 - Parameters for the OSPF protocol.
84 type: dict
85 suboptions:
86 area_cost:
87 description:
88 - The OSPF area cost.
89 - The APIC defaults to C(1) when unset during creation.
90 type: int
91 area_ctrl:
92 description:
93 - The controls of redistribution and summary LSA generation into NSSA and Stub areas.
94 - The APIC defaults to C(redistribute,summary) when unset during creation.
95 type: list
96 elements: str
97 choices: [ redistribute, summary, suppress-fa, unspecified ]
98 area_id:
99 description:
100 - The OSPF Area ID.
101 - An area is a logical collection of OSPF networks, routers, and links that have the same area identification.
102 - A router within an area must maintain a topological database for the area to which it belongs.
103 - The router doesn't have detailed information about network topology outside of its area, thereby reducing the size of its database.
104 - Areas limit the scope of route information distribution. It is not possible to do route update filtering within an area.
105 - The link-state database (LSDB) of routers within the same area must be synchronized and be exactly the same.
106 - However, route summarization and filtering is possible between different areas.
107 - The main benefit of creating areas is a reduction in the number of routes to propagate-by the filtering and the summarization of routes.
108 - Areas are identified by an area ID.
109 - Cisco IOS software supports area IDs expressed in IP address format or decimal format, for example, area 0.0.0.0 is equal to area 0.
110 - The APIC defaults to C(1) when unset during creation.
111 type: str
112 area_type:
113 description:
114 - The OSPF area type.
115 - The APIC defaults to C(nssa) when unset during creation.
116 type: str
117 choices: [ nssa, regular, stub ]
118 description:
119 description:
120 - Specifies the description of a policy component.
121 type: str
122 aliases: [ descr ]
123 multipod_internal:
124 description:
125 - Start OSPF in WAN instance instead of default.
126 - The APIC defaults to C(no) when unset during creation.
127 type: str
128 choices: [ "no", "yes" ]
129 name_alias:
130 description:
131 - The alias for the current object. This relates to the nameAlias field in ACI.
132 type: str
133 asn:
134 description:
135 - The AS number for the L3Out.
136 - Only applicable when using 'eigrp' as the l3protocol.
137 type: int
138 aliases: [ as_number ]
139 description:
140 description:
141 - Description for the L3Out.
142 type: str
143 aliases: [ descr ]
144 state:
145 description:
146 - Use C(present) or C(absent) for adding or removing.
147 - Use C(query) for listing an object or multiple objects.
148 type: str
149 choices: [ absent, present, query ]
150 default: present
151 name_alias:
152 description:
153 - The alias for the current object. This relates to the nameAlias field in ACI.
154 type: str
155 extends_documentation_fragment:
156 - cisco.aci.aci
157 - cisco.aci.annotation
158 - cisco.aci.owner
159
160 notes:
161 - The C(tenant) and C(domain) and C(vrf) used must exist before using this module in your playbook.
162 - The M(cisco.aci.aci_tenant) and M(cisco.aci.aci_domain) and M(cisco.aci.aci_vrf) modules can be used for this.
163 seealso:
164 - module: cisco.aci.aci_tenant
165 - module: cisco.aci.aci_domain
166 - module: cisco.aci.aci_vrf
167 - name: APIC Management Information Model reference
168 description: More information about the internal APIC class B(l3ext:Out).
169 link: https://developer.cisco.com/docs/apic-mim-ref/
170 author:
171 - Rostyslav Davydenko (@rost-d)
172 - Gaspard Micol (@gmicol)
173 - Akini Ross (@akinross)
174 """
175
176 EXAMPLES = r"""
177 - name: Add a new L3Out
178 cisco.aci.aci_l3out:
179 host: apic
180 username: admin
181 password: SomeSecretPassword
182 tenant: production
183 name: prod_l3out
184 description: L3Out for Production tenant
185 domain: l3dom_prod
186 vrf: prod
187 l3protocol: ospf
188 ospf:
189 area_cost: 1
190 area_ctrl: [summary, redistribute]
191 area_id: 0.0.0.1
192 area_type: regular
193 multipod_internal: 'no'
194 state: present
195 delegate_to: localhost
196
197 - name: Delete L3Out
198 cisco.aci.aci_l3out:
199 host: apic
200 username: admin
201 password: SomeSecretPassword
202 tenant: production
203 name: prod_l3out
204 state: absent
205 delegate_to: localhost
206
207 - name: Query L3Out information
208 cisco.aci.aci_l3out:
209 host: apic
210 username: admin
211 password: SomeSecretPassword
212 tenant: production
213 name: prod_l3out
214 state: query
215 delegate_to: localhost
216 register: query_result
217
218 - name: Query all L3Outs
219 cisco.aci.aci_l3out:
220 host: apic
221 username: admin
222 password: SomeSecretPassword
223 state: query
224 delegate_to: localhost
225 register: query_all_result
226 """
227
228 RETURN = r"""
229 current:
230 description: The existing configuration from the APIC after the module has finished
231 returned: success
232 type: list
233 sample:
234 [
235 {
236 "fvTenant": {
237 "attributes": {
238 "descr": "Production environment",
239 "dn": "uni/tn-production",
240 "name": "production",
241 "nameAlias": "",
242 "ownerKey": "",
243 "ownerTag": ""
244 }
245 }
246 }
247 ]
248 error:
249 description: The error information as returned from the APIC
250 returned: failure
251 type: dict
252 sample:
253 {
254 "code": "122",
255 "text": "unknown managed object class foo"
256 }
257 raw:
258 description: The raw output returned by the APIC REST API (xml or json)
259 returned: parse error
260 type: str
261 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
262 sent:
263 description: The actual/minimal configuration pushed to the APIC
264 returned: info
265 type: list
266 sample:
267 {
268 "fvTenant": {
269 "attributes": {
270 "descr": "Production environment"
271 }
272 }
273 }
274 previous:
275 description: The original configuration from the APIC before the module has started
276 returned: info
277 type: list
278 sample:
279 [
280 {
281 "fvTenant": {
282 "attributes": {
283 "descr": "Production",
284 "dn": "uni/tn-production",
285 "name": "production",
286 "nameAlias": "",
287 "ownerKey": "",
288 "ownerTag": ""
289 }
290 }
291 }
292 ]
293 proposed:
294 description: The assembled configuration from the user-provided parameters
295 returned: info
296 type: dict
297 sample:
298 {
299 "fvTenant": {
300 "attributes": {
301 "descr": "Production environment",
302 "name": "production"
303 }
304 }
305 }
306 filter_string:
307 description: The filter string used for the request
308 returned: failure or debug
309 type: str
310 sample: ?rsp-prop-include=config-only
311 method:
312 description: The HTTP method used for the request to the APIC
313 returned: failure or debug
314 type: str
315 sample: POST
316 response:
317 description: The HTTP response from the APIC
318 returned: failure or debug
319 type: str
320 sample: OK (30 bytes)
321 status:
322 description: The HTTP status from the APIC
323 returned: failure or debug
324 type: int
325 sample: 200
326 url:
327 description: The HTTP url used for the request to the APIC
328 returned: failure or debug
329 type: str
330 sample: https://10.11.12.13/api/mo/uni/tn-production.json
331 """
332
333 from ansible.module_utils.basic import AnsibleModule
334 from ansible_collections.cisco.aci.plugins.module_utils.aci import (
335 ACIModule,
336 aci_argument_spec,
337 aci_annotation_spec,
338 aci_owner_spec,
339 )
340 from ansible_collections.cisco.aci.plugins.module_utils.aci import ospf_spec
341
342
343 def main():
344 argument_spec = aci_argument_spec()
345 argument_spec.update(aci_annotation_spec())
346 argument_spec.update(aci_owner_spec())
347 argument_spec.update(
348 tenant=dict(type="str", aliases=["tenant_name"]), # Not required for querying all objects
349 l3out=dict(type="str", aliases=["l3out_name", "name"]), # Not required for querying all objects
350 domain=dict(type="str", aliases=["ext_routed_domain_name", "routed_domain"]),
351 vrf=dict(type="str", aliases=["vrf_name"]),
352 description=dict(type="str", aliases=["descr"]),
353 route_control=dict(
354 type="list",
355 elements="str",
356 choices=["export", "import"],
357 aliases=["route_control_enforcement"],
358 ),
359 mpls=dict(type="str", choices=["no", "yes"]),
360 dscp=dict(
361 type="str",
362 choices=[
363 "AF11",
364 "AF12",
365 "AF13",
366 "AF21",
367 "AF22",
368 "AF23",
369 "AF31",
370 "AF32",
371 "AF33",
372 "AF41",
373 "AF42",
374 "AF43",
375 "CS0",
376 "CS1",
377 "CS2",
378 "CS3",
379 "CS4",
380 "CS5",
381 "CS6",
382 "CS7",
383 "EF",
384 "VA",
385 "unspecified",
386 ],
387 aliases=["target"],
388 ),
389 l3protocol=dict(
390 type="list",
391 elements="str",
392 choices=["bgp", "eigrp", "ospf", "pim", "static"],
393 ),
394 ospf=dict(type="dict", options=ospf_spec()),
395 asn=dict(type="int", aliases=["as_number"]),
396 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
397 name_alias=dict(type="str"),
398 )
399
400 module = AnsibleModule(
401 argument_spec=argument_spec,
402 supports_check_mode=True,
403 required_if=[
404 ["state", "absent", ["l3out", "tenant"]],
405 ["state", "present", ["l3out", "tenant", "domain", "vrf"]],
406 ],
407 )
408
409 aci = ACIModule(module)
410
411 l3out = module.params.get("l3out")
412 domain = module.params.get("domain")
413 dscp = module.params.get("dscp")
414 description = module.params.get("description")
415 route_control = module.params.get("route_control")
416 mpls = module.params.get("mpls")
417 vrf = module.params.get("vrf")
418 l3protocol = module.params.get("l3protocol")
419 ospf = module.params.get("ospf")
420 asn = module.params.get("asn")
421 state = module.params.get("state")
422 tenant = module.params.get("tenant")
423 name_alias = module.params.get("name_alias")
424
425 if l3protocol:
426 if "eigrp" in l3protocol and asn is None:
427 module.fail_json(msg="Parameter 'asn' is required when l3protocol is 'eigrp'")
428 if "eigrp" not in l3protocol and asn is not None:
429 module.warn("Parameter 'asn' is only applicable when l3protocol is 'eigrp'. The ASN will be ignored")
430 if "ospf" not in l3protocol and ospf is not None:
431 module.warn("Parameter 'ospf' is only applicable when l3protocol is 'ospf'. The OPSF specifications will be ignored")
432
433 enforce_ctrl = None
434 if route_control is not None:
435 if len(route_control) == 1 and route_control[0] == "import":
436 aci.fail_json("The route_control parameter is invalid: allowed options are export or import,export only")
437 else:
438 enforce_ctrl = ",".join(route_control)
439
440 child_classes = [
441 "l3extRsL3DomAtt",
442 "l3extRsEctx",
443 "bgpExtP",
444 "ospfExtP",
445 "eigrpExtP",
446 "pimExtP",
447 ]
448
449 child_configs = [
450 dict(l3extRsL3DomAtt=dict(attributes=dict(tDn="uni/l3dom-{0}".format(domain)))),
451 dict(l3extRsEctx=dict(attributes=dict(tnFvCtxName=vrf))),
452 ]
453
454 if tenant == "infra" and mpls == "yes":
455 if l3protocol != ["bgp"] and state == "present":
456 module.fail_json(msg="The l3protocol parameter must be 'bgp' when tenant is 'infra' and mpls is 'yes'")
457 if vrf != "overlay-1" and state == "present":
458 module.fail_json(msg="The vrf parameter must be 'overlay-1' when tenant is 'infra' and mpls is 'yes'")
459 child_classes += ["mplsExtP", "mplsRsLabelPol", "l3extProvLbl"]
460 child_configs.append(
461 dict(mplsExtP=dict(attributes=dict(), children=[dict(mplsRsLabelPol=dict(attributes=dict(tDn="uni/tn-infra/mplslabelpol-default")))]))
462 )
463 child_configs.append(dict(l3extProvLbl=dict(attributes=dict(name=l3out))))
464
465 aci.construct_url(
466 root_class=dict(
467 aci_class="fvTenant",
468 aci_rn="tn-{0}".format(tenant),
469 module_object=tenant,
470 target_filter={"name": tenant},
471 ),
472 subclass_1=dict(
473 aci_class="l3extOut",
474 aci_rn="out-{0}".format(l3out),
475 module_object=l3out,
476 target_filter={"name": l3out},
477 ),
478 child_classes=child_classes,
479 )
480
481 aci.get_existing()
482
483 if state == "present":
484 if l3protocol is not None:
485 if isinstance(aci.existing, list) and len(aci.existing) > 0:
486 for child in aci.existing[0].get("l3extOut", {}).get("children", {}):
487 if child.get("bgpExtP") and "bgp" not in l3protocol:
488 child_configs.append(dict(bgpExtP=dict(attributes=dict(status="deleted"))))
489 if child.get("eigrpExtP") and "eigrp" not in l3protocol:
490 child_configs.append(dict(eigrpExtP=dict(attributes=dict(status="deleted"))))
491 if child.get("ospfExtP") and "ospf" not in l3protocol:
492 child_configs.append(dict(ospfExtP=dict(attributes=dict(status="deleted"))))
493 if child.get("pimExtP") and "pim" not in l3protocol:
494 child_configs.append(dict(pimExtP=dict(attributes=dict(status="deleted"))))
495
496 for protocol in l3protocol:
497 if protocol == "bgp":
498 child_configs.append(dict(bgpExtP=dict(attributes=dict(descr=""))))
499 elif protocol == "eigrp":
500 child_configs.append(dict(eigrpExtP=dict(attributes=dict(asn=asn))))
501 elif protocol == "ospf":
502 if isinstance(ospf, dict):
503 ospf["area_ctrl"] = ",".join(ospf.get("area_ctrl"))
504 child_configs.append(
505 dict(
506 ospfExtP=dict(
507 attributes=dict(
508 areaCost=ospf.get("area_cost"),
509 areaCtrl=ospf.get("area_ctrl"),
510 areaId=ospf.get("area_id"),
511 areaType=ospf.get("area_type"),
512 descr=ospf.get("description"),
513 multipodInternal=ospf.get("multipod_internal"),
514 nameAlias=ospf.get("name_alias"),
515 )
516 )
517 )
518 )
519 else:
520 child_configs.append(dict(ospfExtP=dict(attributes=dict(descr=""))))
521 elif protocol == "pim":
522 child_configs.append(dict(pimExtP=dict(attributes=dict(descr=""))))
523
524 aci.payload(
525 aci_class="l3extOut",
526 class_config=dict(
527 name=l3out,
528 descr=description,
529 enforceRtctrl=enforce_ctrl,
530 mplsEnabled=mpls,
531 targetDscp=dscp,
532 nameAlias=name_alias,
533 ),
534 child_configs=child_configs,
535 )
536
537 aci.get_diff(aci_class="l3extOut")
538
539 aci.post_config()
540
541 elif state == "absent":
542 aci.delete_config()
543
544 aci.exit_json()
545
546
547 if __name__ == "__main__":
548 main()