| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, Christian Kotte <[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_vmhba_info |
| 15 |
short_description: Gathers info about vmhbas available on the given ESXi host |
| 16 |
description: |
| 17 |
- This module can be used to gather information about vmhbas available on the given ESXi host. |
| 18 |
- If O(cluster_name) is provided, then vmhba information about all hosts from given cluster will be returned. |
| 19 |
- If O(esxi_hostname) is provided, then vmhba information about given host system will be returned. |
| 20 |
author: |
| 21 |
- Christian Kotte (@ckotte) |
| 22 |
options: |
| 23 |
esxi_hostname: |
| 24 |
description: |
| 25 |
- Name of the host system to work with. |
| 26 |
- Vmhba information about this ESXi server will be returned. |
| 27 |
- This parameter is required if O(cluster_name) is not specified. |
| 28 |
type: str |
| 29 |
cluster_name: |
| 30 |
description: |
| 31 |
- Name of the cluster from which all host systems will be used. |
| 32 |
- Vmhba information about each ESXi server will be returned for the given cluster. |
| 33 |
- This parameter is required if O(esxi_hostname) is not specified. |
| 34 |
type: str |
| 35 |
extends_documentation_fragment: |
| 36 |
- vmware.vmware.base_options |
| 37 |
|
| 38 |
''' |
| 39 |
|
| 40 |
EXAMPLES = r''' |
| 41 |
- name: Gather info about vmhbas of all ESXi Host in the given Cluster |
| 42 |
community.vmware.vmware_host_vmhba_info: |
| 43 |
hostname: '{{ vcenter_hostname }}' |
| 44 |
username: '{{ vcenter_username }}' |
| 45 |
password: '{{ vcenter_password }}' |
| 46 |
cluster_name: '{{ cluster_name }}' |
| 47 |
delegate_to: localhost |
| 48 |
register: cluster_host_vmhbas |
| 49 |
|
| 50 |
- name: Gather info about vmhbas of an ESXi Host |
| 51 |
community.vmware.vmware_host_vmhba_info: |
| 52 |
hostname: '{{ vcenter_hostname }}' |
| 53 |
username: '{{ vcenter_username }}' |
| 54 |
password: '{{ vcenter_password }}' |
| 55 |
esxi_hostname: '{{ esxi_hostname }}' |
| 56 |
delegate_to: localhost |
| 57 |
register: host_vmhbas |
| 58 |
''' |
| 59 |
|
| 60 |
RETURN = r''' |
| 61 |
hosts_vmhbas_info: |
| 62 |
description: |
| 63 |
- dict with hostname as key and dict with vmhbas information as value. |
| 64 |
returned: hosts_vmhbas_info |
| 65 |
type: dict |
| 66 |
sample: |
| 67 |
{ |
| 68 |
"10.76.33.204": { |
| 69 |
"vmhba_details": [ |
| 70 |
{ |
| 71 |
"adapter": "HPE Smart Array P440ar", |
| 72 |
"bus": 3, |
| 73 |
"device": "vmhba0", |
| 74 |
"driver": "nhpsa", |
| 75 |
"location": "0000:03:00.0", |
| 76 |
"model": "Smart Array P440ar", |
| 77 |
"node_wwn": "50:01:43:80:37:18:9e:a0", |
| 78 |
"status": "unknown", |
| 79 |
"type": "SAS" |
| 80 |
}, |
| 81 |
{ |
| 82 |
"adapter": "QLogic Corp ISP2532-based 8Gb Fibre Channel to PCI Express HBA", |
| 83 |
"bus": 5, |
| 84 |
"device": "vmhba1", |
| 85 |
"driver": "qlnativefc", |
| 86 |
"location": "0000:05:00.0", |
| 87 |
"model": "ISP2532-based 8Gb Fibre Channel to PCI Express HBA", |
| 88 |
"node_wwn": "57:64:96:32:15:90:23:95:82", |
| 89 |
"port_type": "unknown", |
| 90 |
"port_wwn": "57:64:96:32:15:90:23:95:82", |
| 91 |
"speed": 8, |
| 92 |
"status": "online", |
| 93 |
"type": "Fibre Channel" |
| 94 |
}, |
| 95 |
{ |
| 96 |
"adapter": "QLogic Corp ISP2532-based 8Gb Fibre Channel to PCI Express HBA", |
| 97 |
"bus": 8, |
| 98 |
"device": "vmhba2", |
| 99 |
"driver": "qlnativefc", |
| 100 |
"location": "0000:08:00.0", |
| 101 |
"model": "ISP2532-based 8Gb Fibre Channel to PCI Express HBA", |
| 102 |
"node_wwn": "57:64:96:32:15:90:23:95:21", |
| 103 |
"port_type": "unknown", |
| 104 |
"port_wwn": "57:64:96:32:15:90:23:95:21", |
| 105 |
"speed": 8, |
| 106 |
"status": "online", |
| 107 |
"type": "Fibre Channel" |
| 108 |
} |
| 109 |
], |
| 110 |
} |
| 111 |
} |
| 112 |
''' |
| 113 |
|
| 114 |
try: |
| 115 |
from pyVmomi import vim |
| 116 |
except ImportError: |
| 117 |
pass |
| 118 |
|
| 119 |
from ansible.module_utils.basic import AnsibleModule |
| 120 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi |
| 121 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 122 |
|
| 123 |
|
| 124 |
class HostVmhbaMgr(PyVmomi): |
| 125 |
"""Class to manage vmhba info""" |
| 126 |
|
| 127 |
def __init__(self, module): |
| 128 |
super(HostVmhbaMgr, self).__init__(module) |
| 129 |
cluster_name = self.params.get('cluster_name', None) |
| 130 |
esxi_host_name = self.params.get('esxi_hostname', None) |
| 131 |
self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name) |
| 132 |
if not self.hosts: |
| 133 |
self.module.fail_json(msg="Failed to find host system.") |
| 134 |
|
| 135 |
def gather_host_vmhba_info(self): |
| 136 |
"""Gather vmhba info""" |
| 137 |
hosts_vmhba_info = {} |
| 138 |
for host in self.hosts: |
| 139 |
host_vmhba_info = dict() |
| 140 |
host_st_system = host.configManager.storageSystem |
| 141 |
if host_st_system: |
| 142 |
device_info = host_st_system.storageDeviceInfo |
| 143 |
host_vmhba_info['vmhba_details'] = [] |
| 144 |
for hba in device_info.hostBusAdapter: |
| 145 |
hba_info = dict() |
| 146 |
if hba.pci: |
| 147 |
hba_info['location'] = hba.pci |
| 148 |
for pci_device in host.hardware.pciDevice: |
| 149 |
if pci_device.id == hba.pci: |
| 150 |
hba_info['adapter'] = pci_device.vendorName + ' ' + pci_device.deviceName |
| 151 |
break |
| 152 |
else: |
| 153 |
hba_info['location'] = 'PCI' |
| 154 |
hba_info['device'] = hba.device |
| 155 |
# contains type as string in format of 'key-vim.host.FibreChannelHba-vmhba1' |
| 156 |
hba_type = hba.key.split(".")[-1].split("-")[0] |
| 157 |
if hba_type == 'SerialAttachedHba': |
| 158 |
hba_info['type'] = 'SAS' |
| 159 |
elif hba_type == 'FibreChannelHba': |
| 160 |
hba_info['type'] = 'Fibre Channel' |
| 161 |
else: |
| 162 |
hba_info['type'] = hba_type |
| 163 |
hba_info['bus'] = hba.bus |
| 164 |
hba_info['status'] = hba.status |
| 165 |
hba_info['model'] = hba.model |
| 166 |
hba_info['driver'] = hba.driver |
| 167 |
try: |
| 168 |
if isinstance(hba, (vim.host.FibreChannelHba, vim.host.FibreChannelOverEthernetHba)): |
| 169 |
hba_info['node_wwn'] = self.format_number('%X' % hba.nodeWorldWideName) |
| 170 |
else: |
| 171 |
hba_info['node_wwn'] = self.format_number(hba.nodeWorldWideName) |
| 172 |
except AttributeError: |
| 173 |
pass |
| 174 |
try: |
| 175 |
if isinstance(hba, (vim.host.FibreChannelHba, vim.host.FibreChannelOverEthernetHba)): |
| 176 |
hba_info['port_wwn'] = self.format_number('%X' % hba.portWorldWideName) |
| 177 |
else: |
| 178 |
hba_info['port_wwn'] = self.format_number(hba.portWorldWideName) |
| 179 |
except AttributeError: |
| 180 |
pass |
| 181 |
try: |
| 182 |
hba_info['port_type'] = hba.portType |
| 183 |
except AttributeError: |
| 184 |
pass |
| 185 |
try: |
| 186 |
hba_info['speed'] = hba.speed |
| 187 |
except AttributeError: |
| 188 |
pass |
| 189 |
host_vmhba_info['vmhba_details'].append(hba_info) |
| 190 |
|
| 191 |
hosts_vmhba_info[host.name] = host_vmhba_info |
| 192 |
return hosts_vmhba_info |
| 193 |
|
| 194 |
@staticmethod |
| 195 |
def format_number(number): |
| 196 |
"""Format number""" |
| 197 |
string = str(number) |
| 198 |
return ':'.join(a + b for a, b in zip(string[::2], string[1::2])) |
| 199 |
|
| 200 |
|
| 201 |
def main(): |
| 202 |
"""Main""" |
| 203 |
argument_spec = base_argument_spec() |
| 204 |
argument_spec.update( |
| 205 |
cluster_name=dict(type='str', required=False), |
| 206 |
esxi_hostname=dict(type='str', required=False), |
| 207 |
) |
| 208 |
|
| 209 |
module = AnsibleModule( |
| 210 |
argument_spec=argument_spec, |
| 211 |
required_one_of=[ |
| 212 |
['cluster_name', 'esxi_hostname'], |
| 213 |
], |
| 214 |
supports_check_mode=True, |
| 215 |
) |
| 216 |
|
| 217 |
host_vmhba_mgr = HostVmhbaMgr(module) |
| 218 |
module.exit_json(changed=False, hosts_vmhbas_info=host_vmhba_mgr.gather_host_vmhba_info()) |
| 219 |
|
| 220 |
|
| 221 |
if __name__ == "__main__": |
| 222 |
main() |