Debian Codegraph @

ansible_collections/cisco/meraki/plugins/modules/meraki_ms_access_policies.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2022
5 # Marcin Woźniak (@y0rune) <[email protected]>
6 # GNU General Public License v3.0+ (see COPYING 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": ["deprecated"],
15 "supported_by": "community",
16 }
17
18 DOCUMENTATION = r"""
19 author:
20 - "Marcin Woźniak (@y0rune)"
21 deprecated:
22 alternative: cisco.meraki.networks_switch_access_policies
23 removed_in: 3.0.0
24 why: Updated modules released with increased functionality
25 description: Module for managing a Switch Access Policies in the Meraki cloud
26 extends_documentation_fragment: cisco.meraki.meraki
27 module: meraki_ms_access_policies
28 options:
29 access_policy_type:
30 choices:
31 - 802.1x
32 - MAC authentication bypass
33 - Hybrid authentication
34 description:
35 - Set type of the access policy
36 type: str
37 auth_method:
38 choices:
39 - Meraki authentication
40 - my RADIUS server
41 description:
42 - Set authentication method in the policy.
43 type: str
44 data_vlan_id:
45 description:
46 - Set a Data VLAN ID for Critical Auth VLAN
47 type: int
48 guest_vlan:
49 description:
50 - Guest Vlan
51 type: int
52 host_mode:
53 choices:
54 - Single-Host
55 - Multi-Domain
56 - Multi-Host
57 - Multi-Auth
58 description:
59 - Choose the Host Mode for the access policy.
60 type: str
61 name:
62 description:
63 - Name of Access Policy.
64 type: str
65 net_id:
66 description:
67 - ID of network.
68 type: str
69 net_name:
70 aliases:
71 - network
72 description:
73 - Name of a network.
74 type: str
75 number:
76 aliases:
77 - access_policy_number
78 description:
79 - Number of the access_policy.
80 type: int
81 org_id:
82 description:
83 - ID of organization associated to a network.
84 type: str
85 radius_accounting_enabled:
86 description:
87 - Enable or disable RADIUS accounting.
88 type: bool
89 radius_accounting_servers:
90 description:
91 - List of RADIUS servers for RADIUS accounting.
92 elements: dict
93 suboptions:
94 host:
95 description:
96 - IP address or hostname of RADIUS server.
97 required: true
98 type: str
99 port:
100 description:
101 - Port number RADIUS server is listening to.
102 type: int
103 secret:
104 description:
105 - RADIUS password.
106 type: str
107 type: list
108 radius_attribute_group_policy_name:
109 choices:
110 - Filter-Id
111 - ''
112 default: ''
113 description:
114 - Enable that attribute for a RADIUS
115 type: str
116 radius_coa_enabled:
117 description:
118 - Enable or disable RADIUS CoA (Change of Authorization).
119 type: bool
120 radius_servers:
121 description:
122 - List of RADIUS servers.
123 elements: dict
124 suboptions:
125 host:
126 description:
127 - IP address or hostname of RADIUS server.
128 required: true
129 type: str
130 port:
131 description:
132 - Port number RADIUS server is listening to.
133 type: int
134 secret:
135 description:
136 - RADIUS password.
137 - Setting password is not idempotent.
138 type: str
139 type: list
140 radius_testing:
141 default: true
142 description:
143 - Set status of testing a radius.
144 type: bool
145 state:
146 choices:
147 - absent
148 - query
149 - present
150 default: present
151 description:
152 - Specifies whether SNMP information should be queried or modified.
153 type: str
154 suspend_port_bounce:
155 default: false
156 description:
157 - Enable or disable the Suspend Port Bounce when RADIUS servers are unreachable.
158 type: bool
159 systems_management_enrollment:
160 default: false
161 description:
162 - Set if the Systems Management Enrollemnt is enabled or disabled
163 type: bool
164 voice_vlan_clients:
165 default: true
166 description:
167 - If is enabled that means Voice VLAN client require authentication
168 type: bool
169 voice_vlan_id:
170 description:
171 - Set a Voice VLAN ID for Critical Auth VLAN
172 type: int
173 short_description: Manage Switch Access Policies in the Meraki cloud
174 """
175
176 EXAMPLES = r"""
177 - name: Create access policy with auth_method is "Meraki authentication"
178 cisco.meraki.meraki_ms_access_policies:
179 auth_key: abc123
180 state: present
181 name: Meraki authentication policy
182 auth_method: Meraki authentication
183 net_name: YourNet
184 org_name: YourOrg
185 delegate_to: localhost
186 - name: Create access policy with auth_method is "my Radius Server"
187 cisco.meraki.meraki_ms_access_policies:
188 auth_key: abc123
189 access_policy_type: 802.1x
190 host_mode: Single-Host
191 state: present
192 name: Meraki authentication policy
193 auth_method: my RADIUS server
194 radius_servers:
195 - host: 192.0.1.18
196 port: 7890
197 secret: secret123
198 net_name: YourNet
199 org_name: YourOrg
200 radius_coa_enabled: false
201 radius_accounting_enabled: false
202 guest_vlan: 10
203 voice_vlan_clients: false
204 """
205
206 RETURN = r"""
207 data:
208 description: List of Access Policies
209 returned: success
210 type: complex
211 contains:
212 number:
213 description: Number of the Access Policy
214 returned: success
215 type: int
216 sample: 1
217 name:
218 description: Name of the Access Policy
219 returned: success
220 type: str
221 sample: Policy with 802.1x
222 access_policy_type:
223 description: Type of the access policy
224 returned: success
225 type: str
226 sample: 802.1x
227 guest_vlan_id:
228 description: ID of the Guest Vlan
229 returned: success
230 type: int
231 sample: 10
232 host_mode:
233 description: Choosen teh Host Mode for the access policy
234 returned: success
235 type: str
236 sample: Single-Host
237 radius:
238 description: List of radius specific list
239 returned: success
240 type: complex
241 contains:
242 critial_auth:
243 description: Critial Auth List
244 returned: success
245 type: complex
246 contains:
247 data_vlan_id:
248 description: VLAN ID for data
249 returned: success
250 type: int
251 sample: 10
252 suspend_port_bounce:
253 description: Enable or disable suspend port bounce
254 returned: success
255 type: bool
256 sample: false
257 voice_vlan_id:
258 description: VLAN ID for voice
259 returned: success
260 type: int
261 sample: 10
262 failed_auth_vlan_id:
263 description: VLAN ID when failed auth
264 returned: success
265 type: int
266 sample: 11
267 re_authentication_interval:
268 description: Interval of re-authentication
269 returned: success
270 type: int
271 sample:
272 radius_coa_enabled:
273 description:
274 - Enable or disable RADIUS CoA (Change of Authorization).
275 type: bool
276 radius_accounting_enabled:
277 description:
278 - Enable or disable RADIUS accounting.
279 type: bool
280 radius_accounting_servers:
281 description:
282 - List of RADIUS servers for RADIUS accounting.
283 type: list
284 elements: dict
285 radius_servers:
286 description:
287 - List of RADIUS servers.
288 type: list
289 elements: dict
290 radius_attribute_group_policy_name:
291 description: Enable the radius group attribute
292 returned: success
293 type: str
294 choices: [ "11", ""]
295 sample: 11
296 radius_testing_enabled:
297 description: Enable or disable Radius Testing
298 returned: success
299 type: bool
300 sample: true
301 voice_vlan_clients:
302 description: Enable or disable Voice Vlan Clients
303 returned: success
304 type: bool
305 sample: false
306 """
307
308 from ansible.module_utils.basic import AnsibleModule, json
309 from ansible_collections.cisco.meraki.plugins.module_utils.network.meraki.meraki import (
310 MerakiModule,
311 meraki_argument_spec,
312 )
313
314
315 def convert_vlan_id(vlan_id):
316 if vlan_id == "":
317 return None
318 elif vlan_id == 0:
319 return None
320 elif vlan_id in range(1, 4094):
321 return vlan_id
322
323
324 def convert_radius_attribute_group_policy_name(arg):
325 if arg == "Filter-Id":
326 return 11
327 else:
328 return ""
329
330
331 def main():
332 argument_spec = meraki_argument_spec()
333
334 radius_arg_spec = dict(
335 host=dict(type="str", required=True),
336 port=dict(type="int"),
337 secret=dict(type="str", no_log=True),
338 )
339
340 argument_spec.update(
341 state=dict(
342 type="str",
343 choices=["present", "query", "absent"],
344 default="present",
345 ),
346 net_id=dict(type="str"),
347 net_name=dict(type="str", aliases=["network"]),
348 number=dict(type="int", aliases=["access_policy_number"]),
349 name=dict(type="str"),
350 auth_method=dict(
351 type="str",
352 choices=["Meraki authentication", "my RADIUS server"],
353 ),
354 guest_vlan=dict(type="int"),
355 access_policy_type=dict(
356 type="str",
357 choices=[
358 "802.1x",
359 "MAC authentication bypass",
360 "Hybrid authentication",
361 ],
362 ),
363 systems_management_enrollment=dict(type="bool", default=False),
364 radius_servers=dict(
365 type="list", default=None, elements="dict", options=radius_arg_spec
366 ),
367 radius_testing=dict(type="bool", default="True"),
368 voice_vlan_clients=dict(type="bool", default="True"),
369 radius_coa_enabled=dict(type="bool"),
370 radius_accounting_enabled=dict(type="bool"),
371 radius_accounting_servers=dict(
372 type="list", elements="dict", options=radius_arg_spec
373 ),
374 host_mode=dict(
375 type="str",
376 choices=["Single-Host", "Multi-Domain", "Multi-Host", "Multi-Auth"],
377 ),
378 data_vlan_id=dict(type="int"),
379 voice_vlan_id=dict(type="int"),
380 suspend_port_bounce=dict(type="bool", default="False"),
381 radius_attribute_group_policy_name=dict(
382 type="str", choices=["Filter-Id", ""], default=""
383 ),
384 )
385
386 module = AnsibleModule(
387 argument_spec=argument_spec,
388 supports_check_mode=True,
389 )
390 meraki = MerakiModule(module, function="access_policies")
391
392 net_id = meraki.params["net_id"]
393 net_name = meraki.params["net_name"]
394
395 org_id = meraki.params["org_id"]
396 org_name = meraki.params["org_name"]
397
398 if meraki.params["net_name"] and meraki.params["net_id"]:
399 meraki.fail_json(msg="net_name and net_id are mutually exclusive")
400
401 if meraki.params["org_name"] and meraki.params["org_id"]:
402 meraki.fail_json(msg="org_name and org_id are mutually exclusive")
403
404 if net_id or net_name:
405 if net_id is None:
406 if org_id is None:
407 org_id = meraki.get_org_id(org_name)
408 nets = meraki.get_nets(org_id=org_id)
409 net_id = meraki.get_net_id(net_name=net_name, data=nets)
410
411 query_urls = {"access_policies": "/networks/{net_id}/switch/accessPolicies"}
412 query_url = {
413 "access_policies": "/networks/{net_id}/switch/accessPolicies/{number}"
414 }
415 update_url = {
416 "access_policies": "/networks/{net_id}/switch/accessPolicies/{number}"
417 }
418 create_url = {"access_policies": "/networks/{net_id}/switch/accessPolicies"}
419
420 meraki.url_catalog["get_all"].update(query_urls)
421 meraki.url_catalog["get_one"].update(query_url)
422 meraki.url_catalog["update"] = update_url
423 meraki.url_catalog["create"] = create_url
424
425 payload_auth = {
426 "name": meraki.params["name"],
427 "radiusServers": [],
428 "radiusTestingEnabled": False,
429 "radiusGroupAttribute": meraki.params[
430 "radius_attribute_group_policy_name"
431 ],
432 "radius": {
433 "criticalAuth": {
434 "dataVlanId": None,
435 "voiceVlanId": None,
436 "suspendPortBounce": False,
437 },
438 "failedAuthVlanId": None,
439 "reAuthenticationInterval": None,
440 },
441 "radiusCoaSupportEnabled": False,
442 "radiusAccountingEnabled": False,
443 "radiusAccountingServers": [],
444 "hostMode": "Single-Host",
445 "accessPolicyType": "802.1x",
446 "voiceVlanClients": True,
447 "systems_management_enrollment": meraki.params[
448 "systems_management_enrollment"
449 ],
450 "guestVlanId": meraki.params["guest_vlan"],
451 "urlRedirectWalledGardenEnabled": False,
452 }
453
454 payload_radius = {
455 "name": meraki.params["name"],
456 "radiusServers": meraki.params["radius_servers"],
457 "radiusTestingEnabled": meraki.params["radius_testing"],
458 "radiusGroupAttribute": convert_radius_attribute_group_policy_name(
459 meraki.params["radius_attribute_group_policy_name"]
460 ),
461 "radius": {
462 "criticalAuth": {
463 "dataVlanId": convert_vlan_id(meraki.params["data_vlan_id"]),
464 "voiceVlanId": convert_vlan_id(meraki.params["voice_vlan_id"]),
465 "suspendPortBounce": meraki.params["suspend_port_bounce"],
466 },
467 "failedAuthVlanId": None,
468 "reAuthenticationInterval": None,
469 },
470 "radiusCoaSupportEnabled": meraki.params["radius_coa_enabled"],
471 "hostMode": meraki.params["host_mode"],
472 "accessPolicyType": meraki.params["access_policy_type"],
473 "guestVlanId": meraki.params["guest_vlan"],
474 "voiceVlanClients": meraki.params["voice_vlan_clients"],
475 "urlRedirectWalledGardenEnabled": False,
476 "radiusAccountingEnabled": meraki.params["radius_accounting_enabled"],
477 "radiusAccountingServers": meraki.params["radius_accounting_servers"],
478 "systems_management_enrollment": meraki.params[
479 "systems_management_enrollment"
480 ],
481 }
482
483 if meraki.params["state"] == "query":
484 if meraki.params["number"]:
485 path = meraki.construct_path(
486 "get_one",
487 net_id=net_id,
488 custom={
489 "number": meraki.params["number"],
490 },
491 )
492 response = meraki.request(path, method="GET")
493 meraki.result["data"] = response
494 else:
495 path = meraki.construct_path("get_all", net_id=net_id)
496 response = meraki.request(path, method="GET")
497 meraki.result["data"] = response
498 elif meraki.params["state"] == "present":
499
500 query_path_all = meraki.construct_path(
501 "get_all",
502 net_id=net_id,
503 )
504
505 original_all = meraki.request(query_path_all, method="GET")
506
507 for i in original_all:
508 if i.get("name") == meraki.params["name"]:
509 meraki.params["number"] = i.get("accessPolicyNumber")
510
511 if meraki.params["number"] is None:
512 path = meraki.construct_path(
513 "create",
514 net_id=net_id,
515 )
516 if meraki.params["auth_method"] == "Meraki authentication":
517 response = meraki.request(
518 path, method="POST", payload=json.dumps(payload_auth)
519 )
520 meraki.result["changed"] = True
521 meraki.result["data"] = response
522 elif meraki.params["auth_method"] == "my RADIUS server":
523 response = meraki.request(
524 path, method="POST", payload=json.dumps(payload_radius)
525 )
526 meraki.result["changed"] = True
527 meraki.result["data"] = response
528 else:
529 query_path = meraki.construct_path(
530 "get_one",
531 net_id=net_id,
532 custom={
533 "number": meraki.params["number"],
534 },
535 )
536
537 update_path = meraki.construct_path(
538 "update",
539 net_id=net_id,
540 custom={
541 "number": meraki.params["number"],
542 },
543 )
544
545 proposed = ""
546
547 if meraki.params["auth_method"] == "Meraki authentication":
548 proposed = payload_auth.copy()
549 elif meraki.params["auth_method"] == "my RADIUS server":
550 proposed = payload_radius.copy()
551
552 original = meraki.request(query_path, method="GET")
553
554 ignored_parameters = [
555 "accessPolicyNumber",
556 "secret",
557 "systems_management_enrollment",
558 ]
559
560 if meraki.params["radius_accounting_enabled"]:
561 proposed.update(
562 {
563 "radiusAccountingServers": meraki.params[
564 "radius_accounting_servers"
565 ],
566 }
567 )
568 else:
569 proposed.update(
570 {
571 "radiusAccountingServers": [],
572 }
573 )
574
575 if meraki.params["radius_servers"]:
576 proposed.update(
577 {
578 "radiusServers": meraki.params["radius_servers"],
579 }
580 )
581 else:
582 proposed.update(
583 {
584 "radiusServers": [],
585 }
586 )
587
588 if meraki.is_update_required(
589 original,
590 proposed,
591 optional_ignore=ignored_parameters,
592 ):
593
594 if meraki.check_mode is True:
595 original.update(proposed)
596 meraki.result["data"] = original
597 meraki.result["changed"] = True
598 meraki.exit_json(**meraki.result)
599
600 response = meraki.request(
601 update_path, method="PUT", payload=json.dumps(proposed)
602 )
603 meraki.result["changed"] = True
604 meraki.result["data"] = response
605 else:
606 meraki.result["data"] = original
607
608 elif meraki.params["state"] == "absent":
609 path = meraki.construct_path(
610 "update",
611 net_id=net_id,
612 custom={
613 "number": meraki.params["number"],
614 },
615 )
616
617 response = meraki.request(path, method="DELETE")
618 meraki.result["changed"] = True
619 meraki.result["data"] = response
620
621 meraki.exit_json(**meraki.result)
622
623
624 if __name__ == "__main__":
625 main()