| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2024, Nina Loser <[email protected]> |
| 5 |
# Copyright: (c) 2021, Ansible Project |
| 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 |
DOCUMENTATION = r''' |
| 13 |
--- |
| 14 |
module: vmware_first_class_disk_info |
| 15 |
short_description: Gather info about VMware vSphere First Class Disks |
| 16 |
description: |
| 17 |
- This module can be used to gather information about VMware vSphere First Class Disks. |
| 18 |
author: |
| 19 |
- Nina Loser (@nina2244) |
| 20 |
options: |
| 21 |
datacenter_name: |
| 22 |
description: The name of the datacenter. |
| 23 |
type: str |
| 24 |
datastore_name: |
| 25 |
description: Name of datastore or datastore cluster of the disk. |
| 26 |
required: true |
| 27 |
type: str |
| 28 |
disk_name: |
| 29 |
description: The name of the disk. If not set return all found. |
| 30 |
type: str |
| 31 |
extends_documentation_fragment: |
| 32 |
- vmware.vmware.base_options |
| 33 |
''' |
| 34 |
|
| 35 |
EXAMPLES = r''' |
| 36 |
- name: Gather info of 1GBDisk |
| 37 |
community.vmware.vmware_first_class_disk_info: |
| 38 |
hostname: '{{ vcenter_hostname }}' |
| 39 |
username: '{{ vcenter_username }}' |
| 40 |
password: '{{ vcenter_password }}' |
| 41 |
datastore_name: '{{ datastore_name }}' |
| 42 |
disk_name: '1GBDisk' |
| 43 |
register: disk_info |
| 44 |
delegate_to: localhost |
| 45 |
|
| 46 |
- debug: |
| 47 |
var: disk_info.first_class_disks |
| 48 |
|
| 49 |
- name: Gather info of all first class disks |
| 50 |
community.vmware.vmware_first_class_disk_info: |
| 51 |
hostname: '{{ vcenter_hostname }}' |
| 52 |
username: '{{ vcenter_username }}' |
| 53 |
password: '{{ vcenter_password }}' |
| 54 |
datastore_name: '{{ datastore_name }}' |
| 55 |
register: disk_info |
| 56 |
delegate_to: localhost |
| 57 |
|
| 58 |
- debug: |
| 59 |
var: disk_info.first_class_disks |
| 60 |
''' |
| 61 |
|
| 62 |
RETURN = r''' |
| 63 |
first_class_disks: |
| 64 |
description: list of dictionary of First-class disk and their information |
| 65 |
returned: success |
| 66 |
type: list |
| 67 |
sample: [ |
| 68 |
{ |
| 69 |
"name": "1GBDisk", |
| 70 |
"datastore_name": "DS0", |
| 71 |
"size_mb": "1024", |
| 72 |
"consumption_type": "disk", |
| 73 |
"descriptor_version": 0, |
| 74 |
"consumer_ids": [] |
| 75 |
} |
| 76 |
] |
| 77 |
''' |
| 78 |
|
| 79 |
from ansible.module_utils.basic import AnsibleModule |
| 80 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi |
| 81 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 82 |
|
| 83 |
|
| 84 |
class FirstClassDiskInfo(PyVmomi): |
| 85 |
def __init__(self, module): |
| 86 |
super(FirstClassDiskInfo, self).__init__(module) |
| 87 |
self.datacenter_name = self.params['datacenter_name'] |
| 88 |
self.datastore_name = self.params['datastore_name'] |
| 89 |
self.disk_name = self.params['disk_name'] |
| 90 |
|
| 91 |
def gather_first_class_disk_info(self): |
| 92 |
self.datastore_obj = self.find_datastore_by_name(datastore_name=self.datastore_name, datacenter_name=self.datacenter_name) |
| 93 |
if not self.datastore_obj: |
| 94 |
self.module.fail_json(msg='Failed to find datastore %s.' % self.datastore_name) |
| 95 |
|
| 96 |
if self.disk_name: |
| 97 |
self.disk_obj = self.find_first_class_disk_by_name(self.disk_name, self.datastore_obj) |
| 98 |
if not self.disk_obj: |
| 99 |
self.module.fail_json(msg='Failed to find disk %s.' % self.disk_name) |
| 100 |
|
| 101 |
self.disks = [self.disk_obj] |
| 102 |
|
| 103 |
else: |
| 104 |
self.disks = self.find_first_class_disks(self.datastore_obj) |
| 105 |
if not self.disks: |
| 106 |
return [] |
| 107 |
|
| 108 |
disk_infos = list() |
| 109 |
for disk in self.disks: |
| 110 |
disk_info = dict( |
| 111 |
name=disk.config.name, |
| 112 |
id=disk.config.id.id, |
| 113 |
datastore_name=disk.config.backing.datastore.name, |
| 114 |
size_mb=disk.config.capacityInMB, |
| 115 |
consumption_type=disk.config.consumptionType, |
| 116 |
descriptor_version=disk.config.descriptorVersion, |
| 117 |
consumer_ids=list(id.id for id in disk.config.consumerId) |
| 118 |
) |
| 119 |
disk_infos.append(disk_info) |
| 120 |
|
| 121 |
return disk_infos |
| 122 |
|
| 123 |
|
| 124 |
def main(): |
| 125 |
argument_spec = base_argument_spec() |
| 126 |
argument_spec.update( |
| 127 |
dict( |
| 128 |
datacenter_name=dict(type='str'), |
| 129 |
datastore_name=dict(required=True, type='str'), |
| 130 |
disk_name=dict(type='str') |
| 131 |
) |
| 132 |
) |
| 133 |
|
| 134 |
module = AnsibleModule(argument_spec=argument_spec, |
| 135 |
supports_check_mode=True) |
| 136 |
|
| 137 |
first_class_disk = FirstClassDiskInfo(module) |
| 138 |
|
| 139 |
_first_class_disks = first_class_disk.gather_first_class_disk_info() |
| 140 |
|
| 141 |
module.exit_json(changed=False, first_class_disks=_first_class_disks) |
| 142 |
|
| 143 |
|
| 144 |
if __name__ == '__main__': |
| 145 |
main() |