Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2018, Abhijeet Kasurde <[email protected]>
5 # Copyright: (c) 2018, Christian Kotte <[email protected]>
6 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
7 # SPDX-License-Identifier: GPL-3.0-or-later
8
9 from __future__ import absolute_import, division, print_function
10 __metaclass__ = type
11
12
13 DOCUMENTATION = r'''
14 ---
15 module: vmware_portgroup_info
16 short_description: Gathers info about an ESXi host's Port Group configuration
17 description:
18 - This module can be used to gather information about an ESXi host's Port Group configuration when ESXi hostname or Cluster name is given.
19 author:
20 - Abhijeet Kasurde (@Akasurde)
21 - Christian Kotte (@ckotte)
22 options:
23 policies:
24 description:
25 - Gather information about Security, Traffic Shaping, as well as Teaming and failover.
26 - The property C(ts) stands for Traffic Shaping and C(lb) for Load Balancing.
27 type: bool
28 default: false
29 cluster_name:
30 description:
31 - Name of the cluster.
32 - Info will be returned for all hostsystem belonging to this cluster name.
33 - If O(esxi_hostname) is not given, this parameter is required.
34 type: str
35 esxi_hostname:
36 description:
37 - ESXi hostname to gather information from.
38 - If O(cluster_name) is not given, this parameter is required.
39 type: str
40 extends_documentation_fragment:
41 - vmware.vmware.base_options
42
43 '''
44
45 EXAMPLES = r'''
46 - name: Gather portgroup info about all ESXi Host in given Cluster
47 community.vmware.vmware_portgroup_info:
48 hostname: '{{ vcenter_hostname }}'
49 username: '{{ vcenter_username }}'
50 password: '{{ vcenter_password }}'
51 cluster_name: '{{ cluster_name }}'
52 delegate_to: localhost
53
54 - name: Gather portgroup info about ESXi Host system
55 community.vmware.vmware_portgroup_info:
56 hostname: '{{ vcenter_hostname }}'
57 username: '{{ vcenter_username }}'
58 password: '{{ vcenter_password }}'
59 esxi_hostname: '{{ esxi_hostname }}'
60 delegate_to: localhost
61 '''
62
63 RETURN = r'''
64 hosts_portgroup_info:
65 description: metadata about host's portgroup configuration
66 returned: on success
67 type: dict
68 sample: {
69 "esx01": [
70 {
71 "failback": true,
72 "failover_active": ["vmnic0", "vmnic1"],
73 "failover_standby": [],
74 "failure_detection": "link_status_only",
75 "lb": "loadbalance_srcid",
76 "notify": true,
77 "portgroup": "Management Network",
78 "security": [false, false, false],
79 "ts": "No override",
80 "vlan_id": 0,
81 "vswitch": "vSwitch0"
82 },
83 {
84 "failback": true,
85 "failover_active": ["vmnic2"],
86 "failover_standby": ["vmnic3"],
87 "failure_detection": "No override",
88 "lb": "No override",
89 "notify": true,
90 "portgroup": "vMotion",
91 "security": [false, false, false],
92 "ts": "No override",
93 "vlan_id": 33,
94 "vswitch": "vSwitch1"
95 }
96 ]
97 }
98 '''
99
100 from ansible.module_utils.basic import AnsibleModule
101 from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi
102 from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec
103
104
105 class PortgroupInfoManager(PyVmomi):
106 """Class to manage Port Group info"""
107
108 def __init__(self, module):
109 super(PortgroupInfoManager, self).__init__(module)
110 cluster_name = self.params.get('cluster_name', None)
111 esxi_host_name = self.params.get('esxi_hostname', None)
112 self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name)
113 if not self.hosts:
114 self.module.fail_json(msg="Failed to find host system.")
115 self.policies = self.params.get('policies')
116
117 @staticmethod
118 def normalize_pg_info(portgroup_obj, policy_info):
119 """Create Port Group information"""
120 pg_info_dict = dict()
121 spec = portgroup_obj.spec
122 pg_info_dict['portgroup'] = spec.name
123 pg_info_dict['vlan_id'] = spec.vlanId
124 pg_info_dict['vswitch'] = spec.vswitchName
125
126 if policy_info:
127 # Security info
128 if spec.policy.security:
129 promiscuous_mode = spec.policy.security.allowPromiscuous
130 mac_changes = spec.policy.security.macChanges
131 forged_transmits = spec.policy.security.forgedTransmits
132 pg_info_dict['security'] = (
133 ["No override" if promiscuous_mode is None else promiscuous_mode,
134 "No override" if mac_changes is None else mac_changes,
135 "No override" if forged_transmits is None else forged_transmits]
136 )
137 else:
138 pg_info_dict['security'] = ["No override", "No override", "No override"]
139
140 # Traffic Shaping info
141 if spec.policy.shapingPolicy and spec.policy.shapingPolicy.enabled is not None:
142 pg_info_dict['ts'] = portgroup_obj.spec.policy.shapingPolicy.enabled
143 else:
144 pg_info_dict['ts'] = "No override"
145
146 # Teaming and failover info
147 if spec.policy.nicTeaming:
148 if spec.policy.nicTeaming.policy is None:
149 pg_info_dict['lb'] = "No override"
150 else:
151 pg_info_dict['lb'] = spec.policy.nicTeaming.policy
152 if spec.policy.nicTeaming.notifySwitches is None:
153 pg_info_dict['notify'] = "No override"
154 else:
155 pg_info_dict['notify'] = spec.policy.nicTeaming.notifySwitches
156 if spec.policy.nicTeaming.rollingOrder is None:
157 pg_info_dict['failback'] = "No override"
158 else:
159 pg_info_dict['failback'] = not spec.policy.nicTeaming.rollingOrder
160 if spec.policy.nicTeaming.nicOrder is None:
161 pg_info_dict['failover_active'] = "No override"
162 pg_info_dict['failover_standby'] = "No override"
163 else:
164 pg_info_dict['failover_active'] = spec.policy.nicTeaming.nicOrder.activeNic
165 pg_info_dict['failover_standby'] = spec.policy.nicTeaming.nicOrder.standbyNic
166 if spec.policy.nicTeaming.failureCriteria is None:
167 pg_info_dict['failure_detection'] = "No override"
168 else:
169 if spec.policy.nicTeaming.failureCriteria.checkBeacon:
170 pg_info_dict['failure_detection'] = "beacon_probing"
171 else:
172 pg_info_dict['failure_detection'] = "link_status_only"
173 else:
174 pg_info_dict['lb'] = "No override"
175 pg_info_dict['notify'] = "No override"
176 pg_info_dict['failback'] = "No override"
177 pg_info_dict['failover_active'] = "No override"
178 pg_info_dict['failover_standby'] = "No override"
179 pg_info_dict['failure_detection'] = "No override"
180
181 return pg_info_dict
182
183 def gather_host_portgroup_info(self):
184 """Gather Port Group info per ESXi host"""
185 hosts_pg_info = dict()
186 for host in self.hosts:
187 pgs = host.config.network.portgroup
188 hosts_pg_info[host.name] = []
189 for portgroup in pgs:
190 hosts_pg_info[host.name].append(
191 self.normalize_pg_info(portgroup_obj=portgroup, policy_info=self.policies)
192 )
193 return hosts_pg_info
194
195
196 def main():
197 """Main"""
198 argument_spec = base_argument_spec()
199 argument_spec.update(
200 cluster_name=dict(type='str', required=False),
201 esxi_hostname=dict(type='str', required=False),
202 policies=dict(type='bool', required=False, default=False),
203 )
204
205 module = AnsibleModule(
206 argument_spec=argument_spec,
207 required_one_of=[
208 ['cluster_name', 'esxi_hostname'],
209 ],
210 supports_check_mode=True
211 )
212
213 host_pg_mgr = PortgroupInfoManager(module)
214 module.exit_json(changed=False, hosts_portgroup_info=host_pg_mgr.gather_host_portgroup_info())
215
216
217 if __name__ == "__main__":
218 main()