| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2019, NAER William Leemans (@bushvin) <[email protected]> |
| 5 |
# Copyright: (c) 2018, Ansible Project |
| 6 |
# Copyright: (c) 2018, Abhijeet Kasurde <[email protected]> |
| 7 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 8 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 9 |
|
| 10 |
from __future__ import absolute_import, division, print_function |
| 11 |
__metaclass__ = type |
| 12 |
|
| 13 |
|
| 14 |
DOCUMENTATION = r''' |
| 15 |
--- |
| 16 |
module: vmware_guest_disk_info |
| 17 |
short_description: Gather info about disks of given virtual machine |
| 18 |
description: |
| 19 |
- This module can be used to gather information about disks belonging to given virtual machine. |
| 20 |
author: |
| 21 |
- Abhijeet Kasurde (@Akasurde) <[email protected]> |
| 22 |
options: |
| 23 |
name: |
| 24 |
description: |
| 25 |
- Name of the virtual machine. |
| 26 |
- This is required parameter, if parameter O(uuid) or O(moid) is not supplied. |
| 27 |
type: str |
| 28 |
uuid: |
| 29 |
description: |
| 30 |
- UUID of the instance to gather information if known, this is VMware's unique identifier. |
| 31 |
- This is required parameter, if parameter O(name) or O(moid) is not supplied. |
| 32 |
type: str |
| 33 |
moid: |
| 34 |
description: |
| 35 |
- Managed Object ID of the instance to manage if known, this is a unique identifier only within a single vCenter instance. |
| 36 |
- This is required if O(name) or O(uuid) is not supplied. |
| 37 |
type: str |
| 38 |
use_instance_uuid: |
| 39 |
description: |
| 40 |
- Whether to use the VMware instance UUID rather than the BIOS UUID. |
| 41 |
default: false |
| 42 |
type: bool |
| 43 |
folder: |
| 44 |
description: |
| 45 |
- Destination folder, absolute or relative path to find an existing guest. |
| 46 |
- This is required parameter, only if multiple VMs are found with same name. |
| 47 |
- The folder should include the datacenter. ESX's datacenter is ha-datacenter |
| 48 |
- 'Examples:' |
| 49 |
- ' folder: /ha-datacenter/vm' |
| 50 |
- ' folder: ha-datacenter/vm' |
| 51 |
- ' folder: /datacenter1/vm' |
| 52 |
- ' folder: datacenter1/vm' |
| 53 |
- ' folder: /datacenter1/vm/folder1' |
| 54 |
- ' folder: datacenter1/vm/folder1' |
| 55 |
- ' folder: /folder1/datacenter1/vm' |
| 56 |
- ' folder: folder1/datacenter1/vm' |
| 57 |
- ' folder: /folder1/datacenter1/vm/folder2' |
| 58 |
type: str |
| 59 |
datacenter: |
| 60 |
description: |
| 61 |
- The datacenter name to which virtual machine belongs to. |
| 62 |
required: true |
| 63 |
type: str |
| 64 |
extends_documentation_fragment: |
| 65 |
- vmware.vmware.base_options |
| 66 |
|
| 67 |
''' |
| 68 |
|
| 69 |
EXAMPLES = r''' |
| 70 |
- name: Gather disk info from virtual machine using UUID |
| 71 |
community.vmware.vmware_guest_disk_info: |
| 72 |
hostname: "{{ vcenter_hostname }}" |
| 73 |
username: "{{ vcenter_username }}" |
| 74 |
password: "{{ vcenter_password }}" |
| 75 |
datacenter: ha-datacenter |
| 76 |
uuid: 421e4592-c069-924d-ce20-7e7533fab926 |
| 77 |
delegate_to: localhost |
| 78 |
register: disk_info |
| 79 |
|
| 80 |
- name: Gather disk info from virtual machine using name |
| 81 |
community.vmware.vmware_guest_disk_info: |
| 82 |
hostname: "{{ vcenter_hostname }}" |
| 83 |
username: "{{ vcenter_username }}" |
| 84 |
password: "{{ vcenter_password }}" |
| 85 |
datacenter: ha-datacenter |
| 86 |
name: VM_225 |
| 87 |
delegate_to: localhost |
| 88 |
register: disk_info |
| 89 |
|
| 90 |
- name: Gather disk info from virtual machine using moid |
| 91 |
community.vmware.vmware_guest_disk_info: |
| 92 |
hostname: "{{ vcenter_hostname }}" |
| 93 |
username: "{{ vcenter_username }}" |
| 94 |
password: "{{ vcenter_password }}" |
| 95 |
datacenter: ha-datacenter |
| 96 |
moid: vm-42 |
| 97 |
delegate_to: localhost |
| 98 |
register: disk_info |
| 99 |
''' |
| 100 |
|
| 101 |
RETURN = r''' |
| 102 |
guest_disk_info: |
| 103 |
description: metadata about the virtual machine's disks |
| 104 |
returned: always |
| 105 |
type: dict |
| 106 |
sample: { |
| 107 |
"0": { |
| 108 |
"backing_datastore": "datastore2", |
| 109 |
"backing_disk_mode": "persistent", |
| 110 |
"backing_diskmode": "persistent", |
| 111 |
"backing_eagerlyscrub": false, |
| 112 |
"backing_filename": "[datastore2] VM_225/VM_225.vmdk", |
| 113 |
"backing_thinprovisioned": false, |
| 114 |
"backing_type": "FlatVer2", |
| 115 |
"backing_writethrough": false, |
| 116 |
"backing_uuid": "200C3A00-f82a-97af-02ff-62a595f0020a", |
| 117 |
"capacity_in_bytes": 10485760, |
| 118 |
"capacity_in_kb": 10240, |
| 119 |
"controller_bus_number": 0, |
| 120 |
"controller_key": 1000, |
| 121 |
"controller_type": "paravirtual", |
| 122 |
"key": 2000, |
| 123 |
"label": "Hard disk 1", |
| 124 |
"summary": "10,240 KB", |
| 125 |
"unit_number": 0 |
| 126 |
}, |
| 127 |
"1": { |
| 128 |
"backing_datastore": "datastore3", |
| 129 |
"backing_devicename": "vml.012345678901234567890123456789012345678901234567890123", |
| 130 |
"backing_disk_mode": "independent_persistent", |
| 131 |
"backing_diskmode": "independent_persistent", |
| 132 |
"backing_filename": "[datastore3] VM_226/VM_226.vmdk", |
| 133 |
"backing_lunuuid": "012345678901234567890123456789012345678901234567890123", |
| 134 |
"backing_type": "RawDiskMappingVer1", |
| 135 |
"backing_uuid": null, |
| 136 |
"capacity_in_bytes": 15728640, |
| 137 |
"capacity_in_kb": 15360, |
| 138 |
"controller_bus_number": 0, |
| 139 |
"controller_key": 1000, |
| 140 |
"controller_type": "paravirtual", |
| 141 |
"key": 2001, |
| 142 |
"label": "Hard disk 3", |
| 143 |
"summary": "15,360 KB", |
| 144 |
"unit_number": 1 |
| 145 |
}, |
| 146 |
} |
| 147 |
''' |
| 148 |
|
| 149 |
from ansible.module_utils.basic import AnsibleModule |
| 150 |
from ansible.module_utils.common.text.converters import to_text |
| 151 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi |
| 152 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 153 |
from ansible_collections.community.vmware.plugins.module_utils.vm_device_helper import PyVmomiDeviceHelper |
| 154 |
|
| 155 |
|
| 156 |
def main(): |
| 157 |
argument_spec = base_argument_spec() |
| 158 |
argument_spec.update( |
| 159 |
name=dict(type='str'), |
| 160 |
uuid=dict(type='str'), |
| 161 |
moid=dict(type='str'), |
| 162 |
use_instance_uuid=dict(type='bool', default=False), |
| 163 |
folder=dict(type='str'), |
| 164 |
datacenter=dict(type='str', required=True), |
| 165 |
) |
| 166 |
module = AnsibleModule( |
| 167 |
argument_spec=argument_spec, |
| 168 |
required_one_of=[ |
| 169 |
['name', 'uuid', 'moid'] |
| 170 |
], |
| 171 |
supports_check_mode=True, |
| 172 |
) |
| 173 |
|
| 174 |
if module.params['folder']: |
| 175 |
# FindByInventoryPath() does not require an absolute path |
| 176 |
# so we should leave the input folder path unmodified |
| 177 |
module.params['folder'] = module.params['folder'].rstrip('/') |
| 178 |
|
| 179 |
pyv = PyVmomi(module) |
| 180 |
device_helper = PyVmomiDeviceHelper(module) |
| 181 |
# Check if the VM exists before continuing |
| 182 |
vm = pyv.get_vm() |
| 183 |
|
| 184 |
if vm: |
| 185 |
# VM exists |
| 186 |
try: |
| 187 |
module.exit_json(guest_disk_info=device_helper.gather_disk_info(vm)) |
| 188 |
except Exception as exc: |
| 189 |
module.fail_json(msg="Failed to gather information with exception : %s" % to_text(exc)) |
| 190 |
else: |
| 191 |
# We unable to find the virtual machine user specified |
| 192 |
# Bail out |
| 193 |
vm_id = (module.params.get('uuid') or module.params.get('moid') or module.params.get('name')) |
| 194 |
module.fail_json(msg="Unable to gather disk information for non-existing VM %s" % vm_id) |
| 195 |
|
| 196 |
|
| 197 |
if __name__ == '__main__': |
| 198 |
main() |