Debian Codegraph @

ansible_collections/community/vmware/plugins/modules/vmware_host_vmnic_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_host_vmnic_info
16 short_description: Gathers info about vmnics available on the given ESXi host
17 description:
18 - This module can be used to gather information about vmnics available on the given ESXi host.
19 - If O(cluster_name) is provided, then vmnic information about all hosts from given cluster will be returned.
20 - If O(esxi_hostname) is provided, then vmnic information about given host system will be returned.
21 author:
22 - Abhijeet Kasurde (@Akasurde)
23 - Christian Kotte (@ckotte)
24 options:
25 capabilities:
26 description:
27 - Gather information about general capabilities (Auto negotiation, Wake On LAN, and Network I/O Control).
28 type: bool
29 default: false
30 directpath_io:
31 description:
32 - Gather information about DirectPath I/O capabilities and configuration.
33 type: bool
34 default: false
35 sriov:
36 description:
37 - Gather information about SR-IOV capabilities and configuration.
38 type: bool
39 default: false
40 esxi_hostname:
41 description:
42 - Name of the host system to work with.
43 - Vmnic information about this ESXi server will be returned.
44 - This parameter is required if O(cluster_name) is not specified.
45 type: str
46 cluster_name:
47 description:
48 - Name of the cluster from which all host systems will be used.
49 - Vmnic information about each ESXi server will be returned for the given cluster.
50 - This parameter is required if O(esxi_hostname) is not specified.
51 type: str
52 extends_documentation_fragment:
53 - vmware.vmware.base_options
54
55 '''
56
57 EXAMPLES = r'''
58 - name: Gather info about vmnics of all ESXi Host in the given Cluster
59 community.vmware.vmware_host_vmnic_info:
60 hostname: '{{ vcenter_hostname }}'
61 username: '{{ vcenter_username }}'
62 password: '{{ vcenter_password }}'
63 cluster_name: '{{ cluster_name }}'
64 delegate_to: localhost
65 register: cluster_host_vmnics
66
67 - name: Gather info about vmnics of an ESXi Host
68 community.vmware.vmware_host_vmnic_info:
69 hostname: '{{ vcenter_hostname }}'
70 username: '{{ vcenter_username }}'
71 password: '{{ vcenter_password }}'
72 esxi_hostname: '{{ esxi_hostname }}'
73 delegate_to: localhost
74 register: host_vmnics
75 '''
76
77 RETURN = r'''
78 hosts_vmnics_info:
79 description:
80 - dict with hostname as key and dict with vmnics information as value.
81 - for C(num_vmnics), only NICs starting with vmnic are counted. NICs like vusb* are not counted.
82 returned: hosts_vmnics_info
83 type: dict
84 sample:
85 {
86 "10.76.33.204": {
87 "all": [
88 "vmnic0",
89 "vmnic1"
90 ],
91 "available": [],
92 "dvswitch": {
93 "dvs_0002": [
94 "vmnic1"
95 ]
96 },
97 "num_vmnics": 2,
98 "used": [
99 "vmnic1",
100 "vmnic0"
101 ],
102 "vmnic_details": [
103 {
104 "actual_duplex": "Full Duplex",
105 "actual_speed": 10000,
106 "adapter": "Intel(R) 82599 10 Gigabit Dual Port Network Connection",
107 "configured_duplex": "Auto negotiate",
108 "configured_speed": "Auto negotiate",
109 "device": "vmnic0",
110 "driver": "ixgbe",
111 "lldp_info": {
112 "Aggregated Port ID": "0",
113 "Aggregation Status": "1",
114 "Enabled Capabilities": {
115 "_vimtype": "vim.host.PhysicalNic.CdpDeviceCapability",
116 "host": false,
117 "igmpEnabled": false,
118 "networkSwitch": false,
119 "repeater": false,
120 "router": true,
121 "sourceRouteBridge": false,
122 "transparentBridge": true
123 },
124 "MTU": "9216",
125 "Port Description": "switch port description",
126 "Samples": 18814,
127 "System Description": "omitted from output",
128 "System Name": "sw1",
129 "TimeOut": 30,
130 "Vlan ID": "1"
131 },
132 "location": "0000:01:00.0",
133 "mac": "aa:bb:cc:dd:ee:ff",
134 "status": "Connected",
135 },
136 {
137 "actual_duplex": "Full Duplex",
138 "actual_speed": 10000,
139 "adapter": "Intel(R) 82599 10 Gigabit Dual Port Network Connection",
140 "configured_duplex": "Auto negotiate",
141 "configured_speed": "Auto negotiate",
142 "device": "vmnic1",
143 "driver": "ixgbe",
144 "lldp_info": "N/A",
145 "location": "0000:01:00.1",
146 "mac": "ab:ba:cc:dd:ee:ff",
147 "status": "Connected",
148 },
149 ],
150 "vswitch": {
151 "vSwitch0": [
152 "vmnic0"
153 ]
154 }
155 }
156 }
157 '''
158
159 try:
160 from pyVmomi import vim, vmodl
161 except ImportError:
162 pass
163
164 from ansible.module_utils.basic import AnsibleModule
165 from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi, get_all_objs
166 from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec
167
168
169 class HostVmnicMgr(PyVmomi):
170 """Class to manage vmnic info"""
171
172 def __init__(self, module):
173 super(HostVmnicMgr, self).__init__(module)
174 self.capabilities = self.params.get('capabilities')
175 self.directpath_io = self.params.get('directpath_io')
176 self.sriov = self.params.get('sriov')
177 cluster_name = self.params.get('cluster_name', None)
178 esxi_host_name = self.params.get('esxi_hostname', None)
179 self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name)
180 if not self.hosts:
181 self.module.fail_json(msg="Failed to find host system.")
182
183 def find_dvs_by_uuid(self, uuid=None):
184 """Find DVS by it's UUID"""
185 dvs_obj = None
186 if uuid is None:
187 return dvs_obj
188
189 dvswitches = get_all_objs(self.content, [vim.DistributedVirtualSwitch])
190 for dvs in dvswitches:
191 if dvs.uuid == uuid:
192 dvs_obj = dvs
193 break
194
195 return dvs_obj
196
197 def gather_host_vmnic_info(self):
198 """Gather vmnic info"""
199 hosts_vmnic_info = {}
200 for host in self.hosts:
201 host_vmnic_info = dict(all=[], available=[], used=[], vswitch=dict(), dvswitch=dict())
202 host_nw_system = host.configManager.networkSystem
203 if host_nw_system:
204 nw_config = host_nw_system.networkConfig
205 vmnics = [pnic.device for pnic in nw_config.pnic if pnic.device.startswith('vmnic')]
206 host_vmnic_info['all'] = [pnic.device for pnic in nw_config.pnic]
207 host_vmnic_info['num_vmnics'] = len(vmnics)
208 host_vmnic_info['vmnic_details'] = []
209 for pnic in host.config.network.pnic:
210 pnic_info = dict()
211 if pnic.device.startswith('vmnic'):
212 if pnic.pci:
213 pnic_info['location'] = pnic.pci
214 for pci_device in host.hardware.pciDevice:
215 if pci_device.id == pnic.pci:
216 pnic_info['adapter'] = pci_device.vendorName + ' ' + pci_device.deviceName
217 break
218 else:
219 pnic_info['location'] = 'PCI'
220 pnic_info['device'] = pnic.device
221 pnic_info['driver'] = pnic.driver
222 if pnic.linkSpeed:
223 pnic_info['status'] = 'Connected'
224 pnic_info['actual_speed'] = pnic.linkSpeed.speedMb
225 pnic_info['actual_duplex'] = 'Full Duplex' if pnic.linkSpeed.duplex else 'Half Duplex'
226 try:
227 network_hint = host_nw_system.QueryNetworkHint(pnic.device)
228 for hint in self.to_json(network_hint):
229 if hint.get('lldpInfo'):
230 pnic_info['lldp_info'] = {x['key']: x['value'] for x in hint['lldpInfo'].get('parameter')}
231 else:
232 pnic_info['lldp_info'] = 'N/A'
233 if hint.get('connectedSwitchPort'):
234 pnic_info['cdp_info'] = hint.get('connectedSwitchPort')
235 else:
236 pnic_info['cdp_info'] = 'N/A'
237 except (vmodl.fault.HostNotConnected, vmodl.fault.HostNotReachable):
238 pnic_info['lldp_info'] = 'N/A'
239 pnic_info['cdp_info'] = 'N/A'
240 else:
241 pnic_info['status'] = 'Disconnected'
242 pnic_info['actual_speed'] = 'N/A'
243 pnic_info['actual_duplex'] = 'N/A'
244 pnic_info['lldp_info'] = 'N/A'
245 pnic_info['cdp_info'] = 'N/A'
246 if pnic.spec.linkSpeed:
247 pnic_info['configured_speed'] = pnic.spec.linkSpeed.speedMb
248 pnic_info['configured_duplex'] = 'Full Duplex' if pnic.spec.linkSpeed.duplex else 'Half Duplex'
249 else:
250 pnic_info['configured_speed'] = 'Auto negotiate'
251 pnic_info['configured_duplex'] = 'Auto negotiate'
252 pnic_info['mac'] = pnic.mac
253 # General NIC capabilities
254 if self.capabilities:
255 pnic_info['nioc_status'] = 'Allowed' if pnic.resourcePoolSchedulerAllowed else 'Not allowed'
256 pnic_info['auto_negotiation_supported'] = pnic.autoNegotiateSupported
257 pnic_info['wake_on_lan_supported'] = pnic.wakeOnLanSupported
258 # DirectPath I/O and SR-IOV capabilities and configuration
259 if self.directpath_io:
260 pnic_info['directpath_io_supported'] = pnic.vmDirectPathGen2Supported
261 if self.directpath_io or self.sriov:
262 if pnic.pci:
263 for pci_device in host.configManager.pciPassthruSystem.pciPassthruInfo:
264 if pci_device.id == pnic.pci:
265 if self.directpath_io:
266 pnic_info['passthru_enabled'] = pci_device.passthruEnabled
267 pnic_info['passthru_capable'] = pci_device.passthruCapable
268 pnic_info['passthru_active'] = pci_device.passthruActive
269 if self.sriov:
270 try:
271 if pci_device.sriovCapable:
272 pnic_info['sriov_status'] = (
273 'Enabled' if pci_device.sriovEnabled else 'Disabled'
274 )
275 pnic_info['sriov_active'] = \
276 pci_device.sriovActive
277 pnic_info['sriov_virt_functions'] = \
278 pci_device.numVirtualFunction
279 pnic_info['sriov_virt_functions_requested'] = \
280 pci_device.numVirtualFunctionRequested
281 pnic_info['sriov_virt_functions_supported'] = \
282 pci_device.maxVirtualFunctionSupported
283 else:
284 pnic_info['sriov_status'] = 'Not supported'
285 except AttributeError:
286 pnic_info['sriov_status'] = 'Not supported'
287 host_vmnic_info['vmnic_details'].append(pnic_info)
288
289 vswitch_vmnics = []
290 proxy_switch_vmnics = []
291 if nw_config.vswitch:
292 for vswitch in nw_config.vswitch:
293 host_vmnic_info['vswitch'][vswitch.name] = []
294 # Workaround for "AttributeError: 'NoneType' object has no attribute 'nicDevice'"
295 # this issue doesn't happen every time; vswitch.spec.bridge.nicDevice exists!
296 try:
297 for vnic in vswitch.spec.bridge.nicDevice:
298 vswitch_vmnics.append(vnic)
299 host_vmnic_info['vswitch'][vswitch.name].append(vnic)
300 except AttributeError:
301 pass
302
303 if nw_config.proxySwitch:
304 for proxy_config in nw_config.proxySwitch:
305 dvs_obj = self.find_dvs_by_uuid(uuid=proxy_config.uuid)
306 if dvs_obj:
307 host_vmnic_info['dvswitch'][dvs_obj.name] = []
308 for proxy_nic in proxy_config.spec.backing.pnicSpec:
309 proxy_switch_vmnics.append(proxy_nic.pnicDevice)
310 if dvs_obj:
311 host_vmnic_info['dvswitch'][dvs_obj.name].append(proxy_nic.pnicDevice)
312
313 used_vmics = proxy_switch_vmnics + vswitch_vmnics
314 host_vmnic_info['used'] = used_vmics
315 host_vmnic_info['available'] = [pnic.device for pnic in nw_config.pnic if pnic.device not in used_vmics]
316
317 hosts_vmnic_info[host.name] = host_vmnic_info
318 return hosts_vmnic_info
319
320
321 def main():
322 """Main"""
323 argument_spec = base_argument_spec()
324 argument_spec.update(
325 cluster_name=dict(type='str', required=False),
326 esxi_hostname=dict(type='str', required=False),
327 capabilities=dict(type='bool', required=False, default=False),
328 directpath_io=dict(type='bool', required=False, default=False),
329 sriov=dict(type='bool', required=False, default=False),
330 )
331
332 module = AnsibleModule(
333 argument_spec=argument_spec,
334 required_one_of=[
335 ['cluster_name', 'esxi_hostname'],
336 ],
337 supports_check_mode=True,
338 )
339
340 host_vmnic_mgr = HostVmnicMgr(module)
341 module.exit_json(changed=False, hosts_vmnics_info=host_vmnic_mgr.gather_host_vmnic_info())
342
343
344 if __name__ == "__main__":
345 main()