Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2017, Bruno Calogero <[email protected]>
5 # Copyright: (c) 2022, Sabari Jaganathan (@sajagana) <[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 = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "certified"}
13
14 DOCUMENTATION = r"""
15 ---
16 module: aci_bulk_static_binding_to_epg
17 short_description: Bind static paths to EPGs (fv:RsPathAtt)
18 description:
19 - Bind static paths to EPGs on Cisco ACI fabrics.
20 options:
21 tenant:
22 description:
23 - Name of the tenant.
24 type: str
25 aliases: [ tenant_name ]
26 ap:
27 description:
28 - The name of the application profile.
29 type: str
30 aliases: [ app_profile, app_profile_name ]
31 epg:
32 description:
33 - The name of the end point group.
34 type: str
35 aliases: [ epg_name ]
36 description:
37 description:
38 - Description for the static path to EPG binding.
39 type: str
40 aliases: [ descr ]
41 encap_id:
42 description:
43 - The encapsulation ID associating the C(epg) with the interface path.
44 - This acts as the secondary C(encap_id) when using micro-segmentation.
45 - Accepted values are any valid encap ID for specified encap, currently ranges between C(1) and C(4096).
46 type: int
47 aliases: [ vlan, vlan_id ]
48 primary_encap_id:
49 description:
50 - Determines the primary encapsulation ID associating the C(epg)
51 with the interface path when using micro-segmentation.
52 - Accepted values are any valid encap ID for specified encap, currently ranges between C(1) and C(4096) and C(unknown).
53 - C(unknown) is the default value and using C(unknown) disables the Micro-Segmentation.
54 type: str
55 aliases: [ primary_vlan, primary_vlan_id ]
56 deploy_immediacy:
57 description:
58 - The Deployment Immediacy of Static EPG on PC, VPC or Interface.
59 - The APIC defaults to C(lazy) when unset during creation.
60 type: str
61 choices: [ immediate, lazy ]
62 interface_mode:
63 description:
64 - Determines how layer 2 tags will be read from and added to frames.
65 - Values C(802.1p) and C(native) are identical.
66 - Values C(access) and C(untagged) are identical.
67 - Values C(regular), C(tagged) and C(trunk) are identical.
68 - The APIC defaults to C(trunk) when unset during creation.
69 type: str
70 choices: [ 802.1p, access, native, regular, tagged, trunk, untagged ]
71 aliases: [ interface_mode_name, mode ]
72 interface_type:
73 description:
74 - The type of interface for the static EPG deployment.
75 type: str
76 choices: [ fex, port_channel, switch_port, vpc, fex_port_channel, fex_vpc ]
77 default: switch_port
78 interface_configs:
79 description:
80 - List of interface configurations, elements in the form of a dictionary.
81 - Module level attributes will be overridden by the path level attributes.
82 type: list
83 elements: dict
84 suboptions:
85 description:
86 description:
87 - Description for the static path to EPG binding.
88 type: str
89 aliases: [ descr ]
90 encap_id:
91 description:
92 - The encapsulation ID associating the C(epg) with the interface path.
93 - This acts as the secondary C(encap_id) when using micro-segmentation.
94 - Accepted values are any valid encap ID for specified encap, currently ranges between C(1) and C(4096).
95 type: int
96 aliases: [ vlan, vlan_id ]
97 primary_encap_id:
98 description:
99 - Determines the primary encapsulation ID associating the C(epg)
100 with the interface path when using micro-segmentation.
101 - Accepted values are any valid encap ID for specified encap, currently ranges between C(1) and C(4096) and C(unknown).
102 - C(unknown) is the default value and using C(unknown) disables the Micro-Segmentation.
103 type: str
104 aliases: [ primary_vlan, primary_vlan_id ]
105 deploy_immediacy:
106 description:
107 - The Deployment Immediacy of Static EPG on PC, VPC or Interface.
108 - The APIC defaults to C(lazy) when unset during creation.
109 type: str
110 choices: [ immediate, lazy ]
111 interface_mode:
112 description:
113 - Determines how layer 2 tags will be read from and added to frames.
114 - Values C(802.1p) and C(native) are identical.
115 - Values C(access) and C(untagged) are identical.
116 - Values C(regular), C(tagged) and C(trunk) are identical.
117 - The APIC defaults to C(trunk) when unset during creation.
118 type: str
119 choices: [ 802.1p, access, native, regular, tagged, trunk, untagged ]
120 aliases: [ interface_mode_name, mode ]
121 interface_type:
122 description:
123 - The type of interface for the static EPG deployment.
124 type: str
125 choices: [ fex, port_channel, switch_port, vpc, fex_port_channel, fex_vpc ]
126 pod_id:
127 description:
128 - The pod number part of the tDn.
129 - C(pod_id) is usually an integer below C(10).
130 type: int
131 required: true
132 aliases: [ pod, pod_number ]
133 leafs:
134 description:
135 - The switch ID(s) that the C(interface) belongs to.
136 - When C(interface_type) is C(switch_port), C(port_channel), or C(fex), then C(leafs) is a string of the leaf ID.
137 - When C(interface_type) is C(vpc), then C(leafs) is a list with both leaf IDs.
138 - The C(leafs) value is usually something like '101' or '101-102' depending on C(connection_type).
139 type: list
140 elements: str
141 required: true
142 aliases: [ leaves, nodes, paths, switches ]
143 interface:
144 description:
145 - The C(interface) string value part of the tDn.
146 - Usually a policy group like C(test-IntPolGrp) or an interface of the following format C(1/7) depending on C(interface_type).
147 type: str
148 required: true
149 extpaths:
150 description:
151 - The C(extpaths) integer value part of the tDn.
152 - C(extpaths) is only used if C(interface_type) is C(fex), C(fex_vpc) or C(fex_port_channel).
153 - When C(interface_type) is C(fex_vpc), then C(extpaths) is a list with both fex IDs.
154 - Usually something like C(1011).
155 type: list
156 elements: str
157 state:
158 description:
159 - Use C(present) or C(absent) for adding or removing.
160 - Use C(query) for listing an object or multiple objects.
161 type: str
162 choices: [ absent, present, query ]
163 default: present
164 extends_documentation_fragment:
165 - cisco.aci.aci
166 - cisco.aci.annotation
167
168 notes:
169 - The C(tenant), C(ap), C(epg) used must exist before using this module in your playbook.
170 The M(cisco.aci.aci_tenant), M(cisco.aci.aci_ap), M(cisco.aci.aci_epg) modules can be used for this.
171 seealso:
172 - module: cisco.aci.aci_tenant
173 - module: cisco.aci.aci_ap
174 - module: cisco.aci.aci_epg
175 - name: APIC Management Information Model reference
176 description: More information about the internal APIC class B(fv:RsPathAtt).
177 link: https://developer.cisco.com/docs/apic-mim-ref/
178 author:
179 - Bruno Calogero (@brunocalogero)
180 - Marcel Zehnder (@maercu)
181 - Sabari Jaganathan (@sajagana)
182 """
183
184 EXAMPLES = r"""
185 - name: Create list of interfaces using module level attributes
186 cisco.aci.aci_bulk_static_binding_to_epg:
187 host: apic
188 username: admin
189 password: SomeSecretPassword
190 tenant: accessport-code-cert
191 ap: accessport_code_app
192 epg: accessport_epg1
193 encap_id: 221
194 interface_mode: trunk
195 deploy_immediacy: lazy
196 description: "Module level attributes used to create interfaces"
197 interface_configs:
198 - interface: 1/7
199 leafs: 101
200 pod: 1
201 - interface: 1/7
202 leafs: 107
203 pod: 7
204 - interface: 1/8
205 leafs: 108
206 pod: 8
207 encap_id: 108
208 state: present
209 delegate_to: localhost
210
211 - name: Create/Update list of interfaces using path level attributes
212 cisco.aci.aci_bulk_static_binding_to_epg:
213 host: apic
214 username: admin
215 password: SomeSecretPassword
216 tenant: accessport-code-cert
217 ap: accessport_code_app
218 epg: accessport_epg1
219 interface_configs:
220 - interface: 1/7
221 leafs: 101
222 pod: 1
223 encap_id: 221
224 interface_mode: trunk
225 deploy_immediacy: lazy
226 description: "Path level attributes used to create/update interfaces"
227 - interface: 1/7
228 leafs: 107
229 pod: 7
230 encap_id: 221
231 interface_mode: trunk
232 deploy_immediacy: lazy
233 description: "Path level attributes used to create/update interfaces"
234 - interface: 1/8
235 leafs: 108
236 pod: 8
237 encap_id: 108
238 interface_mode: trunk
239 deploy_immediacy: lazy
240 description: "Path level attributes used to create/update interfaces"
241 state: present
242 delegate_to: localhost
243
244 - name: Query all interfaces of an EPG
245 cisco.aci.aci_bulk_static_binding_to_epg:
246 host: apic
247 username: admin
248 password: SomeSecretPassword
249 tenant: accessport-code-cert
250 ap: accessport_code_app
251 epg: accessport_epg1
252 state: query
253 delegate_to: localhost
254
255 - name: Query all interfaces
256 cisco.aci.aci_bulk_static_binding_to_epg:
257 host: apic
258 username: admin
259 password: SomeSecretPassword
260 state: query
261 delegate_to: localhost
262
263 - name: Remove list of interfaces
264 cisco.aci.aci_bulk_static_binding_to_epg:
265 host: apic
266 username: admin
267 password: SomeSecretPassword
268 tenant: accessport-code-cert
269 ap: accessport_code_app
270 epg: accessport_epg1
271 encap_id: 221
272 interface_mode: trunk
273 deploy_immediacy: lazy
274 interface_configs:
275 - interface: 1/7
276 leafs: 101
277 pod: 1
278 - interface: 1/7
279 leafs: 107
280 pod: 7
281 - interface: 1/8
282 leafs: 108
283 pod: 8
284 encap_id: 108
285 state: absent
286 delegate_to: localhost
287 """
288
289 RETURN = r"""
290 current:
291 description: The existing configuration from the APIC after the module has finished
292 returned: success
293 type: list
294 sample:
295 [
296 {
297 "fvTenant": {
298 "attributes": {
299 "descr": "Production environment",
300 "dn": "uni/tn-production",
301 "name": "production",
302 "nameAlias": "",
303 "ownerKey": "",
304 "ownerTag": ""
305 }
306 }
307 }
308 ]
309 error:
310 description: The error information as returned from the APIC
311 returned: failure
312 type: dict
313 sample:
314 {
315 "code": "122",
316 "text": "unknown managed object class foo"
317 }
318 raw:
319 description: The raw output returned by the APIC REST API (xml or json)
320 returned: parse error
321 type: str
322 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
323 sent:
324 description: The actual/minimal configuration pushed to the APIC
325 returned: info
326 type: list
327 sample:
328 {
329 "fvTenant": {
330 "attributes": {
331 "descr": "Production environment"
332 }
333 }
334 }
335 previous:
336 description: The original configuration from the APIC before the module has started
337 returned: info
338 type: list
339 sample:
340 [
341 {
342 "fvTenant": {
343 "attributes": {
344 "descr": "Production",
345 "dn": "uni/tn-production",
346 "name": "production",
347 "nameAlias": "",
348 "ownerKey": "",
349 "ownerTag": ""
350 }
351 }
352 }
353 ]
354 proposed:
355 description: The assembled configuration from the user-provided parameters
356 returned: info
357 type: dict
358 sample:
359 {
360 "fvTenant": {
361 "attributes": {
362 "descr": "Production environment",
363 "name": "production"
364 }
365 }
366 }
367 filter_string:
368 description: The filter string used for the request
369 returned: failure or debug
370 type: str
371 sample: ?rsp-prop-include=config-only
372 method:
373 description: The HTTP method used for the request to the APIC
374 returned: failure or debug
375 type: str
376 sample: POST
377 response:
378 description: The HTTP response from the APIC
379 returned: failure or debug
380 type: str
381 sample: OK (30 bytes)
382 status:
383 description: The HTTP status from the APIC
384 returned: failure or debug
385 type: int
386 sample: 200
387 url:
388 description: The HTTP url used for the request to the APIC
389 returned: failure or debug
390 type: str
391 sample: https://10.11.12.13/api/mo/uni/tn-production.json
392 """
393
394 from ansible.module_utils.basic import AnsibleModule
395 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec
396
397 INTERFACE_MODE_MAPPING = {
398 "802.1p": "native",
399 "access": "untagged",
400 "native": "native",
401 "regular": "regular",
402 "tagged": "regular",
403 "trunk": "regular",
404 "untagged": "untagged",
405 }
406
407 INTERFACE_TYPE_MAPPING = {
408 "fex": "topology/pod-{pod_id}/paths-{leafs}/extpaths-{extpaths}/pathep-[eth{interface}]",
409 "fex_port_channel": "topology/pod-{pod_id}/paths-{leafs}/extpaths-{extpaths}/pathep-[{interface}]",
410 "fex_vpc": "topology/pod-{pod_id}/protpaths-{leafs}/extprotpaths-{extpaths}/pathep-[{interface}]",
411 "port_channel": "topology/pod-{pod_id}/paths-{leafs}/pathep-[{interface}]",
412 "switch_port": "topology/pod-{pod_id}/paths-{leafs}/pathep-[eth{interface}]",
413 "vpc": "topology/pod-{pod_id}/protpaths-{leafs}/pathep-[{interface}]",
414 }
415
416 INTERFACE_STATUS_MAPPING = {"absent": "deleted"}
417
418
419 def main():
420 argument_spec = aci_argument_spec()
421 argument_spec.update(aci_annotation_spec())
422 argument_spec.update(
423 tenant=dict(type="str", aliases=["tenant_name"]),
424 ap=dict(type="str", aliases=["app_profile", "app_profile_name"]),
425 epg=dict(type="str", aliases=["epg_name"]),
426 description=dict(type="str", aliases=["descr"]),
427 encap_id=dict(type="int", aliases=["vlan", "vlan_id"]),
428 primary_encap_id=dict(type="str", aliases=["primary_vlan", "primary_vlan_id"]),
429 deploy_immediacy=dict(type="str", choices=["immediate", "lazy"]),
430 interface_mode=dict(
431 type="str", choices=["802.1p", "access", "native", "regular", "tagged", "trunk", "untagged"], aliases=["interface_mode_name", "mode"]
432 ),
433 interface_type=dict(type="str", default="switch_port", choices=["fex", "port_channel", "switch_port", "vpc", "fex_port_channel", "fex_vpc"]),
434 interface_configs=dict(
435 type="list",
436 elements="dict",
437 options=dict(
438 description=dict(type="str", aliases=["descr"]),
439 encap_id=dict(type="int", aliases=["vlan", "vlan_id"]),
440 primary_encap_id=dict(type="str", aliases=["primary_vlan", "primary_vlan_id"]),
441 deploy_immediacy=dict(type="str", choices=["immediate", "lazy"]),
442 interface_mode=dict(
443 type="str", choices=["802.1p", "access", "native", "regular", "tagged", "trunk", "untagged"], aliases=["interface_mode_name", "mode"]
444 ),
445 interface_type=dict(type="str", choices=["fex", "port_channel", "switch_port", "vpc", "fex_port_channel", "fex_vpc"]),
446 pod_id=dict(type="int", required=True, aliases=["pod", "pod_number"]),
447 leafs=dict(type="list", elements="str", required=True, aliases=["leaves", "nodes", "paths", "switches"]),
448 interface=dict(type="str", required=True),
449 extpaths=dict(type="list", elements="str"),
450 ),
451 ),
452 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
453 )
454
455 module = AnsibleModule(
456 argument_spec=argument_spec,
457 supports_check_mode=True,
458 required_if=[
459 ["state", "absent", ["ap", "epg", "tenant", "interface_configs"]],
460 ["state", "present", ["ap", "epg", "tenant", "interface_configs"]],
461 ],
462 )
463
464 tenant = module.params.get("tenant")
465 ap = module.params.get("ap")
466 epg = module.params.get("epg")
467 module_description = module.params.get("description")
468 module_encap_id = module.params.get("encap_id")
469 module_primary_encap_id = module.params.get("primary_encap_id")
470 module_deploy_immediacy = module.params.get("deploy_immediacy")
471 module_interface_mode = module.params.get("interface_mode")
472 module_interface_type = module.params.get("interface_type")
473 interface_configs = module.params.get("interface_configs")
474 state = module.params.get("state")
475
476 aci = ACIModule(module)
477 children = []
478
479 aci.construct_url(
480 root_class=dict(
481 aci_class="fvTenant",
482 aci_rn="tn-{0}".format(tenant),
483 module_object=tenant,
484 target_filter=dict(name=tenant),
485 ),
486 subclass_1=dict(
487 aci_class="fvAp",
488 aci_rn="ap-{0}".format(ap),
489 module_object=ap,
490 target_filter=dict(name=ap),
491 ),
492 subclass_2=dict(
493 aci_class="fvAEPg",
494 aci_rn="epg-{0}".format(epg),
495 module_object=epg,
496 target_filter=dict(name=epg),
497 ),
498 child_classes=["fvRsPathAtt"],
499 )
500
501 aci.get_existing()
502
503 if state == "present" or state == "absent":
504 for interface_config in interface_configs:
505 pod_id = interface_config.get("pod_id")
506 interface = interface_config.get("interface")
507 extpaths = interface_config.get("extpaths")
508
509 description = interface_config.get("description") or module_description
510 deploy_immediacy = interface_config.get("deploy_immediacy") or module_deploy_immediacy
511 interface_type = interface_config.get("interface_type") or module_interface_type
512 encap_id = interface_config.get("encap_id") or module_encap_id
513 primary_encap_id = interface_config.get("primary_encap_id") or module_primary_encap_id
514 interface_mode = interface_config.get("interface_mode") or module_interface_mode
515
516 if interface_type in ["fex", "fex_vpc", "fex_port_channel"] and extpaths is None:
517 aci.fail_json(msg="extpaths is required when interface_type is: {0}".format(interface_type))
518
519 # Process leafs, and support dash-delimited leafs
520 leafs = []
521 for leaf in interface_config.get("leafs"):
522 # Users are likely to use integers for leaf IDs, which would raise an exception when using the join method
523 leafs.extend(str(leaf).split("-"))
524 if len(leafs) == 1:
525 if interface_type in ["vpc", "fex_vpc"]:
526 aci.fail_json(msg='A interface_type of "vpc" requires 2 leafs')
527 leafs = leafs[0]
528 elif len(leafs) == 2:
529 if interface_type not in ["vpc", "fex_vpc"]:
530 aci.fail_json(msg='The interface_types "switch_port", "port_channel", and "fex" do not support using multiple leafs for a single binding')
531 leafs = "-".join(leafs)
532 else:
533 aci.fail_json(msg='The "leafs" parameter must not have more than 2 entries')
534
535 if extpaths is not None:
536 # Process extpaths, and support dash-delimited extpaths
537 extpaths = []
538 for extpath in interface_config.get("extpaths"):
539 # Users are likely to use integers for extpaths IDs, which would raise an exception when using the join method
540 extpaths.extend(str(extpath).split("-"))
541 if len(extpaths) == 1:
542 if interface_type == "fex_vpc":
543 aci.fail_json(msg='A interface_type of "fex_vpc" requires 2 extpaths')
544 extpaths = extpaths[0]
545 elif len(extpaths) == 2:
546 if interface_type != "fex_vpc":
547 aci.fail_json(msg='The interface_types "fex" and "fex_port_channel" do not support using multiple extpaths for a single binding')
548 extpaths = "-".join(extpaths)
549 else:
550 aci.fail_json(msg='The "extpaths" parameter must not have more than 2 entries')
551
552 if encap_id is not None:
553 if encap_id not in range(1, 4097):
554 aci.fail_json(msg="Valid VLAN assignments are from 1 to 4096")
555 encap_id = "vlan-{0}".format(encap_id)
556
557 if primary_encap_id is not None:
558 try:
559 primary_encap_id = int(primary_encap_id)
560 if isinstance(primary_encap_id, int) and primary_encap_id in range(1, 4097):
561 primary_encap_id = "vlan-{0}".format(primary_encap_id)
562 else:
563 aci.fail_json(msg="Valid VLAN assignments are from 1 to 4096 or unknown.")
564 except Exception as e:
565 if isinstance(primary_encap_id, str) and primary_encap_id != "unknown":
566 aci.fail_json(msg="Valid VLAN assignments are from 1 to 4096 or unknown. %s" % e)
567
568 static_path = INTERFACE_TYPE_MAPPING[interface_type].format(pod_id=pod_id, leafs=leafs, extpaths=extpaths, interface=interface)
569
570 interface_mode = INTERFACE_MODE_MAPPING.get(interface_mode)
571
572 interface_status = INTERFACE_STATUS_MAPPING.get(state)
573
574 children.append(
575 dict(
576 fvRsPathAtt=dict(
577 attributes=dict(
578 descr=description,
579 encap=encap_id,
580 primaryEncap=primary_encap_id,
581 instrImedcy=deploy_immediacy,
582 mode=interface_mode,
583 tDn=static_path,
584 status=interface_status,
585 )
586 )
587 )
588 )
589
590 aci.payload(
591 aci_class="fvAEPg",
592 class_config=dict(),
593 child_configs=children,
594 )
595
596 aci.get_diff(aci_class="fvAEPg")
597
598 aci.post_config()
599
600 aci.exit_json()
601
602
603 if __name__ == "__main__":
604 main()