Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2024, Akini Ross (@akinross) <[email protected]>
5 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "certified"}
12
13 DOCUMENTATION = r"""
14 ---
15 module: aci_fabric_management_access
16 short_description: Manage Fabric Management Access (comm:Pol)
17 description:
18 - Manage Fabric Management Access on Cisco ACI fabrics.
19 options:
20 name:
21 description:
22 - The name of the Fabric Management Access policy.
23 type: str
24 aliases: [ fabric_management_access_policy_name ]
25 description:
26 description:
27 - The description of the Fabric Management Access policy.
28 type: str
29 aliases: [ descr ]
30 name_alias:
31 description:
32 - The name alias of the Fabric Management Access policy.
33 - This relates to the nameAlias property in ACI.
34 type: str
35 http:
36 description:
37 - Parameters for HTTP configuration (comm:Http).
38 type: dict
39 suboptions:
40 admin_state:
41 description:
42 - The admin state of the HTTP connection.
43 - The APIC defaults to C(disabled) when unset during creation.
44 type: str
45 choices: [ enabled, disabled ]
46 port:
47 description:
48 - The port for the HTTP connection.
49 - The APIC defaults to C(80) when unset during creation.
50 type: int
51 redirect:
52 description:
53 - The state of the HTTP to HTTPS redirect service.
54 - The APIC defaults to C(disabled) when unset during creation.
55 type: str
56 choices: [ enabled, disabled, tested ]
57 allow_origins:
58 description:
59 - The allowed origins for the HTTP connection.
60 - 'Example format: http://127.0.0.1:8000'
61 type: str
62 allow_credentials:
63 description:
64 - The state of the allow credential for the HTTP connection.
65 - The APIC defaults to C(disabled) when unset during creation.
66 type: str
67 choices: [ enabled, disabled ]
68 throttle:
69 description:
70 - The state of the request throttle for the HTTP connection.
71 - The APIC defaults to C(disabled) when unset during creation.
72 type: str
73 choices: [ enabled, disabled ]
74 throttle_rate:
75 description:
76 - The rate of the request throttle.
77 - The APIC defaults to C(10000) when unset during creation.
78 type: int
79 throttle_unit:
80 description:
81 - The unit of the request throttle rate.
82 - The APIC defaults to C(requests_per_second) when unset during creation.
83 type: str
84 choices: [ requests_per_second, requests_per_minute ]
85 https:
86 description:
87 - Parameters for HTTPS configuration (comm:Https).
88 type: dict
89 suboptions:
90 admin_state:
91 description:
92 - The admin state of the HTTPS connection.
93 - The APIC defaults to C(enabled) when unset during creation.
94 type: str
95 choices: [ enabled, disabled ]
96 port:
97 description:
98 - The port for the HTTPS connection.
99 - The APIC defaults to C(443) when unset during creation.
100 type: int
101 allow_origins:
102 description:
103 - The allowed origins for the HTTPS connection.
104 - 'Example format: http://127.0.0.1:8000'
105 type: str
106 allow_credentials:
107 description:
108 - The state of the allow credential for the HTTPS connection.
109 - The APIC defaults to C(disabled) when unset during creation.
110 type: str
111 choices: [ enabled, disabled ]
112 ssl:
113 description:
114 - The SSL protocol(s) for the HTTPS connection.
115 - The APIC defaults to C(tls_v1.1) and C(tls_v1.2) set when unset during creation.
116 type: list
117 elements: str
118 choices: [ tls_v1.0, tls_v1.1, tls_v1.2, tls_v1.3 ]
119 aliases: [ ssl_protocols ]
120 dh_param:
121 description:
122 - The Diffie-Hellman parameter for the HTTPS connection.
123 - The APIC defaults to C(none) when unset during creation.
124 type: str
125 choices: [ '1024', '2048', '4096', none ]
126 throttle:
127 description:
128 - The state of the request throttle for the HTTPS connection.
129 - The APIC defaults to C(disabled) when unset during creation.
130 type: str
131 choices: [ enabled, disabled ]
132 throttle_rate:
133 description:
134 - The rate of the request throttle.
135 - The APIC defaults to C(10000) when unset during creation.
136 type: int
137 throttle_unit:
138 description:
139 - The unit of the request throttle rate.
140 - The APIC defaults to C(requests_per_second) when unset during creation.
141 type: str
142 choices: [ requests_per_second, requests_per_minute ]
143 admin_key_ring:
144 description:
145 - The admin key ring for the HTTPS connection.
146 - The APIC defaults to C(default) when unset during creation.
147 type: str
148 client_certificate_trustpoint:
149 description:
150 - The client certificate trustpoint for the HTTPS connection.
151 type: str
152 aliases: [ trustpoint ]
153 client_certificate_authentication_state:
154 description:
155 - The client certificate authentication state for the HTTPS connection.
156 - The APIC defaults to C(disabled) when unset during creation.
157 - The C(enabled) state requires a C(client_certificate_trustpoint) to be set.
158 type: str
159 choices: [ enabled, disabled ]
160 aliases: [ client_certificate_auth_state, auth_state, authentication_state ]
161 telnet:
162 description:
163 - Parameters for telnet configuration (comm:Telnet).
164 type: dict
165 suboptions:
166 admin_state:
167 description:
168 - The admin state of the telnet connection.
169 - The APIC defaults to C(disabled) when unset during creation.
170 type: str
171 choices: [ enabled, disabled ]
172 port:
173 description:
174 - The port for the telnet connection.
175 - The APIC defaults to C(23) when unset during creation.
176 type: int
177 ssh:
178 description:
179 - Parameters for SSH configuration (comm:Ssh).
180 type: dict
181 suboptions:
182 admin_state:
183 description:
184 - The admin state of the SSH connection.
185 - The APIC defaults to C(enabled) when unset during creation.
186 type: str
187 choices: [ enabled, disabled ]
188 password_auth_state:
189 description:
190 - The password authentication state of the SSH connection.
191 - The APIC defaults to C(enabled) when unset during creation.
192 type: str
193 choices: [ enabled, disabled ]
194 port:
195 description:
196 - The port for the SSH connection.
197 - The APIC defaults to C(22) when unset during creation.
198 type: int
199 ciphers:
200 description:
201 - The ciphers of the SSH connection.
202 - The APIC defaults to all options set when unset during creation.
203 type: list
204 elements: str
205 choices: [ aes128_ctr, aes192_ctr, aes256_ctr, aes128_gcm, aes256_gcm, chacha20 ]
206 kex:
207 description:
208 - The KEX algorithms of the SSH connection.
209 - The APIC defaults to all options set when unset during creation.
210 type: list
211 elements: str
212 choices: [ dh_sha1, dh_sha256, dh_sha512, curve_sha256, curve_sha256_libssh, ecdh_256, ecdh_384, ecdh_521 ]
213 macs:
214 description:
215 - The MACs of the SSH connection.
216 - The APIC defaults to all options set when unset during creation.
217 type: list
218 elements: str
219 choices: [ sha1, sha2_256, sha2_512, sha2_256_etm, sha2_512_etm ]
220 ssh_web:
221 description:
222 - Parameters for SSH access via WEB configuration (comm:Shellinabox).
223 type: dict
224 suboptions:
225 admin_state:
226 description:
227 - The admin state of the SSH access via WEB connection.
228 - The APIC defaults to C(disabled) when unset during creation.
229 type: str
230 choices: [ enabled, disabled ]
231 state:
232 description:
233 - Use C(present) for updating configuration.
234 - Use C(query) for showing current configuration.
235 type: str
236 choices: [ absent, present, query ]
237 default: present
238 extends_documentation_fragment:
239 - cisco.aci.aci
240 - cisco.aci.annotation
241 - cisco.aci.owner
242
243 seealso:
244 - name: APIC Management Information Model reference
245 description: More information about the internal APIC class B(comm:Pol).
246 link: https://developer.cisco.com/docs/apic-mim-ref/
247 author:
248 - Akini Ross (@akinross)
249 """
250
251 EXAMPLES = r"""
252 - name: Create a Fabric Management Access policy
253 cisco.aci.aci_fabric_management_access:
254 host: apic
255 username: admin
256 password: SomeSecretPassword
257 name: fabric_management_access_policy_1
258 description: "This is a example Fabric Management Access policy."
259 state: present
260 delegate_to: localhost
261
262 - name: Create a Fabric Management Access policy with telnet enabled
263 cisco.aci.aci_fabric_management_access:
264 host: apic
265 username: admin
266 password: SomeSecretPassword
267 name: fabric_management_access_policy_1
268 description: "This is a example Fabric Management Access policy."
269 telnet:
270 admin_state: enabled
271 state: present
272 delegate_to: localhost
273
274 - name: Create a Fabric Management Access policy with SSH access via WEB enabled
275 cisco.aci.aci_fabric_management_access:
276 host: apic
277 username: admin
278 password: SomeSecretPassword
279 name: fabric_management_access_policy_1
280 description: "This is a example Fabric Management Access policy."
281 ssh_web:
282 admin_state: enabled
283 state: present
284 delegate_to: localhost
285
286 - name: Create a Fabric Management Access policy with SSH enabled and ciphers set
287 cisco.aci.aci_fabric_management_access:
288 host: apic
289 username: admin
290 password: SomeSecretPassword
291 name: fabric_management_access_policy_1
292 description: "This is a example Fabric Management Access policy."
293 ssh:
294 admin_state: enabled
295 ciphers:
296 - aes128_ctr
297 - aes192_ctr
298 - aes256_ctr
299 state: present
300 delegate_to: localhost
301
302 - name: Create a Fabric Management Access policy with HTTP enabled
303 cisco.aci.aci_fabric_management_access:
304 host: apic
305 username: admin
306 password: SomeSecretPassword
307 name: fabric_management_access_policy_1
308 description: "This is a example Fabric Management Access policy."
309 http:
310 admin_state: enabled
311 allow_origins: http://127.0.0.1:8000
312 throttle: enabled
313 throttle_rate: 7500
314 throttle_unit: requests_per_minute
315 state: present
316 delegate_to: localhost
317
318 - name: Create a Fabric Management Access policy with HTTPS enabled
319 cisco.aci.aci_fabric_management_access:
320 host: apic
321 username: admin
322 password: SomeSecretPassword
323 name: fabric_management_access_policy_1
324 description: "This is a example Fabric Management Access policy."
325 https:
326 admin_state: enabled
327 port: 445
328 allow_origins: http://127.0.0.1:8000
329 allow_credentials: enabled
330 ssl:
331 - tls_v1.2
332 dh_param: 4096
333 throttle: enabled
334 throttle_rate: 7500
335 throttle_unit: requests_per_minute
336 admin_key_ring: default
337 client_certificate_trustpoint: ansible_trustpoint
338 client_certificate_authentication_state: enabled
339 state: present
340 delegate_to: localhost
341
342 - name: Query a Fabric Management Access policy
343 cisco.aci.aci_fabric_management_access:
344 host: apic
345 username: admin
346 password: SomeSecretPassword
347 name: fabric_management_access_policy_1
348 state: query
349 delegate_to: localhost
350 register: query_result
351
352 - name: Query all Fabric Management Access policies
353 cisco.aci.aci_fabric_management_access:
354 host: apic
355 username: admin
356 password: SomeSecretPassword
357 state: query
358 delegate_to: localhost
359 register: query_result
360
361 - name: Delete a Fabric Management Access policy
362 cisco.aci.aci_fabric_management_access:
363 host: apic
364 username: admin
365 password: SomeSecretPassword
366 name: fabric_management_access_policy_1
367 state: absent
368 delegate_to: localhost
369 """
370
371 RETURN = r"""
372 current:
373 description: The existing configuration from the APIC after the module has finished
374 returned: success
375 type: list
376 sample:
377 [
378 {
379 "fvTenant": {
380 "attributes": {
381 "descr": "Production environment",
382 "dn": "uni/tn-production",
383 "name": "production",
384 "nameAlias": "",
385 "ownerKey": "",
386 "ownerTag": ""
387 }
388 }
389 }
390 ]
391 error:
392 description: The error information as returned from the APIC
393 returned: failure
394 type: dict
395 sample:
396 {
397 "code": "122",
398 "text": "unknown managed object class foo"
399 }
400 raw:
401 description: The raw output returned by the APIC REST API (xml or json)
402 returned: parse error
403 type: str
404 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
405 sent:
406 description: The actual/minimal configuration pushed to the APIC
407 returned: info
408 type: list
409 sample:
410 {
411 "fvTenant": {
412 "attributes": {
413 "descr": "Production environment"
414 }
415 }
416 }
417 previous:
418 description: The original configuration from the APIC before the module has started
419 returned: info
420 type: list
421 sample:
422 [
423 {
424 "fvTenant": {
425 "attributes": {
426 "descr": "Production",
427 "dn": "uni/tn-production",
428 "name": "production",
429 "nameAlias": "",
430 "ownerKey": "",
431 "ownerTag": ""
432 }
433 }
434 }
435 ]
436 proposed:
437 description: The assembled configuration from the user-provided parameters
438 returned: info
439 type: dict
440 sample:
441 {
442 "fvTenant": {
443 "attributes": {
444 "descr": "Production environment",
445 "name": "production"
446 }
447 }
448 }
449 filter_string:
450 description: The filter string used for the request
451 returned: failure or debug
452 type: str
453 sample: ?rsp-prop-include=config-only
454 method:
455 description: The HTTP method used for the request to the APIC
456 returned: failure or debug
457 type: str
458 sample: POST
459 response:
460 description: The HTTP response from the APIC
461 returned: failure or debug
462 type: str
463 sample: OK (30 bytes)
464 status:
465 description: The HTTP status from the APIC
466 returned: failure or debug
467 type: int
468 sample: 200
469 url:
470 description: The HTTP url used for the request to the APIC
471 returned: failure or debug
472 type: str
473 sample: https://10.11.12.13/api/mo/uni/tn-production.json
474 """
475
476 from ansible.module_utils.basic import AnsibleModule
477 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
478 from ansible_collections.cisco.aci.plugins.module_utils.constants import THROTTLE_UNIT, SSH_CIPHERS, KEX_ALGORITHMS, SSH_MACS, HTTP_TLS_MAPPING
479
480
481 def main():
482 argument_spec = aci_argument_spec()
483 argument_spec.update(aci_annotation_spec())
484 argument_spec.update(aci_owner_spec())
485 argument_spec.update(
486 name=dict(type="str", aliases=["fabric_management_access_policy_name"]), # Not required for querying all objects
487 description=dict(type="str", aliases=["descr"]),
488 name_alias=dict(type="str"),
489 http=dict(
490 type="dict",
491 options=dict(
492 admin_state=dict(type="str", choices=["enabled", "disabled"]),
493 port=dict(type="int"),
494 redirect=dict(type="str", choices=["enabled", "disabled", "tested"]),
495 allow_origins=dict(type="str"),
496 allow_credentials=dict(type="str", choices=["enabled", "disabled"]),
497 throttle=dict(type="str", choices=["enabled", "disabled"]),
498 throttle_rate=dict(type="int"),
499 throttle_unit=dict(type="str", choices=["requests_per_second", "requests_per_minute"]),
500 ),
501 ),
502 https=dict(
503 type="dict",
504 options=dict(
505 admin_state=dict(type="str", choices=["enabled", "disabled"]),
506 port=dict(type="int"),
507 allow_origins=dict(type="str"),
508 allow_credentials=dict(type="str", choices=["enabled", "disabled"]),
509 ssl=dict(
510 type="list",
511 elements="str",
512 choices=list(HTTP_TLS_MAPPING.keys()),
513 aliases=["ssl_protocols"],
514 ),
515 dh_param=dict(type="str", choices=["1024", "2048", "4096", "none"]),
516 throttle=dict(type="str", choices=["enabled", "disabled"]),
517 throttle_rate=dict(type="int"),
518 throttle_unit=dict(type="str", choices=["requests_per_second", "requests_per_minute"]),
519 admin_key_ring=dict(type="str", no_log=False),
520 client_certificate_trustpoint=dict(type="str", aliases=["trustpoint"]),
521 client_certificate_authentication_state=dict(
522 type="str",
523 choices=["enabled", "disabled"],
524 aliases=["client_certificate_auth_state", "auth_state", "authentication_state"],
525 ),
526 ),
527 ),
528 telnet=dict(
529 type="dict",
530 options=dict(
531 admin_state=dict(type="str", choices=["enabled", "disabled"]),
532 port=dict(type="int"),
533 ),
534 ),
535 ssh=dict(
536 type="dict",
537 options=dict(
538 admin_state=dict(type="str", choices=["enabled", "disabled"]),
539 password_auth_state=dict(type="str", choices=["enabled", "disabled"]),
540 port=dict(type="int"),
541 ciphers=dict(type="list", elements="str", choices=list(SSH_CIPHERS.keys())),
542 kex=dict(type="list", elements="str", choices=list(KEX_ALGORITHMS.keys())),
543 macs=dict(type="list", elements="str", choices=list(SSH_MACS.keys())),
544 ),
545 ),
546 ssh_web=dict(
547 type="dict",
548 options=dict(
549 admin_state=dict(type="str", choices=["enabled", "disabled"]),
550 ),
551 ),
552 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
553 )
554
555 module = AnsibleModule(
556 argument_spec=argument_spec,
557 supports_check_mode=True,
558 required_if=[
559 ["state", "present", ["name"]],
560 ["state", "absent", ["name"]],
561 ],
562 )
563
564 aci = ACIModule(module)
565 aci_class = "commPol"
566 aci_child_classes = ["commSsh", "commHttp", "commHttps", "commTelnet", "commShellinabox"]
567
568 name = module.params.get("name")
569 description = module.params.get("description")
570 name_alias = module.params.get("name_alias")
571 http = module.params.get("http")
572 https = module.params.get("https")
573 telnet = module.params.get("telnet")
574 ssh = module.params.get("ssh")
575 ssh_web = module.params.get("ssh_web")
576 state = module.params.get("state")
577
578 aci.construct_url(
579 root_class=dict(
580 aci_class=aci_class,
581 aci_rn="fabric/comm-{0}".format(name),
582 module_object=name,
583 target_filter={"name": name},
584 ),
585 child_classes=aci_child_classes,
586 )
587
588 aci.get_existing()
589
590 if state == "present":
591 child_configs = []
592
593 if ssh:
594 child_configs.append(
595 dict(
596 commSsh=dict(
597 attributes=dict(
598 adminSt=ssh.get("admin_state"),
599 passwordAuth=ssh.get("password_auth_state"),
600 port=ssh.get("port"),
601 sshCiphers=",".join(sorted(SSH_CIPHERS.get(v) for v in set(ssh.get("ciphers")))) if ssh.get("ciphers") else None,
602 kexAlgos=",".join(sorted(KEX_ALGORITHMS.get(v) for v in set(ssh.get("kex")))) if ssh.get("kex") else None,
603 sshMacs=",".join(sorted(SSH_MACS.get(v) for v in set(ssh.get("macs")))) if ssh.get("macs") else None,
604 )
605 )
606 )
607 )
608
609 if http:
610 child_configs.append(
611 dict(
612 commHttp=dict(
613 attributes=dict(
614 adminSt=http.get("admin_state"),
615 port=http.get("port"),
616 redirectSt=http.get("redirect"),
617 accessControlAllowOrigins=http.get("allow_origins"),
618 accessControlAllowCredential=http.get("allow_credentials"),
619 globalThrottleSt=http.get("throttle"),
620 globalThrottleRate=http.get("throttle_rate"),
621 globalThrottleUnit=THROTTLE_UNIT.get(http.get("throttle_unit")),
622 )
623 )
624 )
625 )
626
627 if https:
628 https_config = dict(
629 commHttps=dict(
630 attributes=dict(
631 adminSt=https.get("admin_state"),
632 port=https.get("port"),
633 accessControlAllowOrigins=https.get("allow_origins"),
634 accessControlAllowCredential=https.get("allow_credentials"),
635 sslProtocols=",".join(sorted(HTTP_TLS_MAPPING.get(v) for v in set(https.get("ssl")))) if https.get("ssl") else None,
636 dhParam=https.get("dh_param"),
637 globalThrottleSt=https.get("throttle"),
638 globalThrottleRate=https.get("throttle_rate"),
639 globalThrottleUnit=THROTTLE_UNIT.get(https.get("throttle_unit")),
640 clientCertAuthState=https.get("client_certificate_authentication_state"),
641 ),
642 children=[],
643 )
644 )
645
646 if https.get("admin_key_ring"):
647 https_config["commHttps"]["children"].append(dict(commRsKeyRing=dict(attributes=dict(tnPkiKeyRingName=https.get("admin_key_ring")))))
648
649 if https.get("client_certificate_trustpoint"):
650 https_config["commHttps"]["children"].append(
651 dict(commRsClientCertCA=dict(attributes=dict(tDn="uni/userext/pkiext/tp-{0}".format(https.get("client_certificate_trustpoint")))))
652 )
653
654 child_configs.append(https_config)
655
656 if telnet:
657 child_configs.append(
658 dict(
659 commTelnet=dict(
660 attributes=dict(
661 adminSt=telnet.get("admin_state"),
662 port=telnet.get("port"),
663 )
664 )
665 )
666 )
667
668 if ssh_web:
669 child_configs.append(
670 dict(
671 commShellinabox=dict(
672 attributes=dict(
673 adminSt=ssh_web.get("admin_state"),
674 )
675 )
676 )
677 )
678
679 aci.payload(
680 aci_class=aci_class,
681 class_config=dict(
682 name=name,
683 descr=description,
684 nameAlias=name_alias,
685 ),
686 child_configs=child_configs,
687 )
688
689 aci.get_diff(aci_class=aci_class)
690
691 aci.post_config()
692
693 elif state == "absent":
694 aci.delete_config()
695
696 aci.exit_json()
697
698
699 if __name__ == "__main__":
700 main()