Debian Codegraph @

ansible_collections/fortinet/fortios/plugins/modules/fortios_router_setting.py

raw symbols SCIP
1 #!/usr/bin/python
2 from __future__ import absolute_import, division, print_function
3
4 # Copyright: (c) 2022 Fortinet
5 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 __metaclass__ = type
13
14 ANSIBLE_METADATA = {
15 "status": ["preview"],
16 "supported_by": "community",
17 "metadata_version": "1.1",
18 }
19
20 DOCUMENTATION = """
21 ---
22 module: fortios_router_setting
23 short_description: Configure router settings in Fortinet's FortiOS and FortiGate.
24 description:
25 - This module is able to configure a FortiGate or FortiOS (FOS) device by allowing the
26 user to set and modify router feature and setting category.
27 Examples include all parameters and values need to be adjusted to datasources before usage.
28 Tested with FOS v6.0.0
29 version_added: "2.0.0"
30 author:
31 - Link Zheng (@chillancezen)
32 - Jie Xue (@JieX19)
33 - Hongbin Lu (@fgtdev-hblu)
34 - Frank Shen (@frankshen01)
35 - Miguel Angel Munoz (@mamunozgonzalez)
36 - Nicolas Thomas (@thomnico)
37 notes:
38 - Legacy fortiosapi has been deprecated, httpapi is the preferred way to run playbooks
39
40 - The module supports check_mode.
41
42 requirements:
43 - ansible>=2.16
44 options:
45 access_token:
46 description:
47 - Token-based authentication.
48 Generated from GUI of Fortigate.
49 type: str
50 required: false
51 enable_log:
52 description:
53 - Enable/Disable logging for task.
54 type: bool
55 required: false
56 default: false
57 vdom:
58 description:
59 - Virtual domain, among those defined previously. A vdom is a
60 virtual instance of the FortiGate that can be configured and
61 used as a different unit.
62 type: str
63 default: root
64 member_path:
65 type: str
66 description:
67 - Member attribute path to operate on.
68 - Delimited by a slash character if there are more than one attribute.
69 - Parameter marked with member_path is legitimate for doing member operation.
70 member_state:
71 type: str
72 description:
73 - Add or delete a member under specified attribute path.
74 - When member_state is specified, the state option is ignored.
75 choices:
76 - 'present'
77 - 'absent'
78
79 router_setting:
80 description:
81 - Configure router settings.
82 default: null
83 type: dict
84 suboptions:
85 bgp_debug_flags:
86 description:
87 - bgp_debug_flags
88 type: str
89 hostname:
90 description:
91 - Hostname for this virtual domain router.
92 type: str
93 igmp_debug_flags:
94 description:
95 - igmp_debug_flags
96 type: str
97 imi_debug_flags:
98 description:
99 - imi_debug_flags
100 type: str
101 isis_debug_flags:
102 description:
103 - isis_debug_flags
104 type: str
105 kernel_route_distance:
106 description:
107 - Administrative distance for routes learned from kernel (0 - 255).
108 type: int
109 ospf_debug_events_flags:
110 description:
111 - ospf_debug_events_flags
112 type: str
113 ospf_debug_ifsm_flags:
114 description:
115 - ospf_debug_ifsm_flags
116 type: str
117 ospf_debug_lsa_flags:
118 description:
119 - ospf_debug_lsa_flags
120 type: str
121 ospf_debug_nfsm_flags:
122 description:
123 - ospf_debug_nfsm_flags
124 type: str
125 ospf_debug_nsm_flags:
126 description:
127 - ospf_debug_nsm_flags
128 type: str
129 ospf_debug_packet_flags:
130 description:
131 - ospf_debug_packet_flags
132 type: str
133 ospf_debug_route_flags:
134 description:
135 - ospf_debug_route_flags
136 type: str
137 ospf6_debug_events_flags:
138 description:
139 - ospf6_debug_events_flags
140 type: str
141 ospf6_debug_ifsm_flags:
142 description:
143 - ospf6_debug_ifsm_flags
144 type: str
145 ospf6_debug_lsa_flags:
146 description:
147 - ospf6_debug_lsa_flags
148 type: str
149 ospf6_debug_nfsm_flags:
150 description:
151 - ospf6_debug_nfsm_flags
152 type: str
153 ospf6_debug_nsm_flags:
154 description:
155 - ospf6_debug_nsm_flags
156 type: str
157 ospf6_debug_packet_flags:
158 description:
159 - ospf6_debug_packet_flags
160 type: str
161 ospf6_debug_route_flags:
162 description:
163 - ospf6_debug_route_flags
164 type: str
165 pimdm_debug_flags:
166 description:
167 - pimdm_debug_flags
168 type: str
169 pimsm_debug_joinprune_flags:
170 description:
171 - pimsm_debug_joinprune_flags
172 type: str
173 pimsm_debug_simple_flags:
174 description:
175 - pimsm_debug_simple_flags
176 type: str
177 pimsm_debug_timer_flags:
178 description:
179 - pimsm_debug_timer_flags
180 type: str
181 rip_debug_flags:
182 description:
183 - rip_debug_flags
184 type: str
185 ripng_debug_flags:
186 description:
187 - ripng_debug_flags
188 type: str
189 show_filter:
190 description:
191 - Prefix-list as filter for showing routes. Source router.prefix-list.name.
192 type: str
193 """
194
195 EXAMPLES = """
196 - name: Configure router settings.
197 fortinet.fortios.fortios_router_setting:
198 vdom: "{{ vdom }}"
199 router_setting:
200 bgp_debug_flags: "<your_own_value>"
201 hostname: "myhostname"
202 igmp_debug_flags: "<your_own_value>"
203 imi_debug_flags: "<your_own_value>"
204 isis_debug_flags: "<your_own_value>"
205 kernel_route_distance: "255"
206 ospf_debug_events_flags: "<your_own_value>"
207 ospf_debug_ifsm_flags: "<your_own_value>"
208 ospf_debug_lsa_flags: "<your_own_value>"
209 ospf_debug_nfsm_flags: "<your_own_value>"
210 ospf_debug_nsm_flags: "<your_own_value>"
211 ospf_debug_packet_flags: "<your_own_value>"
212 ospf_debug_route_flags: "<your_own_value>"
213 ospf6_debug_events_flags: "<your_own_value>"
214 ospf6_debug_ifsm_flags: "<your_own_value>"
215 ospf6_debug_lsa_flags: "<your_own_value>"
216 ospf6_debug_nfsm_flags: "<your_own_value>"
217 ospf6_debug_nsm_flags: "<your_own_value>"
218 ospf6_debug_packet_flags: "<your_own_value>"
219 ospf6_debug_route_flags: "<your_own_value>"
220 pimdm_debug_flags: "<your_own_value>"
221 pimsm_debug_joinprune_flags: "<your_own_value>"
222 pimsm_debug_simple_flags: "<your_own_value>"
223 pimsm_debug_timer_flags: "<your_own_value>"
224 rip_debug_flags: "<your_own_value>"
225 ripng_debug_flags: "<your_own_value>"
226 show_filter: "<your_own_value> (source router.prefix-list.name)"
227 """
228
229 RETURN = """
230 build:
231 description: Build number of the fortigate image
232 returned: always
233 type: str
234 sample: '1547'
235 http_method:
236 description: Last method used to provision the content into FortiGate
237 returned: always
238 type: str
239 sample: 'PUT'
240 http_status:
241 description: Last result given by FortiGate on last operation applied
242 returned: always
243 type: str
244 sample: "200"
245 mkey:
246 description: Master key (id) used in the last call to FortiGate
247 returned: success
248 type: str
249 sample: "id"
250 name:
251 description: Name of the table used to fulfill the request
252 returned: always
253 type: str
254 sample: "urlfilter"
255 path:
256 description: Path of the table used to fulfill the request
257 returned: always
258 type: str
259 sample: "webfilter"
260 revision:
261 description: Internal revision number
262 returned: always
263 type: str
264 sample: "17.0.2.10658"
265 serial:
266 description: Serial number of the unit
267 returned: always
268 type: str
269 sample: "FGVMEVYYQT3AB5352"
270 status:
271 description: Indication of the operation's result
272 returned: always
273 type: str
274 sample: "success"
275 vdom:
276 description: Virtual domain used
277 returned: always
278 type: str
279 sample: "root"
280 version:
281 description: Version of the FortiGate
282 returned: always
283 type: str
284 sample: "v5.6.3"
285 """
286 from ansible.module_utils.basic import AnsibleModule
287 from ansible.module_utils.connection import Connection
288 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import (
289 FortiOSHandler,
290 )
291 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import (
292 check_legacy_fortiosapi,
293 )
294 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import (
295 schema_to_module_spec,
296 )
297 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import (
298 check_schema_versioning,
299 )
300 from ansible_collections.fortinet.fortios.plugins.module_utils.fortimanager.common import (
301 FAIL_SOCKET_MSG,
302 )
303 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.data_post_processor import (
304 remove_invalid_fields,
305 )
306 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import (
307 is_same_comparison,
308 )
309 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import (
310 serialize,
311 )
312 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import (
313 find_current_values,
314 )
315 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import (
316 unify_data_format,
317 )
318
319
320 def filter_router_setting_data(json):
321 option_list = [
322 "bgp_debug_flags",
323 "hostname",
324 "igmp_debug_flags",
325 "imi_debug_flags",
326 "isis_debug_flags",
327 "kernel_route_distance",
328 "ospf_debug_events_flags",
329 "ospf_debug_ifsm_flags",
330 "ospf_debug_lsa_flags",
331 "ospf_debug_nfsm_flags",
332 "ospf_debug_nsm_flags",
333 "ospf_debug_packet_flags",
334 "ospf_debug_route_flags",
335 "ospf6_debug_events_flags",
336 "ospf6_debug_ifsm_flags",
337 "ospf6_debug_lsa_flags",
338 "ospf6_debug_nfsm_flags",
339 "ospf6_debug_nsm_flags",
340 "ospf6_debug_packet_flags",
341 "ospf6_debug_route_flags",
342 "pimdm_debug_flags",
343 "pimsm_debug_joinprune_flags",
344 "pimsm_debug_simple_flags",
345 "pimsm_debug_timer_flags",
346 "rip_debug_flags",
347 "ripng_debug_flags",
348 "show_filter",
349 ]
350
351 json = remove_invalid_fields(json)
352 dictionary = {}
353
354 for attribute in option_list:
355 if attribute in json and json[attribute] is not None:
356 dictionary[attribute] = json[attribute]
357
358 return dictionary
359
360
361 def underscore_to_hyphen(data):
362 new_data = None
363 if isinstance(data, list):
364 new_data = []
365 for i, elem in enumerate(data):
366 new_data.append(underscore_to_hyphen(elem))
367 elif isinstance(data, dict):
368 new_data = {}
369 for k, v in data.items():
370 new_data[k.replace("_", "-")] = underscore_to_hyphen(v)
371 else:
372 return data
373 return new_data
374
375
376 def router_setting(data, fos, check_mode=False):
377
378 state = None
379 vdom = data["vdom"]
380 parameters = None
381 state = data.get("state", None)
382 router_setting_data = data["router_setting"]
383
384 filtered_data = filter_router_setting_data(router_setting_data)
385 converted_data = underscore_to_hyphen(filtered_data)
386
387 # check_mode starts from here
388 if check_mode:
389 diff = {
390 "before": "",
391 "after": filtered_data,
392 }
393 mkeyname = fos.get_mkeyname(None, None)
394 mkey = fos.get_mkey("router", "setting", filtered_data, vdom=vdom)
395 current_data = fos.get(
396 "router", "setting", vdom=vdom, mkey=mkey, parameters=parameters
397 )
398 is_existed = (
399 current_data
400 and current_data.get("http_status") == 200
401 and (
402 mkeyname
403 and isinstance(current_data.get("results"), list)
404 and len(current_data["results"]) > 0
405 or not mkeyname
406 and current_data["results"] # global object response
407 )
408 )
409
410 # 2. if it exists and the state is 'present' then compare current settings with desired
411 if state == "present" or state is True or state is None:
412 # for non global modules, mkeyname must exist and it's a new module when mkey is None
413 if mkeyname is not None and mkey is None:
414 return False, True, filtered_data, diff
415
416 # if mkey exists then compare each other
417 # record exits and they're matched or not
418 copied_filtered_data = filtered_data.copy()
419 copied_filtered_data.pop(mkeyname, None)
420 unified_filtered_data = unify_data_format(copied_filtered_data)
421
422 current_data_results = current_data.get("results", {})
423 current_config = (
424 current_data_results[0]
425 if mkeyname
426 and isinstance(current_data_results, list)
427 and len(current_data_results) > 0
428 else current_data_results
429 )
430 if is_existed:
431 unified_current_values = find_current_values(
432 unified_filtered_data,
433 unify_data_format(current_config),
434 )
435
436 is_same = is_same_comparison(
437 serialize(unified_current_values), serialize(unified_filtered_data)
438 )
439
440 return (
441 False,
442 not is_same,
443 filtered_data,
444 {"before": unified_current_values, "after": unified_filtered_data},
445 )
446
447 # record does not exist
448 return False, True, filtered_data, diff
449
450 if state == "absent":
451 if mkey is None:
452 return (
453 False,
454 False,
455 filtered_data,
456 {"before": current_data["results"][0], "after": ""},
457 )
458
459 if is_existed:
460 return (
461 False,
462 True,
463 filtered_data,
464 {"before": current_data["results"][0], "after": ""},
465 )
466 return False, False, filtered_data, {}
467
468 return True, False, {"reason: ": "Must provide state parameter"}, {}
469 # pass post processed data to member operations
470 # no need to do underscore_to_hyphen since do_member_operation handles it by itself
471 data_copy = data.copy()
472 data_copy["router_setting"] = filtered_data
473 fos.do_member_operation(
474 "router",
475 "setting",
476 data_copy,
477 )
478
479 return fos.set(
480 "router", "setting", data=converted_data, vdom=vdom, parameters=parameters
481 )
482
483
484 def is_successful_status(resp):
485 return (
486 "status" in resp
487 and resp["status"] == "success"
488 or "http_status" in resp
489 and resp["http_status"] == 200
490 or "http_method" in resp
491 and resp["http_method"] == "DELETE"
492 and resp["http_status"] == 404
493 )
494
495
496 def fortios_router(data, fos, check_mode):
497
498 if data["router_setting"]:
499 resp = router_setting(data, fos, check_mode)
500 else:
501 fos._module.fail_json(msg="missing task body: %s" % ("router_setting"))
502 if isinstance(resp, tuple) and len(resp) == 4:
503 return resp
504 return (
505 not is_successful_status(resp),
506 is_successful_status(resp)
507 and (resp["revision_changed"] if "revision_changed" in resp else True),
508 resp,
509 {},
510 )
511
512
513 versioned_schema = {
514 "v_range": [["v6.0.0", ""]],
515 "type": "dict",
516 "children": {
517 "show_filter": {"v_range": [["v6.0.0", ""]], "type": "string"},
518 "hostname": {"v_range": [["v6.0.0", ""]], "type": "string"},
519 "kernel_route_distance": {"v_range": [["v7.6.0", ""]], "type": "integer"},
520 "ospf_debug_lsa_flags": {"v_range": [["v6.2.3", "v6.2.3"]], "type": "string"},
521 "ospf_debug_nfsm_flags": {"v_range": [["v6.2.3", "v6.2.3"]], "type": "string"},
522 "ospf_debug_packet_flags": {
523 "v_range": [["v6.2.3", "v6.2.3"]],
524 "type": "string",
525 },
526 "ospf_debug_events_flags": {
527 "v_range": [["v6.2.3", "v6.2.3"]],
528 "type": "string",
529 },
530 "ospf_debug_route_flags": {"v_range": [["v6.2.3", "v6.2.3"]], "type": "string"},
531 "ospf_debug_ifsm_flags": {"v_range": [["v6.2.3", "v6.2.3"]], "type": "string"},
532 "ospf_debug_nsm_flags": {"v_range": [["v6.2.3", "v6.2.3"]], "type": "string"},
533 "rip_debug_flags": {"v_range": [["v6.2.3", "v6.2.3"]], "type": "string"},
534 "bgp_debug_flags": {"v_range": [["v6.2.3", "v6.2.3"]], "type": "string"},
535 "igmp_debug_flags": {"v_range": [["v6.2.3", "v6.2.3"]], "type": "string"},
536 "pimdm_debug_flags": {"v_range": [["v6.2.3", "v6.2.3"]], "type": "string"},
537 "pimsm_debug_simple_flags": {
538 "v_range": [["v6.2.3", "v6.2.3"]],
539 "type": "string",
540 },
541 "pimsm_debug_timer_flags": {
542 "v_range": [["v6.2.3", "v6.2.3"]],
543 "type": "string",
544 },
545 "pimsm_debug_joinprune_flags": {
546 "v_range": [["v6.2.3", "v6.2.3"]],
547 "type": "string",
548 },
549 "imi_debug_flags": {"v_range": [["v6.2.3", "v6.2.3"]], "type": "string"},
550 "isis_debug_flags": {"v_range": [["v6.2.3", "v6.2.3"]], "type": "string"},
551 "ospf6_debug_lsa_flags": {"v_range": [["v6.2.3", "v6.2.3"]], "type": "string"},
552 "ospf6_debug_nfsm_flags": {"v_range": [["v6.2.3", "v6.2.3"]], "type": "string"},
553 "ospf6_debug_packet_flags": {
554 "v_range": [["v6.2.3", "v6.2.3"]],
555 "type": "string",
556 },
557 "ospf6_debug_events_flags": {
558 "v_range": [["v6.2.3", "v6.2.3"]],
559 "type": "string",
560 },
561 "ospf6_debug_route_flags": {
562 "v_range": [["v6.2.3", "v6.2.3"]],
563 "type": "string",
564 },
565 "ospf6_debug_ifsm_flags": {"v_range": [["v6.2.3", "v6.2.3"]], "type": "string"},
566 "ospf6_debug_nsm_flags": {"v_range": [["v6.2.3", "v6.2.3"]], "type": "string"},
567 "ripng_debug_flags": {"v_range": [["v6.2.3", "v6.2.3"]], "type": "string"},
568 },
569 }
570
571
572 def main():
573 module_spec = schema_to_module_spec(versioned_schema)
574 mkeyname = None
575 fields = {
576 "access_token": {"required": False, "type": "str", "no_log": True},
577 "enable_log": {"required": False, "type": "bool", "default": False},
578 "vdom": {"required": False, "type": "str", "default": "root"},
579 "member_path": {"required": False, "type": "str"},
580 "member_state": {
581 "type": "str",
582 "required": False,
583 "choices": ["present", "absent"],
584 },
585 "router_setting": {
586 "required": False,
587 "type": "dict",
588 "default": None,
589 "options": {},
590 },
591 }
592 for attribute_name in module_spec["options"]:
593 fields["router_setting"]["options"][attribute_name] = module_spec["options"][
594 attribute_name
595 ]
596 if mkeyname and mkeyname == attribute_name:
597 fields["router_setting"]["options"][attribute_name]["required"] = True
598
599 module = AnsibleModule(argument_spec=fields, supports_check_mode=True)
600 check_legacy_fortiosapi(module)
601
602 is_error = False
603 has_changed = False
604 result = None
605 diff = None
606
607 versions_check_result = None
608 if module._socket_path:
609 connection = Connection(module._socket_path)
610 if "access_token" in module.params:
611 connection.set_custom_option("access_token", module.params["access_token"])
612
613 if "enable_log" in module.params:
614 connection.set_custom_option("enable_log", module.params["enable_log"])
615 else:
616 connection.set_custom_option("enable_log", False)
617 fos = FortiOSHandler(connection, module, mkeyname, admin_passwd_header=False)
618 versions_check_result = check_schema_versioning(
619 fos, versioned_schema, "router_setting"
620 )
621
622 is_error, has_changed, result, diff = fortios_router(
623 module.params, fos, module.check_mode
624 )
625
626 else:
627 module.fail_json(**FAIL_SOCKET_MSG)
628
629 if versions_check_result and versions_check_result["matched"] is False:
630 module.warn(
631 "Ansible has detected version mismatch between FortOS system and your playbook, see more details by specifying option -vvv"
632 )
633
634 if not is_error:
635 if versions_check_result and versions_check_result["matched"] is False:
636 module.exit_json(
637 changed=has_changed,
638 version_check_warning=versions_check_result,
639 meta=result,
640 diff=diff,
641 )
642 else:
643 module.exit_json(changed=has_changed, meta=result, diff=diff)
644 else:
645 if versions_check_result and versions_check_result["matched"] is False:
646 module.fail_json(
647 msg="Error in repo",
648 version_check_warning=versions_check_result,
649 meta=result,
650 )
651 else:
652 module.fail_json(msg="Error in repo", meta=result)
653
654
655 if __name__ == "__main__":
656 main()