Debian Codegraph @

ansible_collections/community/vmware/plugins/modules/vmware_host_firewall_info.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2018, Abhijeet Kasurde <[email protected]>
5 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
6 # SPDX-License-Identifier: GPL-3.0-or-later
7
8 from __future__ import absolute_import, division, print_function
9 __metaclass__ = type
10
11
12 DOCUMENTATION = r'''
13 ---
14 module: vmware_host_firewall_info
15 short_description: Gathers info about an ESXi host's firewall configuration information
16 description:
17 - This module can be used to gather information about an ESXi host's firewall configuration information when ESXi hostname or Cluster name is given.
18 author:
19 - Abhijeet Kasurde (@Akasurde)
20 options:
21 cluster_name:
22 description:
23 - Name of the cluster from which the ESXi host belong to.
24 - If O(esxi_hostname) is not given, this parameter is required.
25 type: str
26 esxi_hostname:
27 description:
28 - ESXi hostname to gather information from.
29 - If O(cluster_name) is not given, this parameter is required.
30 type: str
31 extends_documentation_fragment:
32 - vmware.vmware.base_options
33
34 '''
35
36 EXAMPLES = r'''
37 - name: Gather firewall info about all ESXi Host in given Cluster
38 community.vmware.vmware_host_firewall_info:
39 hostname: '{{ vcenter_hostname }}'
40 username: '{{ vcenter_username }}'
41 password: '{{ vcenter_password }}'
42 cluster_name: cluster_name
43 delegate_to: localhost
44
45 - name: Gather firewall info about ESXi Host
46 community.vmware.vmware_host_firewall_info:
47 hostname: '{{ vcenter_hostname }}'
48 username: '{{ vcenter_username }}'
49 password: '{{ vcenter_password }}'
50 esxi_hostname: '{{ esxi_hostname }}'
51 delegate_to: localhost
52 '''
53
54 RETURN = r'''
55 hosts_firewall_info:
56 description: metadata about host's firewall configuration
57 returned: on success
58 type: dict
59 sample: {
60 "esxi_hostname_0001": [
61 {
62 "allowed_hosts": {
63 "all_ip": true,
64 "ip_address": [
65 "10.10.10.1",
66 ],
67 "ip_network": [
68 "11.111.112.0/22",
69 "192.168.10.1/24"
70 ],
71 },
72 "enabled": true,
73 "key": "CIMHttpServer",
74 "rule": [
75 {
76 "direction": "inbound",
77 "end_port": null,
78 "port": 5988,
79 "port_type": "dst",
80 "protocol": "tcp"
81 }
82 ],
83 "service": "sfcbd-watchdog"
84 },
85 ]
86 }
87 '''
88
89 from ansible.module_utils.basic import AnsibleModule
90 from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi
91 from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec
92
93
94 class FirewallInfoManager(PyVmomi):
95 def __init__(self, module):
96 super(FirewallInfoManager, self).__init__(module)
97 cluster_name = self.params.get('cluster_name', None)
98 esxi_host_name = self.params.get('esxi_hostname', None)
99 self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name)
100
101 @staticmethod
102 def normalize_rule_set(rule_obj):
103 rule_dict = dict()
104 rule_dict['key'] = rule_obj.key
105 rule_dict['service'] = rule_obj.service
106 rule_dict['enabled'] = rule_obj.enabled
107 rule_dict['rule'] = []
108
109 for rule in rule_obj.rule:
110 rule_set_dict = dict()
111 rule_set_dict['port'] = rule.port
112 rule_set_dict['end_port'] = rule.endPort
113 rule_set_dict['direction'] = rule.direction
114 rule_set_dict['port_type'] = rule.portType
115 rule_set_dict['protocol'] = rule.protocol
116 rule_dict['rule'].append(rule_set_dict)
117
118 allowed_host = rule_obj.allowedHosts
119 rule_allow_host = dict()
120 rule_allow_host['ip_address'] = list(allowed_host.ipAddress)
121 rule_allow_host['ip_network'] = [ip.network + "/" + str(ip.prefixLength) for ip in allowed_host.ipNetwork]
122 rule_allow_host['all_ip'] = allowed_host.allIp
123 rule_dict['allowed_hosts'] = rule_allow_host
124 return rule_dict
125
126 def gather_host_firewall_info(self):
127 hosts_firewall_info = dict()
128 for host in self.hosts:
129 firewall_system = host.configManager.firewallSystem
130 if firewall_system:
131 hosts_firewall_info[host.name] = []
132 for rule_set_obj in firewall_system.firewallInfo.ruleset:
133 hosts_firewall_info[host.name].append(self.normalize_rule_set(rule_obj=rule_set_obj))
134 return hosts_firewall_info
135
136
137 def main():
138 argument_spec = base_argument_spec()
139 argument_spec.update(
140 cluster_name=dict(type='str', required=False),
141 esxi_hostname=dict(type='str', required=False),
142 )
143
144 module = AnsibleModule(
145 argument_spec=argument_spec,
146 required_one_of=[
147 ['cluster_name', 'esxi_hostname'],
148 ],
149 supports_check_mode=True
150 )
151
152 vmware_host_firewall = FirewallInfoManager(module)
153 module.exit_json(changed=False, hosts_firewall_info=vmware_host_firewall.gather_host_firewall_info())
154
155
156 if __name__ == "__main__":
157 main()