| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2020, Matt Proud |
| 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 = ''' |
| 12 |
--- |
| 13 |
module: vmware_host_disk_info |
| 14 |
short_description: Gathers information about disks attached to given ESXi host/s. |
| 15 |
description: |
| 16 |
- This module returns information about disks attached to given ESXi host/s |
| 17 |
- If O(cluster_name) is provided, then disk information about all hosts from the given cluster will be returned. |
| 18 |
- If O(esxi_hostname) is provided, then disk information about the given host system will be returned. |
| 19 |
author: |
| 20 |
- Matt Proud (@laidbackware) |
| 21 |
options: |
| 22 |
cluster_name: |
| 23 |
description: |
| 24 |
- Name of the cluster from which the ESXi host belong to. |
| 25 |
- If O(esxi_hostname) is not given, this parameter is required. |
| 26 |
type: str |
| 27 |
esxi_hostname: |
| 28 |
description: |
| 29 |
- ESXi hostname to gather information from. |
| 30 |
- If O(cluster_name) is not given, this parameter is required. |
| 31 |
type: str |
| 32 |
extends_documentation_fragment: |
| 33 |
- vmware.vmware.base_options |
| 34 |
''' |
| 35 |
|
| 36 |
EXAMPLES = ''' |
| 37 |
- name: Gather info about vmhbas of all ESXi Host in the given Cluster |
| 38 |
community.vmware.vmware_host_disk_info: |
| 39 |
hostname: '{{ vcenter_hostname }}' |
| 40 |
username: '{{ vcenter_username }}' |
| 41 |
password: '{{ vcenter_password }}' |
| 42 |
cluster_name: '{{ cluster_name }}' |
| 43 |
delegate_to: localhost |
| 44 |
register: cluster_host_vmhbas |
| 45 |
|
| 46 |
- name: Gather info about vmhbas of an ESXi Host |
| 47 |
community.vmware.vmware_host_disk_info: |
| 48 |
hostname: '{{ vcenter_hostname }}' |
| 49 |
username: '{{ vcenter_username }}' |
| 50 |
password: '{{ vcenter_password }}' |
| 51 |
esxi_hostname: '{{ esxi_hostname }}' |
| 52 |
delegate_to: localhost |
| 53 |
register: host_vmhbas |
| 54 |
''' |
| 55 |
|
| 56 |
RETURN = ''' |
| 57 |
hosts_disk_info: |
| 58 |
description: list of information for all disks attached to each ESXi host |
| 59 |
returned: always |
| 60 |
type: list |
| 61 |
sample: >- |
| 62 |
"192.168.0.182": [ |
| 63 |
{ |
| 64 |
"canonical_name": "naa.6000c296ed6217bd61df35622eb21a3a", |
| 65 |
"capacity_mb": 4096, |
| 66 |
"device_path": "/vmfs/devices/disks/naa.6000c296ed6217bd61df35622eb21a3a", |
| 67 |
"device_type": "disk", |
| 68 |
"device_ctd_list": [ |
| 69 |
"vmhba0:C0:T1:L0" |
| 70 |
], |
| 71 |
"disk_uid": "key-vim.host.ScsiDisk-02000000006000c296ed6217bd61df35622eb21a3a566972747561", |
| 72 |
"display_name": "Local VMware Disk (naa.6000c296ed6217bd61df35622eb21a3a)" |
| 73 |
}, |
| 74 |
{ |
| 75 |
"canonical_name": "naa.6000c2968ad7142d93faae527fe8822b", |
| 76 |
"capacity_mb": 204800, |
| 77 |
"device_path": "/vmfs/devices/disks/naa.6000c2968ad7142d93faae527fe8822b", |
| 78 |
"device_type": "disk", |
| 79 |
"device_ctd_list": [ |
| 80 |
"vmhba0:C0:T3:L0" |
| 81 |
], |
| 82 |
"disk_uid": "key-vim.host.ScsiDisk-02000000006000c2968ad7142d93faae527fe8822b566972747561", |
| 83 |
"display_name": "Local VMware Disk (naa.6000c2968ad7142d93faae527fe8822b)" |
| 84 |
},] |
| 85 |
''' |
| 86 |
|
| 87 |
from ansible.module_utils.basic import AnsibleModule |
| 88 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi |
| 89 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 90 |
|
| 91 |
|
| 92 |
class HostDiskInfo(PyVmomi): |
| 93 |
"""Class to return host disk info""" |
| 94 |
|
| 95 |
def __init__(self, module): |
| 96 |
super(HostDiskInfo, 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 |
if not self.hosts: |
| 101 |
self.module.fail_json(msg="Failed to find host system.") |
| 102 |
|
| 103 |
def gather_host_disk_info(self): |
| 104 |
hosts_disk_info = {} |
| 105 |
for host in self.hosts: |
| 106 |
host_disk_info = [] |
| 107 |
storage_system = host.configManager.storageSystem.storageDeviceInfo |
| 108 |
# Collect target lookup for naa devices |
| 109 |
lun_lookup = {} |
| 110 |
for lun in storage_system.multipathInfo.lun: |
| 111 |
key = lun.lun |
| 112 |
paths = [] |
| 113 |
for path in lun.path: |
| 114 |
paths.append(path.name) |
| 115 |
lun_lookup[key] = paths |
| 116 |
|
| 117 |
for disk in storage_system.scsiLun: |
| 118 |
canonical_name = disk.canonicalName |
| 119 |
try: |
| 120 |
capacity = int(disk.capacity.block * disk.capacity.blockSize / 1048576) |
| 121 |
except AttributeError: |
| 122 |
capacity = 0 |
| 123 |
try: |
| 124 |
device_path = disk.devicePath |
| 125 |
except AttributeError: |
| 126 |
device_path = "" |
| 127 |
device_type = disk.deviceType |
| 128 |
display_name = disk.displayName |
| 129 |
disk_uid = disk.key |
| 130 |
device_ctd_list = lun_lookup[disk_uid] |
| 131 |
|
| 132 |
disk_dict = {"capacity_mb": capacity, |
| 133 |
"device_path": device_path, |
| 134 |
"device_type": device_type, |
| 135 |
"display_name": display_name, |
| 136 |
"disk_uid": disk_uid, |
| 137 |
"device_ctd_list": device_ctd_list, |
| 138 |
"canonical_name": canonical_name} |
| 139 |
host_disk_info.append(disk_dict) |
| 140 |
|
| 141 |
hosts_disk_info[host.name] = host_disk_info |
| 142 |
|
| 143 |
return hosts_disk_info |
| 144 |
|
| 145 |
|
| 146 |
def main(): |
| 147 |
"""Main""" |
| 148 |
argument_spec = base_argument_spec() |
| 149 |
argument_spec.update( |
| 150 |
cluster_name=dict(type='str', required=False), |
| 151 |
esxi_hostname=dict(type='str', required=False), |
| 152 |
) |
| 153 |
|
| 154 |
module = AnsibleModule( |
| 155 |
argument_spec=argument_spec, |
| 156 |
required_one_of=[ |
| 157 |
['cluster_name', 'esxi_hostname'], |
| 158 |
], |
| 159 |
supports_check_mode=True, |
| 160 |
) |
| 161 |
|
| 162 |
host_disk_mgr = HostDiskInfo(module) |
| 163 |
module.exit_json(changed=False, hosts_disk_info=host_disk_mgr.gather_host_disk_info()) |
| 164 |
|
| 165 |
|
| 166 |
if __name__ == "__main__": |
| 167 |
main() |