Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2023, Tim Cragg (@timcragg)
5 # Copyright: (c) 2023, Akini Ross (@akinross)
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: aci_access_switch_policy_group
17 short_description: Manage Access Switch Policy Groups (infra:AccNodePGrp and infra:SpineAccNodePGrp).
18 description:
19 - Manage Access Switch Policy Groups on Cisco ACI fabrics.
20 options:
21 name:
22 description:
23 - The name of the access switch policy group.
24 aliases: [ policy_group ]
25 type: str
26 description:
27 description:
28 - The description of the access switch policy group.
29 type: str
30 switch_type:
31 description:
32 - Whether this is a leaf or spine policy group
33 type: str
34 choices: [ leaf, spine ]
35 required: true
36 spanning_tree_policy:
37 description:
38 - The spanning tree policy bound to the access switch policy group.
39 - Only available in APIC version 5.2 or later.
40 - Only available when I(switch_type=leaf).
41 - The APIC defaults to C("") which results in the target DN set to the default policy when unset during creation.
42 type: str
43 bfd_ipv4_policy:
44 description:
45 - The BFD IPv4 policy bound to the access switch policy group.
46 - The APIC defaults to C("") which results in the target DN set to the default policy when unset during creation.
47 type: str
48 bfd_ipv6_policy:
49 description:
50 - The BFD IPv6 policy bound to the access switch policy group.
51 - The APIC defaults to C("") which results in the target DN set to the default policy when unset during creation.
52 type: str
53 bfd_multihop_ipv4_policy:
54 description:
55 - The BFD multihop IPv4 policy bound to the access switch policy group.
56 - Only available in APIC version 5.x or later.
57 - Only available when I(switch_type=leaf).
58 - The APIC defaults to C("") which results in the target DN set to the default policy when unset during creation.
59 type: str
60 bfd_multihop_ipv6_policy:
61 description:
62 - The BFD multihop IPv6 policy bound to the access switch policy group.
63 - Only available in APIC version 5.x or later.
64 - Only available when I(switch_type=leaf).
65 - The APIC defaults to C("") which results in the target DN set to the default policy when unset during creation.
66 type: str
67 fibre_channel_node_policy:
68 description:
69 - The fibre channel node policy bound to the access switch policy group.
70 - Only available when I(switch_type=leaf).
71 - The APIC defaults to C("") which results in the target DN set to the default policy when unset during creation.
72 type: str
73 poe_node_policy:
74 description:
75 - The PoE node policy bound to the access switch policy group.
76 - Only available when I(switch_type=leaf).
77 - The APIC defaults to C("") which results in the target DN set to the default policy when unset during creation.
78 type: str
79 fibre_channel_san_policy:
80 description:
81 - The fibre channel SAN policy bound to the access switch policy group.
82 - Only available when I(switch_type=leaf).
83 - The APIC defaults to C("") which results in the target DN set to the default policy when unset during creation.
84 type: str
85 monitoring_policy:
86 description:
87 - The monitoring policy bound to the access switch policy group.
88 - Only available when I(switch_type=leaf).
89 - The APIC defaults to C("") which results in the target DN set to the default policy when unset during creation.
90 type: str
91 netflow_node_policy:
92 description:
93 - The netflow node policy bound to the access switch policy group.
94 - Only available when I(switch_type=leaf).
95 - The APIC defaults to C("") which results in the target DN set to the default policy when unset during creation.
96 type: str
97 copp_policy:
98 description:
99 - The CoPP policy bound to the access switch policy group.
100 - The APIC defaults to C("") which results in the target DN set to the default policy when unset during creation.
101 type: str
102 forward_scale_profile_policy:
103 description:
104 - The forward scale profile policy bound to the access switch policy group.
105 - Only available when I(switch_type=leaf).
106 - The APIC defaults to C("") which results in the target DN set to the default policy when unset during creation.
107 type: str
108 fast_link_failover_policy:
109 description:
110 - The fast link failover policy bound to the access switch policy group.
111 - Only available when I(switch_type=leaf).
112 - The APIC defaults to C("") which results in the target DN set to the default policy when unset during creation.
113 type: str
114 node_802_1x_authentication_policy:
115 description:
116 - The 802.1x node authentication policy bound to the access switch policy group.
117 - Only available when I(switch_type=leaf).
118 - The APIC defaults to C("") which results in the target DN set to the default policy when unset during creation.
119 type: str
120 copp_pre_filter_policy:
121 description:
122 - The CoPP pre-filter policy bound to the access switch policy group.
123 - The APIC defaults to C("") which results in the target DN set to the default policy when unset during creation.
124 type: str
125 equipment_flash_policy:
126 description:
127 - The equipment flash policy bound to the access switch policy group.
128 - Only available when I(switch_type=leaf).
129 - The APIC defaults to C("") which results in the target DN set to the default policy when unset during creation.
130 type: str
131 cdp_policy:
132 description:
133 - The CDP policy bound to the access switch policy group.
134 - The APIC defaults to C("") which results in the target DN set to the default policy when unset during creation.
135 type: str
136 lldp_policy:
137 description:
138 - The LLDP policy bound to the access switch policy group.
139 - The APIC defaults to C("") which results in the target DN set to the default policy when unset during creation.
140 type: str
141 sync_e_node_policy:
142 description:
143 - The SyncE node policy bound to the access switch policy group.
144 - Only available in APIC version 5.x or later.
145 - Only available when I(switch_type=leaf).
146 - The APIC defaults to C("") which results in the target DN set to the default policy when unset during creation.
147 type: str
148 ptp_node_policy:
149 description:
150 - The PTP node policy bound to the access switch policy group.
151 - Only available in APIC version 5.2 or later.
152 - Only available when I(switch_type=leaf).
153 - The APIC defaults to C("") which results in the target DN set to the default policy when unset during creation.
154 type: str
155 usb_configuration_policy:
156 description:
157 - The USB configuration policy bound to the access switch policy group.
158 - Only available in APIC version 5.2 or later.
159 - The APIC defaults to C("") which results in the target DN set to the default policy when unset during creation.
160 type: str
161 state:
162 description:
163 - Use C(present) or C(absent) for adding or removing.
164 - Use C(query) for listing an object or multiple objects.
165 type: str
166 choices: [ absent, present, query ]
167 default: present
168 extends_documentation_fragment:
169 - cisco.aci.aci
170 - cisco.aci.annotation
171 - cisco.aci.owner
172
173 seealso:
174 - name: APIC Management Information Model reference
175 description: More information about the internal APIC class B(infra:AccNodePGrp) and B(infra:SpineAccNodePGrp).
176 link: https://developer.cisco.com/docs/apic-mim-ref/
177 author:
178 - Tim Cragg (@timcragg)
179 - Akini Ross (@akinross)
180 """
181
182 EXAMPLES = r"""
183 - name: Create Leaf Access Switch Policy Group
184 cisco.aci.aci_access_switch_policy_group:
185 host: apic
186 username: admin
187 password: SomeSecretPassword
188 name: ansible_pol_grp_spine
189 switch_type: leaf
190 spanning_tree_policy: example_spanning_tree_policy
191 bfd_ipv4_policy: example_bfd_ipv4_policy
192 bfd_ipv6_policy: example_bfd_ipv6_policy
193 fibre_channel_node_policy: example_fibre_channel_node_policy
194 poe_node_policy: example_poe_node_policy
195 fibre_channel_san_policy: example_fibre_channel_san_policy
196 monitoring_policy: example_monitoring_policy
197 copp_policy: example_copp_policy
198 forward_scale_profile_policy: example_forward_scale_profile_policy
199 fast_link_failover_policy: example_fast_link_failover_policy
200 node_802_1x_authentication_policy: example_node_802_1x_authentication_policy
201 copp_pre_filter_policy: example_copp_pre_filter_policy
202 equipment_flash_policy: example_equipment_flash_policy
203 cdp_policy: example_cdp_policy
204 lldp_policy: example_lldp_policy
205 state: present
206 delegate_to: localhost
207
208 - name: Create Spine Access Switch Policy Group
209 cisco.aci.aci_access_switch_policy_group:
210 host: apic
211 username: admin
212 password: SomeSecretPassword
213 name: ansible_pol_grp_leaf
214 switch_type: spine
215 bfd_ipv4_policy: example_bfd_ipv4_policy
216 bfd_ipv6_policy: example_bfd_ipv6_policy
217 copp_policy: example_copp_policy
218 copp_pre_filter_policy: example_copp_pre_filter_policy
219 cdp_policy: example_cdp_policy
220 lldp_policy: example_lldp_policy
221 state: present
222 delegate_to: localhost
223
224 - name: Delete Leaf Access Switch Policy Group
225 cisco.aci.aci_access_switch_policy_group:
226 host: apic
227 username: admin
228 password: SomeSecretPassword
229 name: ansible_pol_grp_leaf
230 switch_type: leaf
231 state: absent
232 delegate_to: localhost
233
234 - name: Query Leaf Access Switch Policy Group
235 cisco.aci.aci_access_switch_policy_group:
236 host: apic
237 username: admin
238 password: SomeSecretPassword
239 name: ansible_pol_grp_leaf
240 switch_type: leaf
241 state: query
242 delegate_to: localhost
243 register: query_result
244
245 - name: Query All Leaf Access Switch Policy Groups
246 cisco.aci.aci_access_switch_policy_group:
247 host: apic
248 username: admin
249 password: SomeSecretPassword
250 switch_type: leaf
251 state: query
252 delegate_to: localhost
253 register: query_result
254 """
255
256 RETURN = r"""
257 current:
258 description: The existing configuration from the APIC after the module has finished
259 returned: success
260 type: list
261 sample:
262 [
263 {
264 "fvTenant": {
265 "attributes": {
266 "descr": "Production environment",
267 "dn": "uni/tn-production",
268 "name": "production",
269 "nameAlias": "",
270 "ownerKey": "",
271 "ownerTag": ""
272 }
273 }
274 }
275 ]
276 error:
277 description: The error information as returned from the APIC
278 returned: failure
279 type: dict
280 sample:
281 {
282 "code": "122",
283 "text": "unknown managed object class foo"
284 }
285 raw:
286 description: The raw output returned by the APIC REST API (xml or json)
287 returned: parse error
288 type: str
289 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class "/></imdata>'
290 sent:
291 description: The actual/minimal configuration pushed to the APIC
292 returned: info
293 type: list
294 sample:
295 {
296 "fvTenant": {
297 "attributes": {
298 "descr": "Production environment"
299 }
300 }
301 }
302 previous:
303 description: The original configuration from the APIC before the module has started
304 returned: info
305 type: list
306 sample:
307 [
308 {
309 "fvTenant": {
310 "attributes": {
311 "descr": "Production",
312 "dn": "uni/tn-production",
313 "name": "production",
314 "nameAlias": "",
315 "ownerKey": "",
316 "ownerTag": ""
317 }
318 }
319 }
320 ]
321 proposed:
322 description: The assembled configuration from the user-provided parameters
323 returned: info
324 type: dict
325 sample:
326 {
327 "fvTenant": {
328 "attributes": {
329 "descr": "Production environment",
330 "name": "production"
331 }
332 }
333 }
334 filter_string:
335 description: The filter string used for the request
336 returned: failure or debug
337 type: str
338 sample: ?rsp-prop-include=config-only
339 method:
340 description: The HTTP method used for the request to the APIC
341 returned: failure or debug
342 type: str
343 sample: POST
344 response:
345 description: The HTTP response from the APIC
346 returned: failure or debug
347 type: str
348 sample: OK (30 bytes)
349 status:
350 description: The HTTP status from the APIC
351 returned: failure or debug
352 type: int
353 sample: 200
354 url:
355 description: The HTTP url used for the request to the APIC
356 returned: failure or debug
357 type: str
358 sample: https://10.11.12.13/api/mo/uni/tn-production.json
359 """
360
361 from ansible.module_utils.basic import AnsibleModule
362 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
363 from ansible_collections.cisco.aci.plugins.module_utils.constants import ACI_ACCESS_SWITCH_POLICY_GROUP_CLASS_MAPPING
364
365
366 def main():
367 argument_spec = aci_argument_spec()
368 argument_spec.update(aci_annotation_spec())
369 argument_spec.update(aci_owner_spec())
370 argument_spec.update(
371 name=dict(type="str", aliases=["policy_group"]),
372 description=dict(type="str"),
373 switch_type=dict(type="str", choices=["leaf", "spine"], required=True),
374 spanning_tree_policy=dict(type="str"),
375 bfd_ipv4_policy=dict(type="str"),
376 bfd_ipv6_policy=dict(type="str"),
377 bfd_multihop_ipv4_policy=dict(type="str"),
378 bfd_multihop_ipv6_policy=dict(type="str"),
379 fibre_channel_node_policy=dict(type="str"),
380 poe_node_policy=dict(type="str"),
381 fibre_channel_san_policy=dict(type="str"),
382 monitoring_policy=dict(type="str"),
383 netflow_node_policy=dict(type="str"),
384 copp_policy=dict(type="str"),
385 forward_scale_profile_policy=dict(type="str"),
386 fast_link_failover_policy=dict(type="str"),
387 node_802_1x_authentication_policy=dict(type="str"),
388 copp_pre_filter_policy=dict(type="str"),
389 equipment_flash_policy=dict(type="str"),
390 cdp_policy=dict(type="str"),
391 lldp_policy=dict(type="str"),
392 sync_e_node_policy=dict(type="str"),
393 ptp_node_policy=dict(type="str"),
394 usb_configuration_policy=dict(type="str"),
395 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
396 )
397
398 module = AnsibleModule(
399 argument_spec=argument_spec,
400 supports_check_mode=True,
401 required_if=[
402 ["state", "absent", ["name"]],
403 ["state", "present", ["name"]],
404 ],
405 )
406 name = module.params.get("name")
407 description = module.params.get("description")
408 switch_type = module.params.get("switch_type")
409 spanning_tree_policy = module.params.get("spanning_tree_policy")
410 bfd_ipv4_policy = module.params.get("bfd_ipv4_policy")
411 bfd_ipv6_policy = module.params.get("bfd_ipv6_policy")
412 bfd_multihop_ipv4_policy = module.params.get("bfd_multihop_ipv4_policy")
413 bfd_multihop_ipv6_policy = module.params.get("bfd_multihop_ipv6_policy")
414 fibre_channel_node_policy = module.params.get("fibre_channel_node_policy")
415 poe_node_policy = module.params.get("poe_node_policy")
416 fibre_channel_san_policy = module.params.get("fibre_channel_san_policy")
417 monitoring_policy = module.params.get("monitoring_policy")
418 netflow_node_policy = module.params.get("netflow_node_policy")
419 copp_policy = module.params.get("copp_policy")
420 forward_scale_profile_policy = module.params.get("forward_scale_profile_policy")
421 fast_link_failover_policy = module.params.get("fast_link_failover_policy")
422 node_802_1x_authentication_policy = module.params.get("node_802_1x_authentication_policy")
423 copp_pre_filter_policy = module.params.get("copp_pre_filter_policy")
424 equipment_flash_policy = module.params.get("equipment_flash_policy")
425 cdp_policy = module.params.get("cdp_policy")
426 lldp_policy = module.params.get("lldp_policy")
427 sync_e_node_policy = module.params.get("sync_e_node_policy")
428 ptp_node_policy = module.params.get("ptp_node_policy")
429 usb_configuration_policy = module.params.get("usb_configuration_policy")
430 state = module.params.get("state")
431
432 aci = ACIModule(module)
433
434 if switch_type == "spine" and not all(
435 v is None
436 for v in [
437 spanning_tree_policy,
438 bfd_multihop_ipv4_policy,
439 bfd_multihop_ipv6_policy,
440 fibre_channel_node_policy,
441 poe_node_policy,
442 fibre_channel_san_policy,
443 monitoring_policy,
444 netflow_node_policy,
445 forward_scale_profile_policy,
446 fast_link_failover_policy,
447 node_802_1x_authentication_policy,
448 equipment_flash_policy,
449 sync_e_node_policy,
450 ptp_node_policy,
451 ]
452 ):
453 aci.fail_json(msg="Unsupported policy provided for spine switch type.")
454
455 class_name = ACI_ACCESS_SWITCH_POLICY_GROUP_CLASS_MAPPING.get(switch_type).get("class_name")
456
457 aci.construct_url(
458 root_class=dict(
459 aci_class=class_name,
460 aci_rn=ACI_ACCESS_SWITCH_POLICY_GROUP_CLASS_MAPPING.get(switch_type).get("rn").format(name),
461 module_object=name,
462 target_filter={"name": name},
463 ),
464 rsp_subtree="children",
465 )
466
467 aci.get_existing()
468
469 if state == "present":
470 child_configs = []
471 if forward_scale_profile_policy is not None:
472 child_configs.append({"infraRsTopoctrlFwdScaleProfPol": {"attributes": {"tnTopoctrlFwdScaleProfilePolName": forward_scale_profile_policy}}})
473 if usb_configuration_policy is not None:
474 child_configs.append(
475 {
476 ACI_ACCESS_SWITCH_POLICY_GROUP_CLASS_MAPPING.get(switch_type)
477 .get("usb_configuration_policy")
478 .get("class_name"): {
479 "attributes": {
480 ACI_ACCESS_SWITCH_POLICY_GROUP_CLASS_MAPPING.get(switch_type)
481 .get("usb_configuration_policy")
482 .get("tn_name"): usb_configuration_policy
483 }
484 }
485 }
486 )
487 if lldp_policy is not None:
488 child_configs.append(
489 {
490 ACI_ACCESS_SWITCH_POLICY_GROUP_CLASS_MAPPING.get(switch_type)
491 .get("lldp_policy")
492 .get("class_name"): {
493 "attributes": {ACI_ACCESS_SWITCH_POLICY_GROUP_CLASS_MAPPING.get(switch_type).get("lldp_policy").get("tn_name"): lldp_policy}
494 }
495 }
496 )
497 if cdp_policy is not None:
498 child_configs.append(
499 {
500 ACI_ACCESS_SWITCH_POLICY_GROUP_CLASS_MAPPING.get(switch_type)
501 .get("cdp_policy")
502 .get("class_name"): {
503 "attributes": {ACI_ACCESS_SWITCH_POLICY_GROUP_CLASS_MAPPING.get(switch_type).get("cdp_policy").get("tn_name"): cdp_policy}
504 }
505 }
506 )
507 if bfd_ipv4_policy is not None:
508 child_configs.append(
509 {
510 ACI_ACCESS_SWITCH_POLICY_GROUP_CLASS_MAPPING.get(switch_type)
511 .get("bfd_ipv4_policy")
512 .get("class_name"): {
513 "attributes": {ACI_ACCESS_SWITCH_POLICY_GROUP_CLASS_MAPPING.get(switch_type).get("bfd_ipv4_policy").get("tn_name"): bfd_ipv4_policy}
514 }
515 }
516 )
517 if bfd_ipv6_policy is not None:
518 child_configs.append(
519 {
520 ACI_ACCESS_SWITCH_POLICY_GROUP_CLASS_MAPPING.get(switch_type)
521 .get("bfd_ipv6_policy")
522 .get("class_name"): {
523 "attributes": {ACI_ACCESS_SWITCH_POLICY_GROUP_CLASS_MAPPING.get(switch_type).get("bfd_ipv6_policy").get("tn_name"): bfd_ipv6_policy}
524 }
525 }
526 )
527 if sync_e_node_policy is not None:
528 child_configs.append({"infraRsSynceInstPol": {"attributes": {"tnSynceInstPolName": sync_e_node_policy}}})
529 if poe_node_policy is not None:
530 child_configs.append({"infraRsPoeInstPol": {"attributes": {"tnPoeInstPolName": poe_node_policy}}})
531 if bfd_multihop_ipv4_policy is not None:
532 child_configs.append({"infraRsBfdMhIpv4InstPol": {"attributes": {"tnBfdMhIpv4InstPolName": bfd_multihop_ipv4_policy}}})
533 if bfd_multihop_ipv6_policy is not None:
534 child_configs.append({"infraRsBfdMhIpv6InstPol": {"attributes": {"tnBfdMhIpv6InstPolName": bfd_multihop_ipv6_policy}}})
535 if equipment_flash_policy is not None:
536 child_configs.append({"infraRsEquipmentFlashConfigPol": {"attributes": {"tnEquipmentFlashConfigPolName": equipment_flash_policy}}})
537 if monitoring_policy is not None:
538 child_configs.append({"infraRsMonNodeInfraPol": {"attributes": {"tnMonInfraPolName": monitoring_policy}}})
539 if fibre_channel_node_policy is not None:
540 child_configs.append({"infraRsFcInstPol": {"attributes": {"tnFcInstPolName": fibre_channel_node_policy}}})
541 if fast_link_failover_policy is not None:
542 child_configs.append(
543 {"infraRsTopoctrlFastLinkFailoverInstPol": {"attributes": {"tnTopoctrlFastLinkFailoverInstPolName": fast_link_failover_policy}}}
544 )
545 if spanning_tree_policy is not None:
546 child_configs.append({"infraRsMstInstPol": {"attributes": {"tnStpInstPolName": spanning_tree_policy}}})
547 if fibre_channel_san_policy is not None:
548 child_configs.append({"infraRsFcFabricPol": {"attributes": {"tnFcFabricPolName": fibre_channel_san_policy}}})
549 if copp_policy is not None:
550 child_configs.append(
551 {
552 ACI_ACCESS_SWITCH_POLICY_GROUP_CLASS_MAPPING.get(switch_type)
553 .get("copp_policy")
554 .get("class_name"): {
555 "attributes": {ACI_ACCESS_SWITCH_POLICY_GROUP_CLASS_MAPPING.get(switch_type).get("copp_policy").get("tn_name"): copp_policy}
556 }
557 }
558 )
559 if node_802_1x_authentication_policy is not None:
560 child_configs.append({"infraRsL2NodeAuthPol": {"attributes": {"tnL2NodeAuthPolName": node_802_1x_authentication_policy}}})
561 if copp_pre_filter_policy is not None:
562 child_configs.append(
563 {
564 ACI_ACCESS_SWITCH_POLICY_GROUP_CLASS_MAPPING.get(switch_type)
565 .get("copp_pre_filter_policy")
566 .get("class_name"): {
567 "attributes": {
568 ACI_ACCESS_SWITCH_POLICY_GROUP_CLASS_MAPPING.get(switch_type).get("copp_pre_filter_policy").get("tn_name"): copp_pre_filter_policy
569 }
570 }
571 }
572 )
573 if netflow_node_policy is not None:
574 child_configs.append({"infraRsNetflowNodePol": {"attributes": {"tnNetflowNodePolName": netflow_node_policy}}})
575 if ptp_node_policy is not None:
576 child_configs.append({"infraRsPtpInstPol": {"attributes": {"tnPtpInstPolName": ptp_node_policy}}})
577
578 if child_configs == []:
579 child_configs = None
580
581 aci.payload(
582 aci_class=class_name,
583 class_config=dict(
584 descr=description,
585 ),
586 child_configs=child_configs,
587 )
588
589 aci.get_diff(aci_class=class_name)
590
591 aci.post_config()
592
593 elif state == "absent":
594 aci.delete_config()
595
596 aci.exit_json()
597
598
599 if __name__ == "__main__":
600 main()