| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2020, sky-joker |
| 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 |
DOCUMENTATION = r''' |
| 12 |
module: vmware_host_iscsi_info |
| 13 |
short_description: Gather iSCSI configuration information of ESXi host |
| 14 |
author: |
| 15 |
- sky-joker (@sky-joker) |
| 16 |
description: |
| 17 |
- This module can be used to gather information about the iSCSI configuration of the ESXi host. |
| 18 |
options: |
| 19 |
esxi_hostname: |
| 20 |
description: |
| 21 |
- The ESXi hostname on which to gather iSCSI settings. |
| 22 |
type: str |
| 23 |
required: true |
| 24 |
extends_documentation_fragment: |
| 25 |
- vmware.vmware.base_options |
| 26 |
''' |
| 27 |
|
| 28 |
EXAMPLES = r''' |
| 29 |
- name: Gather iSCSI configuration information of ESXi host |
| 30 |
community.vmware.vmware_host_iscsi_info: |
| 31 |
hostname: "{{ vcenter_hostname }}" |
| 32 |
username: "{{ vcenter_username }}" |
| 33 |
password: "{{ vcenter_password }}" |
| 34 |
esxi_hostname: "{{ esxi_hostname }}" |
| 35 |
register: iscsi_info |
| 36 |
''' |
| 37 |
|
| 38 |
RETURN = r''' |
| 39 |
iscsi_properties: |
| 40 |
description: dictionary of current iSCSI information |
| 41 |
returned: always |
| 42 |
type: dict |
| 43 |
sample: >- |
| 44 |
{ |
| 45 |
"iscsi_alias": "", |
| 46 |
"iscsi_authentication_properties": { |
| 47 |
"_vimtype": "vim.host.InternetScsiHba.AuthenticationProperties", |
| 48 |
"chapAuthEnabled": false, |
| 49 |
"chapAuthenticationType": "chapProhibited", |
| 50 |
"chapInherited": null, |
| 51 |
"chapName": "", |
| 52 |
"chapSecret": "XXXXXXXXX", |
| 53 |
"mutualChapAuthenticationType": "chapProhibited", |
| 54 |
"mutualChapInherited": null, |
| 55 |
"mutualChapName": "", |
| 56 |
"mutualChapSecret": "XXXXXXXXX" |
| 57 |
}, |
| 58 |
"iscsi_enabled": true, |
| 59 |
"iscsi_name": "iqn.1998-01.com.vmware:esxi-033f58ee", |
| 60 |
"iscsi_send_targets": [ |
| 61 |
{ |
| 62 |
"address": "192.168.0.1", |
| 63 |
"authenticationProperties": { |
| 64 |
"_vimtype": "vim.host.InternetScsiHba.AuthenticationProperties", |
| 65 |
"chapAuthEnabled": false, |
| 66 |
"chapAuthenticationType": "chapProhibited", |
| 67 |
"chapInherited": true, |
| 68 |
"chapName": "", |
| 69 |
"chapSecret": "XXXXXXXXX", |
| 70 |
"mutualChapAuthenticationType": "chapProhibited", |
| 71 |
"mutualChapInherited": true, |
| 72 |
"mutualChapName": "", |
| 73 |
"mutualChapSecret": "XXXXXXXXX" |
| 74 |
}, |
| 75 |
"port": 3260 |
| 76 |
} |
| 77 |
], |
| 78 |
"iscsi_static_targets": [ |
| 79 |
{ |
| 80 |
"address": "192.168.0.1", |
| 81 |
"authenticationProperties": { |
| 82 |
"_vimtype": "vim.host.InternetScsiHba.AuthenticationProperties", |
| 83 |
"chapAuthEnabled": false, |
| 84 |
"chapAuthenticationType": "chapProhibited", |
| 85 |
"chapInherited": true, |
| 86 |
"chapName": "", |
| 87 |
"chapSecret": "XXXXXXXXX", |
| 88 |
"mutualChapAuthenticationType": "chapProhibited", |
| 89 |
"mutualChapInherited": true, |
| 90 |
"mutualChapName": "", |
| 91 |
"mutualChapSecret": "XXXXXXXXX" |
| 92 |
}, |
| 93 |
"iscsi_name": "iqn.2004-04.com.qnap:tvs-673:iscsi.vm3.2c580e", |
| 94 |
"port": 3260 |
| 95 |
} |
| 96 |
], |
| 97 |
"port_bind": [], |
| 98 |
"vmhba_name": "vmhba65" |
| 99 |
} |
| 100 |
detected_iscsi_drives: |
| 101 |
description: |
| 102 |
- list of detected iSCSI drive |
| 103 |
returned: always |
| 104 |
type: list |
| 105 |
sample: >- |
| 106 |
[ |
| 107 |
{ |
| 108 |
"address": [ |
| 109 |
"192.168.0.57:3260" |
| 110 |
], |
| 111 |
"canonical_name": "naa.60014055f198fb3d0cb4bd7ae1f802e1", |
| 112 |
"iscsi_name": "iqn.2021-03.local.iscsi-target:iscsi-storage.target0" |
| 113 |
} |
| 114 |
] |
| 115 |
''' |
| 116 |
|
| 117 |
try: |
| 118 |
from pyVmomi import vim |
| 119 |
except ImportError: |
| 120 |
pass |
| 121 |
|
| 122 |
import re |
| 123 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi |
| 124 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 125 |
from ansible.module_utils.basic import AnsibleModule |
| 126 |
|
| 127 |
|
| 128 |
class VMwareHostiScsiInfo(PyVmomi): |
| 129 |
def __init__(self, module): |
| 130 |
super(VMwareHostiScsiInfo, self).__init__(module) |
| 131 |
self.esxi_hostname = self.params['esxi_hostname'] |
| 132 |
|
| 133 |
def get_iscsi_config(self): |
| 134 |
iscsi_enabled = self.host_obj.config.storageDevice.softwareInternetScsiEnabled |
| 135 |
self.existing_system_iscsi_config = { |
| 136 |
'iscsi_enabled': iscsi_enabled |
| 137 |
} |
| 138 |
self.detected_iscsi_drives = [] |
| 139 |
if iscsi_enabled is True: |
| 140 |
for hba in self.host_obj.config.storageDevice.hostBusAdapter: |
| 141 |
if isinstance(hba, vim.host.InternetScsiHba): |
| 142 |
self.existing_system_iscsi_config.update( |
| 143 |
{ |
| 144 |
'vmhba_name': hba.device, |
| 145 |
'iscsi_name': hba.iScsiName, |
| 146 |
'iscsi_alias': hba.iScsiAlias, |
| 147 |
'iscsi_authentication_properties': self.to_json(hba.authenticationProperties) |
| 148 |
} |
| 149 |
) |
| 150 |
|
| 151 |
iscsi_send_targets = [] |
| 152 |
for iscsi_send_target in self.to_json(hba.configuredSendTarget): |
| 153 |
iscsi_send_targets.append({ |
| 154 |
'address': iscsi_send_target['address'], |
| 155 |
'authenticationProperties': iscsi_send_target['authenticationProperties'], |
| 156 |
'port': iscsi_send_target['port'] |
| 157 |
}) |
| 158 |
self.existing_system_iscsi_config['iscsi_send_targets'] = iscsi_send_targets |
| 159 |
|
| 160 |
iscsi_static_targets = [] |
| 161 |
for iscsi_static_target in self.to_json(hba.configuredStaticTarget): |
| 162 |
iscsi_static_targets.append({ |
| 163 |
'iscsi_name': iscsi_static_target['iScsiName'], |
| 164 |
'address': iscsi_static_target['address'], |
| 165 |
'authenticationProperties': iscsi_static_target['authenticationProperties'], |
| 166 |
'port': iscsi_static_target['port'] |
| 167 |
}) |
| 168 |
self.existing_system_iscsi_config['iscsi_static_targets'] = iscsi_static_targets |
| 169 |
|
| 170 |
detected_iscsi_drives_information = [] |
| 171 |
for lun in self.host_obj.config.storageDevice.scsiLun: |
| 172 |
if isinstance(lun, vim.host.ScsiDisk): |
| 173 |
detected_iscsi_drives_information.append({ |
| 174 |
'key': lun.key, |
| 175 |
'canonical_name': lun.canonicalName |
| 176 |
}) |
| 177 |
|
| 178 |
for scsi_adapter in self.host_obj.config.storageDevice.scsiTopology.adapter: |
| 179 |
if isinstance(scsi_adapter, vim.host.ScsiTopology.Interface): |
| 180 |
if re.search(self.existing_system_iscsi_config['vmhba_name'], scsi_adapter.key): |
| 181 |
for target in scsi_adapter.target: |
| 182 |
scsi_lun = target.lun[0].scsiLun |
| 183 |
for scsi_info in detected_iscsi_drives_information: |
| 184 |
if scsi_info['key'] == scsi_lun: |
| 185 |
self.detected_iscsi_drives.append({ |
| 186 |
'iscsi_name': target.transport.iScsiName, |
| 187 |
'canonical_name': scsi_info['canonical_name'], |
| 188 |
'address': target.transport.address |
| 189 |
}) |
| 190 |
|
| 191 |
vnic_devices = [] |
| 192 |
for vnic in self.host_obj.configManager.iscsiManager.QueryBoundVnics(iScsiHbaName=self.existing_system_iscsi_config['vmhba_name']): |
| 193 |
vnic_devices.append(vnic.vnicDevice) |
| 194 |
self.existing_system_iscsi_config['port_bind'] = vnic_devices |
| 195 |
|
| 196 |
def execute(self): |
| 197 |
self.host_obj = self.find_hostsystem_by_name(self.esxi_hostname) |
| 198 |
if not self.host_obj: |
| 199 |
self.module.fail_json(msg="Cannot find the specified ESXi host: %s" % self.esxi_hostname) |
| 200 |
|
| 201 |
self.get_iscsi_config() |
| 202 |
self.module.exit_json(changed=False, iscsi_properties=self.existing_system_iscsi_config, detected_iscsi_drives=self.detected_iscsi_drives) |
| 203 |
|
| 204 |
|
| 205 |
def main(): |
| 206 |
argument_spec = base_argument_spec() |
| 207 |
argument_spec.update( |
| 208 |
esxi_hostname=dict(type='str', required=True) |
| 209 |
) |
| 210 |
|
| 211 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 212 |
|
| 213 |
vmware_host_iscsi_info = VMwareHostiScsiInfo(module) |
| 214 |
vmware_host_iscsi_info.execute() |
| 215 |
|
| 216 |
|
| 217 |
if __name__ == "__main__": |
| 218 |
main() |