Debian Codegraph @

ansible_collections/fortinet/fortios/plugins/modules/fortios_firewall_address.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_firewall_address
23 short_description: Configure IPv4 addresses 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 firewall feature and address 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 state:
80 description:
81 - Indicates whether to create or remove the object.
82 type: str
83 required: true
84 choices:
85 - 'present'
86 - 'absent'
87 firewall_address:
88 description:
89 - Configure IPv4 addresses.
90 default: null
91 type: dict
92 suboptions:
93 agent_id:
94 description:
95 - Telemetry agent id. Source telemetry-controller.agent.agent-id.
96 type: str
97 allow_routing:
98 description:
99 - Enable/disable use of this address in routing configurations.
100 type: str
101 choices:
102 - 'enable'
103 - 'disable'
104 associated_interface:
105 description:
106 - Network interface associated with address. Source system.interface.name system.zone.name.
107 type: str
108 cache_ttl:
109 description:
110 - Defines the minimal TTL of individual IP addresses in FQDN cache measured in seconds.
111 type: int
112 clearpass_spt:
113 description:
114 - SPT (System Posture Token) value.
115 type: str
116 choices:
117 - 'unknown'
118 - 'healthy'
119 - 'quarantine'
120 - 'checkup'
121 - 'transient'
122 - 'infected'
123 color:
124 description:
125 - Color of icon on the GUI.
126 type: int
127 comment:
128 description:
129 - Comment.
130 type: str
131 country:
132 description:
133 - IP addresses associated to a specific country.
134 type: str
135 end_ip:
136 description:
137 - Final IP address (inclusive) in the range for the address.
138 type: str
139 end_mac:
140 description:
141 - Last MAC address in the range.
142 type: str
143 epg_name:
144 description:
145 - Endpoint group name.
146 type: str
147 fabric_object:
148 description:
149 - Security Fabric global object setting.
150 type: str
151 choices:
152 - 'enable'
153 - 'disable'
154 filter:
155 description:
156 - Match criteria filter.
157 type: str
158 fqdn:
159 description:
160 - Fully Qualified Domain Name address.
161 type: str
162 fsso_group:
163 description:
164 - FSSO group(s).
165 type: list
166 elements: dict
167 suboptions:
168 name:
169 description:
170 - FSSO group name. Source user.adgrp.name.
171 required: true
172 type: str
173 hw_model:
174 description:
175 - Dynamic address matching hardware model.
176 type: str
177 hw_vendor:
178 description:
179 - Dynamic address matching hardware vendor.
180 type: str
181 interface:
182 description:
183 - Name of interface whose IP address is to be used. Source system.interface.name.
184 type: str
185 list:
186 description:
187 - IP address list.
188 type: list
189 elements: dict
190 suboptions:
191 ip:
192 description:
193 - IP.
194 required: true
195 type: str
196 net_id:
197 description:
198 - Network ID.
199 type: str
200 obj_id:
201 description:
202 - Object ID.
203 type: str
204 macaddr:
205 description:
206 - Multiple MAC address ranges.
207 type: list
208 elements: dict
209 suboptions:
210 macaddr:
211 description:
212 - MAC address ranges <start>[-<end>] separated by space.
213 required: true
214 type: str
215 name:
216 description:
217 - Address name.
218 required: true
219 type: str
220 node_ip_only:
221 description:
222 - Enable/disable collection of node addresses only in Kubernetes.
223 type: str
224 choices:
225 - 'enable'
226 - 'disable'
227 obj_id:
228 description:
229 - Object ID for NSX.
230 type: str
231 obj_tag:
232 description:
233 - Tag of dynamic address object.
234 type: str
235 obj_type:
236 description:
237 - Object type.
238 type: str
239 choices:
240 - 'ip'
241 - 'mac'
242 organization:
243 description:
244 - 'Organization domain name (Syntax: organization/domain).'
245 type: str
246 os:
247 description:
248 - Dynamic address matching operating system.
249 type: str
250 passive_fqdn_learning:
251 description:
252 - Enable/disable passive learning of FQDNs. When enabled, the FortiGate learns, trusts, and saves FQDNs from endpoint DNS queries .
253 type: str
254 choices:
255 - 'disable'
256 - 'enable'
257 policy_group:
258 description:
259 - Policy group name.
260 type: str
261 route_tag:
262 description:
263 - route-tag address.
264 type: int
265 sdn:
266 description:
267 - SDN. Source system.sdn-connector.name.
268 type: str
269 sdn_addr_type:
270 description:
271 - Type of addresses to collect.
272 type: str
273 choices:
274 - 'private'
275 - 'public'
276 - 'all'
277 sdn_tag:
278 description:
279 - SDN Tag.
280 type: str
281 sso_attribute_value:
282 description:
283 - RADIUS attributes value.
284 type: list
285 elements: dict
286 suboptions:
287 name:
288 description:
289 - RADIUS attribute value.
290 required: true
291 type: str
292 start_ip:
293 description:
294 - First IP address (inclusive) in the range for the address.
295 type: str
296 start_mac:
297 description:
298 - First MAC address in the range.
299 type: str
300 sub_type:
301 description:
302 - Sub-type of address.
303 type: str
304 choices:
305 - 'sdn'
306 - 'clearpass-spt'
307 - 'fsso'
308 - 'rsso'
309 - 'ems-tag'
310 - 'fortivoice-tag'
311 - 'fortinac-tag'
312 - 'swc-tag'
313 - 'device-identification'
314 - 'external-resource'
315 - 'telemetry'
316 - 'obsolete'
317 - 'fortipolicy-tag'
318 subnet:
319 description:
320 - IP address and subnet mask of address.
321 type: str
322 subnet_name:
323 description:
324 - Subnet name.
325 type: str
326 sw_version:
327 description:
328 - Dynamic address matching software version.
329 type: str
330 tag_detection_level:
331 description:
332 - Tag detection level of dynamic address object.
333 type: str
334 tag_type:
335 description:
336 - Tag type of dynamic address object.
337 type: str
338 tagging:
339 description:
340 - Config object tagging.
341 type: list
342 elements: dict
343 suboptions:
344 category:
345 description:
346 - Tag category. Source system.object-tagging.category.
347 type: str
348 name:
349 description:
350 - Tagging entry name.
351 required: true
352 type: str
353 tags:
354 description:
355 - Tags.
356 type: list
357 elements: dict
358 suboptions:
359 name:
360 description:
361 - Tag name. Source system.object-tagging.tags.name.
362 required: true
363 type: str
364 tenant:
365 description:
366 - Tenant.
367 type: str
368 type:
369 description:
370 - Type of address.
371 type: str
372 choices:
373 - 'ipmask'
374 - 'iprange'
375 - 'fqdn'
376 - 'geography'
377 - 'wildcard'
378 - 'dynamic'
379 - 'interface-subnet'
380 - 'mac'
381 - 'route-tag'
382 - 'wildcard-fqdn'
383 uuid:
384 description:
385 - Universally Unique Identifier (UUID; automatically assigned but can be manually reset).
386 type: str
387 visibility:
388 description:
389 - Enable/disable address visibility in the GUI.
390 type: str
391 choices:
392 - 'enable'
393 - 'disable'
394 wildcard:
395 description:
396 - IP address and wildcard netmask.
397 type: str
398 wildcard_fqdn:
399 description:
400 - Fully Qualified Domain Name with wildcard characters.
401 type: str
402 """
403
404 EXAMPLES = """
405 - name: Configure IPv4 addresses.
406 fortinet.fortios.fortios_firewall_address:
407 vdom: "{{ vdom }}"
408 state: "present"
409 access_token: "<your_own_value>"
410 firewall_address:
411 agent_id: "<your_own_value> (source telemetry-controller.agent.agent-id)"
412 allow_routing: "enable"
413 associated_interface: "<your_own_value> (source system.interface.name system.zone.name)"
414 cache_ttl: "0"
415 clearpass_spt: "unknown"
416 color: "0"
417 comment: "Comment."
418 country: "<your_own_value>"
419 end_ip: "<your_own_value>"
420 end_mac: "<your_own_value>"
421 epg_name: "<your_own_value>"
422 fabric_object: "enable"
423 filter: "<your_own_value>"
424 fqdn: "<your_own_value>"
425 fsso_group:
426 -
427 name: "default_name_18 (source user.adgrp.name)"
428 hw_model: "<your_own_value>"
429 hw_vendor: "<your_own_value>"
430 interface: "<your_own_value> (source system.interface.name)"
431 list:
432 -
433 ip: "<your_own_value>"
434 net_id: "<your_own_value>"
435 obj_id: "<your_own_value>"
436 macaddr:
437 -
438 macaddr: "<your_own_value>"
439 name: "default_name_28"
440 node_ip_only: "enable"
441 obj_id: "<your_own_value>"
442 obj_tag: "<your_own_value>"
443 obj_type: "ip"
444 organization: "<your_own_value>"
445 os: "<your_own_value>"
446 passive_fqdn_learning: "disable"
447 policy_group: "<your_own_value>"
448 route_tag: "0"
449 sdn: "<your_own_value> (source system.sdn-connector.name)"
450 sdn_addr_type: "private"
451 sdn_tag: "<your_own_value>"
452 sso_attribute_value:
453 -
454 name: "default_name_42"
455 start_ip: "<your_own_value>"
456 start_mac: "<your_own_value>"
457 sub_type: "sdn"
458 subnet: "<your_own_value>"
459 subnet_name: "<your_own_value>"
460 sw_version: "<your_own_value>"
461 tag_detection_level: "<your_own_value>"
462 tag_type: "<your_own_value>"
463 tagging:
464 -
465 category: "<your_own_value> (source system.object-tagging.category)"
466 name: "default_name_53"
467 tags:
468 -
469 name: "default_name_55 (source system.object-tagging.tags.name)"
470 tenant: "<your_own_value>"
471 type: "ipmask"
472 uuid: "<your_own_value>"
473 visibility: "enable"
474 wildcard: "<your_own_value>"
475 wildcard_fqdn: "<your_own_value>"
476 """
477
478 RETURN = """
479 build:
480 description: Build number of the fortigate image
481 returned: always
482 type: str
483 sample: '1547'
484 http_method:
485 description: Last method used to provision the content into FortiGate
486 returned: always
487 type: str
488 sample: 'PUT'
489 http_status:
490 description: Last result given by FortiGate on last operation applied
491 returned: always
492 type: str
493 sample: "200"
494 mkey:
495 description: Master key (id) used in the last call to FortiGate
496 returned: success
497 type: str
498 sample: "id"
499 name:
500 description: Name of the table used to fulfill the request
501 returned: always
502 type: str
503 sample: "urlfilter"
504 path:
505 description: Path of the table used to fulfill the request
506 returned: always
507 type: str
508 sample: "webfilter"
509 revision:
510 description: Internal revision number
511 returned: always
512 type: str
513 sample: "17.0.2.10658"
514 serial:
515 description: Serial number of the unit
516 returned: always
517 type: str
518 sample: "FGVMEVYYQT3AB5352"
519 status:
520 description: Indication of the operation's result
521 returned: always
522 type: str
523 sample: "success"
524 vdom:
525 description: Virtual domain used
526 returned: always
527 type: str
528 sample: "root"
529 version:
530 description: Version of the FortiGate
531 returned: always
532 type: str
533 sample: "v5.6.3"
534 """
535 from ansible.module_utils.basic import AnsibleModule
536 from ansible.module_utils.connection import Connection
537 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import (
538 FortiOSHandler,
539 )
540 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import (
541 check_legacy_fortiosapi,
542 )
543 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import (
544 schema_to_module_spec,
545 )
546 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import (
547 check_schema_versioning,
548 )
549 from ansible_collections.fortinet.fortios.plugins.module_utils.fortimanager.common import (
550 FAIL_SOCKET_MSG,
551 )
552 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.data_post_processor import (
553 remove_invalid_fields,
554 )
555 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import (
556 is_same_comparison,
557 )
558 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import (
559 serialize,
560 )
561 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import (
562 find_current_values,
563 )
564 from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import (
565 unify_data_format,
566 )
567
568
569 def filter_firewall_address_data(json):
570 option_list = [
571 "agent_id",
572 "allow_routing",
573 "associated_interface",
574 "cache_ttl",
575 "clearpass_spt",
576 "color",
577 "comment",
578 "country",
579 "end_ip",
580 "end_mac",
581 "epg_name",
582 "fabric_object",
583 "filter",
584 "fqdn",
585 "fsso_group",
586 "hw_model",
587 "hw_vendor",
588 "interface",
589 "list",
590 "macaddr",
591 "name",
592 "node_ip_only",
593 "obj_id",
594 "obj_tag",
595 "obj_type",
596 "organization",
597 "os",
598 "passive_fqdn_learning",
599 "policy_group",
600 "route_tag",
601 "sdn",
602 "sdn_addr_type",
603 "sdn_tag",
604 "sso_attribute_value",
605 "start_ip",
606 "start_mac",
607 "sub_type",
608 "subnet",
609 "subnet_name",
610 "sw_version",
611 "tag_detection_level",
612 "tag_type",
613 "tagging",
614 "tenant",
615 "type",
616 "uuid",
617 "visibility",
618 "wildcard",
619 "wildcard_fqdn",
620 ]
621
622 json = remove_invalid_fields(json)
623 dictionary = {}
624
625 for attribute in option_list:
626 if attribute in json and json[attribute] is not None:
627 dictionary[attribute] = json[attribute]
628
629 return dictionary
630
631
632 def underscore_to_hyphen(data):
633 new_data = None
634 if isinstance(data, list):
635 new_data = []
636 for i, elem in enumerate(data):
637 new_data.append(underscore_to_hyphen(elem))
638 elif isinstance(data, dict):
639 new_data = {}
640 for k, v in data.items():
641 new_data[k.replace("_", "-")] = underscore_to_hyphen(v)
642 else:
643 return data
644 return new_data
645
646
647 def firewall_address(data, fos, check_mode=False):
648
649 state = None
650 vdom = data["vdom"]
651 parameters = None
652 state = data.get("state", None)
653 firewall_address_data = data["firewall_address"]
654
655 filtered_data = filter_firewall_address_data(firewall_address_data)
656 converted_data = underscore_to_hyphen(filtered_data)
657
658 # check_mode starts from here
659 if check_mode:
660 diff = {
661 "before": "",
662 "after": filtered_data,
663 }
664 mkeyname = fos.get_mkeyname(None, None)
665 mkey = fos.get_mkey("firewall", "address", filtered_data, vdom=vdom)
666 current_data = fos.get(
667 "firewall", "address", vdom=vdom, mkey=mkey, parameters=parameters
668 )
669 is_existed = (
670 current_data
671 and current_data.get("http_status") == 200
672 and (
673 mkeyname
674 and isinstance(current_data.get("results"), list)
675 and len(current_data["results"]) > 0
676 or not mkeyname
677 and current_data["results"] # global object response
678 )
679 )
680
681 # 2. if it exists and the state is 'present' then compare current settings with desired
682 if state == "present" or state is True or state is None:
683 # for non global modules, mkeyname must exist and it's a new module when mkey is None
684 if mkeyname is not None and mkey is None:
685 return False, True, filtered_data, diff
686
687 # if mkey exists then compare each other
688 # record exits and they're matched or not
689 copied_filtered_data = filtered_data.copy()
690 copied_filtered_data.pop(mkeyname, None)
691 unified_filtered_data = unify_data_format(copied_filtered_data)
692
693 current_data_results = current_data.get("results", {})
694 current_config = (
695 current_data_results[0]
696 if mkeyname
697 and isinstance(current_data_results, list)
698 and len(current_data_results) > 0
699 else current_data_results
700 )
701 if is_existed:
702 unified_current_values = find_current_values(
703 unified_filtered_data,
704 unify_data_format(current_config),
705 )
706
707 is_same = is_same_comparison(
708 serialize(unified_current_values), serialize(unified_filtered_data)
709 )
710
711 return (
712 False,
713 not is_same,
714 filtered_data,
715 {"before": unified_current_values, "after": unified_filtered_data},
716 )
717
718 # record does not exist
719 return False, True, filtered_data, diff
720
721 if state == "absent":
722 if mkey is None:
723 return (
724 False,
725 False,
726 filtered_data,
727 {"before": current_data["results"][0], "after": ""},
728 )
729
730 if is_existed:
731 return (
732 False,
733 True,
734 filtered_data,
735 {"before": current_data["results"][0], "after": ""},
736 )
737 return False, False, filtered_data, {}
738
739 return True, False, {"reason: ": "Must provide state parameter"}, {}
740 # pass post processed data to member operations
741 # no need to do underscore_to_hyphen since do_member_operation handles it by itself
742 data_copy = data.copy()
743 data_copy["firewall_address"] = filtered_data
744 fos.do_member_operation(
745 "firewall",
746 "address",
747 data_copy,
748 )
749
750 if state == "present" or state is True:
751 return fos.set(
752 "firewall", "address", data=converted_data, vdom=vdom, parameters=parameters
753 )
754
755 elif state == "absent":
756 return fos.delete(
757 "firewall",
758 "address",
759 mkey=converted_data["name"],
760 vdom=vdom,
761 parameters=parameters,
762 )
763 else:
764 fos._module.fail_json(msg="state must be present or absent!")
765
766
767 def is_successful_status(resp):
768 return (
769 "status" in resp
770 and resp["status"] == "success"
771 or "http_status" in resp
772 and resp["http_status"] == 200
773 or "http_method" in resp
774 and resp["http_method"] == "DELETE"
775 and resp["http_status"] == 404
776 )
777
778
779 def fortios_firewall(data, fos, check_mode):
780
781 if data["firewall_address"]:
782 resp = firewall_address(data, fos, check_mode)
783 else:
784 fos._module.fail_json(msg="missing task body: %s" % ("firewall_address"))
785 if isinstance(resp, tuple) and len(resp) == 4:
786 return resp
787 return (
788 not is_successful_status(resp),
789 is_successful_status(resp)
790 and (resp["revision_changed"] if "revision_changed" in resp else True),
791 resp,
792 {},
793 )
794
795
796 versioned_schema = {
797 "type": "list",
798 "elements": "dict",
799 "children": {
800 "name": {"v_range": [["v6.0.0", ""]], "type": "string", "required": True},
801 "uuid": {"v_range": [["v6.0.0", ""]], "type": "string"},
802 "subnet": {"v_range": [["v6.0.0", ""]], "type": "string"},
803 "type": {
804 "v_range": [["v6.0.0", ""]],
805 "type": "string",
806 "options": [
807 {"value": "ipmask"},
808 {"value": "iprange"},
809 {"value": "fqdn"},
810 {"value": "geography"},
811 {"value": "wildcard"},
812 {"value": "dynamic"},
813 {"value": "interface-subnet", "v_range": [["v6.2.0", ""]]},
814 {"value": "mac", "v_range": [["v6.2.0", ""]]},
815 {"value": "route-tag", "v_range": [["v7.4.0", ""]]},
816 {"value": "wildcard-fqdn", "v_range": [["v6.0.0", "v6.0.11"]]},
817 ],
818 },
819 "route_tag": {"v_range": [["v7.4.0", ""]], "type": "integer"},
820 "sub_type": {
821 "v_range": [["v6.2.0", ""]],
822 "type": "string",
823 "options": [
824 {"value": "sdn"},
825 {"value": "clearpass-spt"},
826 {"value": "fsso"},
827 {"value": "rsso", "v_range": [["v7.6.1", ""]]},
828 {"value": "ems-tag", "v_range": [["v6.4.0", ""]]},
829 {"value": "fortivoice-tag", "v_range": [["v7.0.4", ""]]},
830 {"value": "fortinac-tag", "v_range": [["v7.0.4", ""]]},
831 {"value": "swc-tag", "v_range": [["v7.0.1", ""]]},
832 {"value": "device-identification", "v_range": [["v7.4.0", ""]]},
833 {"value": "external-resource", "v_range": [["v7.6.1", ""]]},
834 {"value": "telemetry", "v_range": [["v7.6.4", ""]]},
835 {"value": "obsolete", "v_range": [["v7.6.3", ""]]},
836 {"value": "fortipolicy-tag", "v_range": [["v7.2.4", "v7.6.2"]]},
837 ],
838 },
839 "clearpass_spt": {
840 "v_range": [["v6.2.0", ""]],
841 "type": "string",
842 "options": [
843 {"value": "unknown"},
844 {"value": "healthy"},
845 {"value": "quarantine"},
846 {"value": "checkup"},
847 {"value": "transient"},
848 {"value": "infected"},
849 ],
850 },
851 "macaddr": {
852 "type": "list",
853 "elements": "dict",
854 "children": {
855 "macaddr": {
856 "v_range": [["v7.0.0", ""]],
857 "type": "string",
858 "required": True,
859 }
860 },
861 "v_range": [["v7.0.0", ""]],
862 },
863 "start_ip": {"v_range": [["v6.0.0", ""]], "type": "string"},
864 "end_ip": {"v_range": [["v6.0.0", ""]], "type": "string"},
865 "fqdn": {"v_range": [["v6.0.0", ""]], "type": "string"},
866 "country": {"v_range": [["v6.0.0", ""]], "type": "string"},
867 "wildcard_fqdn": {"v_range": [["v6.0.0", ""]], "type": "string"},
868 "cache_ttl": {"v_range": [["v6.0.0", ""]], "type": "integer"},
869 "wildcard": {"v_range": [["v6.0.0", ""]], "type": "string"},
870 "sdn": {"v_range": [["v6.0.0", ""]], "type": "string"},
871 "fsso_group": {
872 "type": "list",
873 "elements": "dict",
874 "children": {
875 "name": {
876 "v_range": [["v6.2.0", ""]],
877 "type": "string",
878 "required": True,
879 }
880 },
881 "v_range": [["v6.2.0", ""]],
882 },
883 "sso_attribute_value": {
884 "type": "list",
885 "elements": "dict",
886 "children": {
887 "name": {
888 "v_range": [["v7.6.1", ""]],
889 "type": "string",
890 "required": True,
891 }
892 },
893 "v_range": [["v7.6.1", ""]],
894 },
895 "interface": {"v_range": [["v6.2.0", ""]], "type": "string"},
896 "tenant": {"v_range": [["v6.0.0", ""]], "type": "string"},
897 "organization": {"v_range": [["v6.0.0", ""]], "type": "string"},
898 "epg_name": {"v_range": [["v6.0.0", ""]], "type": "string"},
899 "subnet_name": {"v_range": [["v6.0.0", ""]], "type": "string"},
900 "sdn_tag": {"v_range": [["v6.0.0", ""]], "type": "string"},
901 "policy_group": {"v_range": [["v6.0.0", ""]], "type": "string"},
902 "obj_tag": {
903 "v_range": [["v6.4.0", "v6.4.0"], ["v6.4.4", ""]],
904 "type": "string",
905 },
906 "obj_type": {
907 "v_range": [["v6.4.0", "v6.4.0"], ["v6.4.4", ""]],
908 "type": "string",
909 "options": [{"value": "ip"}, {"value": "mac"}],
910 },
911 "tag_detection_level": {"v_range": [["v7.0.4", ""]], "type": "string"},
912 "tag_type": {"v_range": [["v7.0.4", ""]], "type": "string"},
913 "hw_vendor": {"v_range": [["v7.4.0", ""]], "type": "string"},
914 "hw_model": {"v_range": [["v7.4.0", ""]], "type": "string"},
915 "os": {"v_range": [["v7.4.0", ""]], "type": "string"},
916 "sw_version": {"v_range": [["v7.4.0", ""]], "type": "string"},
917 "agent_id": {"v_range": [["v7.6.4", ""]], "type": "string"},
918 "comment": {"v_range": [["v6.0.0", ""]], "type": "string"},
919 "associated_interface": {"v_range": [["v6.0.0", ""]], "type": "string"},
920 "color": {"v_range": [["v6.0.0", ""]], "type": "integer"},
921 "filter": {"v_range": [["v6.0.0", ""]], "type": "string"},
922 "sdn_addr_type": {
923 "v_range": [["v6.2.0", ""]],
924 "type": "string",
925 "options": [{"value": "private"}, {"value": "public"}, {"value": "all"}],
926 },
927 "node_ip_only": {
928 "v_range": [["v7.0.0", ""]],
929 "type": "string",
930 "options": [{"value": "enable"}, {"value": "disable"}],
931 },
932 "obj_id": {"v_range": [["v6.0.0", ""]], "type": "string"},
933 "list": {
934 "type": "list",
935 "elements": "dict",
936 "children": {
937 "ip": {"v_range": [["v6.0.0", ""]], "type": "string", "required": True},
938 "obj_id": {"v_range": [["v6.2.3", "v6.2.3"]], "type": "string"},
939 "net_id": {"v_range": [["v6.2.3", "v6.2.3"]], "type": "string"},
940 },
941 "v_range": [["v6.0.0", ""]],
942 },
943 "tagging": {
944 "type": "list",
945 "elements": "dict",
946 "children": {
947 "name": {
948 "v_range": [["v6.0.0", ""]],
949 "type": "string",
950 "required": True,
951 },
952 "category": {"v_range": [["v6.0.0", ""]], "type": "string"},
953 "tags": {
954 "type": "list",
955 "elements": "dict",
956 "children": {
957 "name": {
958 "v_range": [["v6.0.0", ""]],
959 "type": "string",
960 "required": True,
961 }
962 },
963 "v_range": [["v6.0.0", ""]],
964 },
965 },
966 "v_range": [["v6.0.0", ""]],
967 },
968 "allow_routing": {
969 "v_range": [["v6.0.0", ""]],
970 "type": "string",
971 "options": [{"value": "enable"}, {"value": "disable"}],
972 },
973 "passive_fqdn_learning": {
974 "v_range": [["v7.6.5", ""]],
975 "type": "string",
976 "options": [{"value": "disable"}, {"value": "enable"}],
977 },
978 "fabric_object": {
979 "v_range": [["v6.4.4", ""]],
980 "type": "string",
981 "options": [{"value": "enable"}, {"value": "disable"}],
982 },
983 "start_mac": {"v_range": [["v6.2.0", "v6.4.4"]], "type": "string"},
984 "end_mac": {"v_range": [["v6.2.0", "v6.4.4"]], "type": "string"},
985 "visibility": {
986 "v_range": [["v6.0.0", "v6.2.7"]],
987 "type": "string",
988 "options": [{"value": "enable"}, {"value": "disable"}],
989 },
990 },
991 "v_range": [["v6.0.0", ""]],
992 }
993
994
995 def main():
996 module_spec = schema_to_module_spec(versioned_schema)
997 mkeyname = "name"
998 fields = {
999 "access_token": {"required": False, "type": "str", "no_log": True},
1000 "enable_log": {"required": False, "type": "bool", "default": False},
1001 "vdom": {"required": False, "type": "str", "default": "root"},
1002 "member_path": {"required": False, "type": "str"},
1003 "member_state": {
1004 "type": "str",
1005 "required": False,
1006 "choices": ["present", "absent"],
1007 },
1008 "state": {"required": True, "type": "str", "choices": ["present", "absent"]},
1009 "firewall_address": {
1010 "required": False,
1011 "type": "dict",
1012 "default": None,
1013 "options": {},
1014 },
1015 }
1016 for attribute_name in module_spec["options"]:
1017 fields["firewall_address"]["options"][attribute_name] = module_spec["options"][
1018 attribute_name
1019 ]
1020 if mkeyname and mkeyname == attribute_name:
1021 fields["firewall_address"]["options"][attribute_name]["required"] = True
1022
1023 module = AnsibleModule(argument_spec=fields, supports_check_mode=True)
1024 check_legacy_fortiosapi(module)
1025
1026 is_error = False
1027 has_changed = False
1028 result = None
1029 diff = None
1030
1031 versions_check_result = None
1032 if module._socket_path:
1033 connection = Connection(module._socket_path)
1034 if "access_token" in module.params:
1035 connection.set_custom_option("access_token", module.params["access_token"])
1036
1037 if "enable_log" in module.params:
1038 connection.set_custom_option("enable_log", module.params["enable_log"])
1039 else:
1040 connection.set_custom_option("enable_log", False)
1041 fos = FortiOSHandler(connection, module, mkeyname, admin_passwd_header=False)
1042 versions_check_result = check_schema_versioning(
1043 fos, versioned_schema, "firewall_address"
1044 )
1045
1046 is_error, has_changed, result, diff = fortios_firewall(
1047 module.params, fos, module.check_mode
1048 )
1049
1050 else:
1051 module.fail_json(**FAIL_SOCKET_MSG)
1052
1053 if versions_check_result and versions_check_result["matched"] is False:
1054 module.warn(
1055 "Ansible has detected version mismatch between FortOS system and your playbook, see more details by specifying option -vvv"
1056 )
1057
1058 if not is_error:
1059 if versions_check_result and versions_check_result["matched"] is False:
1060 module.exit_json(
1061 changed=has_changed,
1062 version_check_warning=versions_check_result,
1063 meta=result,
1064 diff=diff,
1065 )
1066 else:
1067 module.exit_json(changed=has_changed, meta=result, diff=diff)
1068 else:
1069 if versions_check_result and versions_check_result["matched"] is False:
1070 module.fail_json(
1071 msg="Error in repo",
1072 version_check_warning=versions_check_result,
1073 meta=result,
1074 )
1075 else:
1076 module.fail_json(msg="Error in repo", meta=result)
1077
1078
1079 if __name__ == "__main__":
1080 main()