Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2018, Ansible Project
5 # Copyright: (c) 2020, Abhijeet Kasurde <[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_scsidisk_info
16 short_description: Gather information about SCSI disk attached to the given ESXi
17 description:
18 - This module can be used to gather information about SCSI disk attached to the given ESXi.
19 author:
20 - Abhijeet Kasurde (@Akasurde)
21 options:
22 esxi_hostname:
23 description:
24 - Name of the host system to work with.
25 - SCSI disk information about this ESXi server will be returned.
26 - This parameter is required if O(cluster_name) is not specified.
27 type: str
28 cluster_name:
29 description:
30 - Name of the cluster from which all host systems will be used.
31 - SCSI disk information about each ESXi server will be returned for the given cluster.
32 - This parameter is required if O(esxi_hostname) is not specified.
33 type: str
34 extends_documentation_fragment:
35 - vmware.vmware.base_options
36 '''
37
38 EXAMPLES = r'''
39 - name: Gather information SCSI disk attached to the given ESXi
40 community.vmware.vmware_host_scsidisk_info:
41 hostname: '{{ vcenter_hostname }}'
42 username: '{{ vcenter_username }}'
43 password: '{{ vcenter_password }}'
44 esxi_hostname: '{{ esxi_hostname }}'
45 delegate_to: localhost
46
47 - name: Gather information of all host systems from the given cluster
48 community.vmware.vmware_host_scsidisk_info:
49 hostname: '{{ vcenter_hostname }}'
50 username: '{{ vcenter_username }}'
51 password: '{{ vcenter_password }}'
52 cluster_name: '{{ cluster_name }}'
53 delegate_to: localhost
54 '''
55
56 RETURN = r'''
57 hosts_scsidisk_info:
58 description: metadata about host system SCSI disk information
59 returned: always
60 type: dict
61 sample: {
62 "10.65.201.106": [
63 {
64 "block": 41943040,
65 "block_size": 512,
66 "canonical_name": "t10.ATA_QEMU_HARDDISK_QM00001_",
67 "device_name": "/vmfs/devices/disks/t10.ATA_QEMU_HARDDISK_QM00001_",
68 "device_path": "/vmfs/devices/disks/t10.ATA_QEMU_HARDDISK_QM00001_",
69 "device_type": "disk",
70 "display_name": "Local ATA Disk (t10.ATA_QEMU_HARDDISK_QM00001_)",
71 "key": "key-vim.host.ScsiDisk-0100000000514d30303030312020202020202020202020202051454d552048",
72 "local_disk": true,
73 "lun_type": "disk",
74 "model": "QEMU HARDDISK ",
75 "perenniallyReserved": null,
76 "protocol_endpoint": false,
77 "revision": "1.5.",
78 "scsi_disk_type": "native512",
79 "scsi_level": 5,
80 "serial_number": "unavailable",
81 "ssd": false,
82 "uuid": "0100000000514d30303030312020202020202020202020202051454d552048",
83 "vStorageSupport": "vStorageUnsupported",
84 "vendor": "ATA "
85 }
86 ]
87 }
88 '''
89
90 from ansible.module_utils.basic import AnsibleModule
91 from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi
92 from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec
93
94
95 class VmwareHostDiskManager(PyVmomi):
96 def __init__(self, module):
97 super(VmwareHostDiskManager, self).__init__(module)
98 cluster_name = self.params.get('cluster_name')
99 esxi_host_name = self.params.get('esxi_hostname')
100 self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name)
101 if not self.hosts:
102 self.module.fail_json(msg="Failed to find host system with given configuration.")
103
104 def gather_disk_info(self):
105 """
106 Gather information about SCSI disks
107
108 """
109 results = dict(changed=False, hosts_scsidisk_info=dict())
110 for host in self.hosts:
111 disk_info = []
112 storage_system = host.configManager.storageSystem
113 for disk in storage_system.storageDeviceInfo.scsiLun:
114 temp_disk_info = {
115 'device_name': disk.deviceName,
116 'device_type': disk.deviceType,
117 'key': disk.key,
118 'uuid': disk.uuid,
119 'canonical_name': disk.canonicalName,
120 'display_name': disk.displayName,
121 'lun_type': disk.lunType,
122 'vendor': disk.vendor,
123 'model': disk.model,
124 'revision': disk.revision,
125 'scsi_level': disk.scsiLevel,
126 'serial_number': disk.serialNumber,
127 'vStorageSupport': disk.vStorageSupport,
128 'protocol_endpoint': disk.protocolEndpoint,
129 'perenniallyReserved': disk.perenniallyReserved,
130 'block_size': None,
131 'block': None,
132 'device_path': '',
133 'ssd': False,
134 'local_disk': False,
135 'scsi_disk_type': None,
136 }
137 if hasattr(disk, 'capacity'):
138 temp_disk_info['block_size'] = disk.capacity.blockSize
139 temp_disk_info['block'] = disk.capacity.block
140 if hasattr(disk, 'devicePath'):
141 temp_disk_info['device_path'] = disk.devicePath
142 if hasattr(disk, 'ssd'):
143 temp_disk_info['ssd'] = disk.ssd
144 if hasattr(disk, 'localDisk'):
145 temp_disk_info['local_disk'] = disk.localDisk
146 if hasattr(disk, 'scsiDiskType'):
147 temp_disk_info['scsi_disk_type'] = disk.scsiDiskType
148
149 disk_info.append(temp_disk_info)
150 results['hosts_scsidisk_info'][host.name] = disk_info
151 self.module.exit_json(**results)
152
153
154 def main():
155 argument_spec = base_argument_spec()
156 argument_spec.update(
157 esxi_hostname=dict(type='str', required=False),
158 cluster_name=dict(type='str', required=False),
159 )
160
161 module = AnsibleModule(
162 argument_spec=argument_spec,
163 supports_check_mode=True,
164 required_one_of=[
165 ['cluster_name', 'esxi_hostname'],
166 ]
167 )
168
169 host_scsidisk_manager = VmwareHostDiskManager(module)
170 host_scsidisk_manager.gather_disk_info()
171
172
173 if __name__ == '__main__':
174 main()