| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2019, Ansible Project |
| 5 |
# Copyright: (c) 2019, VMware, Inc. All Rights Reserved. |
| 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_guest_controller |
| 16 |
short_description: Manage disk or USB controllers related to virtual machine in given vCenter infrastructure |
| 17 |
description: |
| 18 |
- This module can be used to add, remove disk controllers or USB controllers belonging to given virtual machine. |
| 19 |
author: |
| 20 |
- Diane Wang (@Tomorrow9) <[email protected]> |
| 21 |
options: |
| 22 |
name: |
| 23 |
description: |
| 24 |
- Name of the virtual machine. |
| 25 |
- This is a required parameter, if parameter O(uuid) or O(moid) is not supplied. |
| 26 |
type: str |
| 27 |
uuid: |
| 28 |
description: |
| 29 |
- UUID of the instance to gather facts if known, this is VMware's unique identifier. |
| 30 |
- This is a required parameter, if parameter O(name) or O(moid) is not supplied. |
| 31 |
type: str |
| 32 |
moid: |
| 33 |
description: |
| 34 |
- Managed Object ID of the instance to manage if known, this is a unique identifier only within a single vCenter instance. |
| 35 |
- This is required if O(name) or O(uuid) is not supplied. |
| 36 |
type: str |
| 37 |
folder: |
| 38 |
description: |
| 39 |
- Destination folder, absolute or relative path to find an existing guest. |
| 40 |
- This is a required parameter, only if multiple VMs are found with same name. |
| 41 |
- The folder should include the datacenter. ESX's datacenter is ha-datacenter |
| 42 |
- 'Examples:' |
| 43 |
- ' folder: /ha-datacenter/vm' |
| 44 |
- ' folder: ha-datacenter/vm' |
| 45 |
- ' folder: /datacenter1/vm' |
| 46 |
- ' folder: datacenter1/vm' |
| 47 |
- ' folder: /datacenter1/vm/folder1' |
| 48 |
- ' folder: datacenter1/vm/folder1' |
| 49 |
- ' folder: /folder1/datacenter1/vm' |
| 50 |
- ' folder: folder1/datacenter1/vm' |
| 51 |
- ' folder: /folder1/datacenter1/vm/folder2' |
| 52 |
type: str |
| 53 |
datacenter: |
| 54 |
description: |
| 55 |
- The datacenter name to which virtual machine belongs to. |
| 56 |
default: ha-datacenter |
| 57 |
type: str |
| 58 |
use_instance_uuid: |
| 59 |
description: |
| 60 |
- Whether to use the VMware instance UUID rather than the BIOS UUID. |
| 61 |
default: false |
| 62 |
type: bool |
| 63 |
controllers: |
| 64 |
description: |
| 65 |
- A list of disk or USB controllers to add or remove. |
| 66 |
- Total 4 disk controllers with the same type are allowed per VM. |
| 67 |
- Total 2 USB controllers are allowed per VM, 1 USB 2.0 and 1 USB 3.0 or 3.1. |
| 68 |
- For specific guest OS, supported controller types please refer to VMware Compatibility Guide. |
| 69 |
suboptions: |
| 70 |
controller_number: |
| 71 |
description: |
| 72 |
- Disk controller bus number. When O(controllers[].state=absent), this parameter is required. |
| 73 |
- When O(controllers[].type=usb2) or O(controllers[].type=usb3), this parameter is not required. |
| 74 |
type: int |
| 75 |
choices: |
| 76 |
- 0 |
| 77 |
- 1 |
| 78 |
- 2 |
| 79 |
- 3 |
| 80 |
type: |
| 81 |
description: |
| 82 |
- Type of disk or USB controller. |
| 83 |
- From vSphere 6.5 and virtual machine with hardware version 13, V(nvme) controller starts to be supported. |
| 84 |
required: true |
| 85 |
type: str |
| 86 |
choices: |
| 87 |
- buslogic |
| 88 |
- lsilogic |
| 89 |
- lsilogicsas |
| 90 |
- paravirtual |
| 91 |
- sata |
| 92 |
- nvme |
| 93 |
- usb2 |
| 94 |
- usb3 |
| 95 |
state: |
| 96 |
description: |
| 97 |
- Add new controller or remove specified existing controller. |
| 98 |
- If set to V(absent), the specified controller will be removed from virtual machine when there is no disk or device attaching to it. |
| 99 |
- If specified controller is removed or not exist, no action will be taken only warning message. |
| 100 |
- If set to V(present), new controller with specified type will be added. |
| 101 |
- If the number of controller with specified controller type reaches it's maximum, no action will be taken only warning message. |
| 102 |
required: true |
| 103 |
type: str |
| 104 |
choices: |
| 105 |
- present |
| 106 |
- absent |
| 107 |
bus_sharing: |
| 108 |
description: |
| 109 |
- Bus sharing type for SCSI controller. |
| 110 |
required: false |
| 111 |
type: str |
| 112 |
choices: ['noSharing', 'physicalSharing', 'virtualSharing' ] |
| 113 |
default: 'noSharing' |
| 114 |
type: list |
| 115 |
elements: dict |
| 116 |
gather_disk_controller_facts: |
| 117 |
description: |
| 118 |
- Whether to collect existing disk and USB controllers facts only. |
| 119 |
- When this parameter is set to V(true), O(controllers) parameter will be ignored. |
| 120 |
type: bool |
| 121 |
default: false |
| 122 |
sleep_time: |
| 123 |
description: |
| 124 |
- 'The sleep time in seconds after VM reconfigure task completes, used when not get the updated VM controller |
| 125 |
facts after VM reconfiguration.' |
| 126 |
- This parameter is not required. Maximum value is 600. |
| 127 |
default: 10 |
| 128 |
type: int |
| 129 |
extends_documentation_fragment: |
| 130 |
- vmware.vmware.base_options |
| 131 |
|
| 132 |
''' |
| 133 |
|
| 134 |
EXAMPLES = r''' |
| 135 |
- name: Add disk and USB 3.0 controllers for virtual machine located by name |
| 136 |
community.vmware.vmware_guest_controller: |
| 137 |
hostname: "{{ vcenter_hostname }}" |
| 138 |
username: "{{ vcenter_username }}" |
| 139 |
password: "{{ vcenter_password }}" |
| 140 |
datacenter: "{{ datacenter_name }}" |
| 141 |
name: test_VM |
| 142 |
controllers: |
| 143 |
- state: present |
| 144 |
type: sata |
| 145 |
- state: present |
| 146 |
type: nvme |
| 147 |
- state: present |
| 148 |
type: usb3 |
| 149 |
delegate_to: localhost |
| 150 |
register: disk_controller_facts |
| 151 |
|
| 152 |
- name: Remove disk controllers and USB 2.0 from virtual machine located by moid |
| 153 |
community.vmware.vmware_guest_controller: |
| 154 |
hostname: "{{ vcenter_hostname }}" |
| 155 |
username: "{{ vcenter_username }}" |
| 156 |
password: "{{ vcenter_password }}" |
| 157 |
datacenter: "{{ datacenter_name }}" |
| 158 |
moid: vm-33 |
| 159 |
controllers: |
| 160 |
- state: absent |
| 161 |
controller_number: 1 |
| 162 |
type: sata |
| 163 |
- state: absent |
| 164 |
controller_number: 0 |
| 165 |
type: nvme |
| 166 |
- state: absent |
| 167 |
type: usb2 |
| 168 |
delegate_to: localhost |
| 169 |
register: disk_controller_facts |
| 170 |
''' |
| 171 |
|
| 172 |
RETURN = r''' |
| 173 |
disk_controller_status: |
| 174 |
description: metadata about the virtual machine's existing disk controllers or after adding or removing operation |
| 175 |
returned: always |
| 176 |
type: dict |
| 177 |
sample: { |
| 178 |
"nvme": { |
| 179 |
"0": { |
| 180 |
"controller_busnumber": 0, |
| 181 |
"controller_controllerkey": 100, |
| 182 |
"controller_devicekey": 31000, |
| 183 |
"controller_disks_devicekey": [], |
| 184 |
"controller_label": "NVME controller 0", |
| 185 |
"controller_summary": "NVME controller 0", |
| 186 |
"controller_unitnumber": 30 |
| 187 |
} |
| 188 |
}, |
| 189 |
"sata": { |
| 190 |
"0": { |
| 191 |
"controller_busnumber": 0, |
| 192 |
"controller_controllerkey": 100, |
| 193 |
"controller_devicekey": 15000, |
| 194 |
"controller_disks_devicekey": [ |
| 195 |
16000, |
| 196 |
16001 |
| 197 |
], |
| 198 |
"controller_label": "SATA controller 0", |
| 199 |
"controller_summary": "AHCI", |
| 200 |
"controller_unitnumber": 24 |
| 201 |
} |
| 202 |
}, |
| 203 |
"scsi": { |
| 204 |
"0": { |
| 205 |
"controller_busnumber": 0, |
| 206 |
"controller_controllerkey": 100, |
| 207 |
"controller_devicekey": 1000, |
| 208 |
"controller_disks_devicekey": [ |
| 209 |
2000 |
| 210 |
], |
| 211 |
"controller_label": "SCSI controller 0", |
| 212 |
"controller_summary": "LSI Logic SAS", |
| 213 |
"controller_unitnumber": 3, |
| 214 |
"controller_bus_sharing": 'noSharing' |
| 215 |
}, |
| 216 |
"1": { |
| 217 |
"controller_busnumber": 1, |
| 218 |
"controller_controllerkey": 100, |
| 219 |
"controller_devicekey": 1001, |
| 220 |
"controller_disks_devicekey": [], |
| 221 |
"controller_label": "SCSI controller 1", |
| 222 |
"controller_summary": "VMware paravirtual SCSI", |
| 223 |
"controller_unitnumber": 4, |
| 224 |
"controller_bus_sharing": 'physicalSharing' |
| 225 |
} |
| 226 |
}, |
| 227 |
"usb2": { |
| 228 |
"0": { |
| 229 |
"controller_busnumber": 0, |
| 230 |
"controller_controllerkey": 100, |
| 231 |
"controller_devicekey": 7000, |
| 232 |
"controller_disks_devicekey": [], |
| 233 |
"controller_label": "USB Controller", |
| 234 |
"controller_summary": "Auto connect Disabled", |
| 235 |
"controller_unitnumber": 22 |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
''' |
| 240 |
|
| 241 |
try: |
| 242 |
from pyVmomi import vim |
| 243 |
except ImportError: |
| 244 |
pass |
| 245 |
|
| 246 |
import time |
| 247 |
from ansible.module_utils.basic import AnsibleModule |
| 248 |
from ansible.module_utils.common.text.converters import to_native |
| 249 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi, wait_for_task, TaskError |
| 250 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 251 |
from ansible_collections.community.vmware.plugins.module_utils.vm_device_helper import PyVmomiDeviceHelper |
| 252 |
|
| 253 |
|
| 254 |
class PyVmomiHelper(PyVmomi): |
| 255 |
def __init__(self, module): |
| 256 |
super(PyVmomiHelper, self).__init__(module) |
| 257 |
self.device_helper = PyVmomiDeviceHelper(self.module) |
| 258 |
self.sleep_time = 10 |
| 259 |
self.controller_types = self.device_helper.scsi_device_type.copy() |
| 260 |
self.controller_types.update(self.device_helper.usb_device_type) |
| 261 |
self.controller_types.update({'sata': self.device_helper.sata_device_type, 'nvme': self.device_helper.nvme_device_type}) |
| 262 |
self.config_spec = vim.vm.ConfigSpec() |
| 263 |
self.config_spec.deviceChange = [] |
| 264 |
self.change_detected = False |
| 265 |
self.disk_ctl_bus_num_list = dict(sata=list(range(0, 4)), |
| 266 |
nvme=list(range(0, 4)), |
| 267 |
scsi=list(range(0, 4))) |
| 268 |
|
| 269 |
def get_unused_ctl_bus_number(self): |
| 270 |
""" |
| 271 |
Get gid of occupied bus numbers of each type of disk controller, update the available bus number list |
| 272 |
""" |
| 273 |
for device in self.current_vm_obj.config.hardware.device: |
| 274 |
if isinstance(device, self.device_helper.sata_device_type): |
| 275 |
if len(self.disk_ctl_bus_num_list['sata']) != 0: |
| 276 |
self.disk_ctl_bus_num_list['sata'].remove(device.busNumber) |
| 277 |
if isinstance(device, self.device_helper.nvme_device_type): |
| 278 |
if len(self.disk_ctl_bus_num_list['nvme']) != 0: |
| 279 |
self.disk_ctl_bus_num_list['nvme'].remove(device.busNumber) |
| 280 |
if isinstance(device, tuple(self.device_helper.scsi_device_type.values())): |
| 281 |
if len(self.disk_ctl_bus_num_list['scsi']) != 0: |
| 282 |
self.disk_ctl_bus_num_list['scsi'].remove(device.busNumber) |
| 283 |
|
| 284 |
def check_ctl_disk_exist(self, ctl_type=None, bus_number=None): |
| 285 |
""" |
| 286 |
Check if controller of specified type exists and if there is disk attaching to it |
| 287 |
Return: Specified controller device, True or False of attaching disks |
| 288 |
""" |
| 289 |
ctl_specified = None |
| 290 |
disks_attached_exist = False |
| 291 |
if ctl_type is None: |
| 292 |
return ctl_specified, disks_attached_exist |
| 293 |
|
| 294 |
for device in self.current_vm_obj.config.hardware.device: |
| 295 |
if isinstance(device, self.controller_types.get(ctl_type)): |
| 296 |
if bus_number is not None and device.busNumber != bus_number: |
| 297 |
continue |
| 298 |
ctl_specified = device |
| 299 |
if len(device.device) != 0: |
| 300 |
disks_attached_exist = True |
| 301 |
break |
| 302 |
|
| 303 |
return ctl_specified, disks_attached_exist |
| 304 |
|
| 305 |
def create_controller(self, ctl_type, bus_sharing, bus_number=0): |
| 306 |
""" |
| 307 |
Create new disk or USB controller with specified type |
| 308 |
Args: |
| 309 |
ctl_type: controller type |
| 310 |
bus_number: disk controller bus number |
| 311 |
bus_sharing: noSharing, virtualSharing, physicalSharing |
| 312 |
|
| 313 |
Return: Virtual device spec for virtual controller |
| 314 |
""" |
| 315 |
if ctl_type == 'sata' or ctl_type == 'nvme' or ctl_type in self.device_helper.scsi_device_type.keys(): |
| 316 |
disk_ctl = self.device_helper.create_disk_controller(ctl_type, bus_number, bus_sharing) |
| 317 |
elif ctl_type in self.device_helper.usb_device_type.keys(): |
| 318 |
disk_ctl = vim.vm.device.VirtualDeviceSpec() |
| 319 |
disk_ctl.operation = vim.vm.device.VirtualDeviceSpec.Operation.add |
| 320 |
disk_ctl.device = self.device_helper.usb_device_type.get(ctl_type)() |
| 321 |
|
| 322 |
if ctl_type == 'usb2': |
| 323 |
disk_ctl.device.key = 7000 |
| 324 |
elif ctl_type == 'usb3': |
| 325 |
disk_ctl.device.key = 14000 |
| 326 |
|
| 327 |
disk_ctl.device.deviceInfo = vim.Description() |
| 328 |
disk_ctl.device.busNumber = bus_number |
| 329 |
|
| 330 |
return disk_ctl |
| 331 |
|
| 332 |
def gather_disk_controller_facts(self): |
| 333 |
""" |
| 334 |
Gather existing controller facts |
| 335 |
|
| 336 |
Return: A dictionary of each type controller facts |
| 337 |
""" |
| 338 |
disk_ctl_facts = dict( |
| 339 |
scsi=dict(), |
| 340 |
sata=dict(), |
| 341 |
nvme=dict(), |
| 342 |
usb2=dict(), |
| 343 |
usb3=dict() |
| 344 |
) |
| 345 |
for device in self.current_vm_obj.config.hardware.device: |
| 346 |
ctl_facts_dict = dict() |
| 347 |
if isinstance(device, tuple(self.controller_types.values())): |
| 348 |
ctl_facts_dict[str(device.busNumber)] = dict( |
| 349 |
controller_summary=device.deviceInfo.summary, |
| 350 |
controller_label=device.deviceInfo.label, |
| 351 |
controller_busnumber=device.busNumber, |
| 352 |
controller_controllerkey=device.controllerKey, |
| 353 |
controller_devicekey=device.key, |
| 354 |
controller_unitnumber=device.unitNumber, |
| 355 |
controller_disks_devicekey=device.device, |
| 356 |
) |
| 357 |
if hasattr(device, 'sharedBus'): |
| 358 |
ctl_facts_dict[str(device.busNumber)]['controller_bus_sharing'] = device.sharedBus |
| 359 |
if isinstance(device, tuple(self.device_helper.scsi_device_type.values())): |
| 360 |
disk_ctl_facts['scsi'].update(ctl_facts_dict) |
| 361 |
if isinstance(device, self.device_helper.nvme_device_type): |
| 362 |
disk_ctl_facts['nvme'].update(ctl_facts_dict) |
| 363 |
if isinstance(device, self.device_helper.sata_device_type): |
| 364 |
disk_ctl_facts['sata'].update(ctl_facts_dict) |
| 365 |
if isinstance(device, self.device_helper.usb_device_type.get('usb2')): |
| 366 |
disk_ctl_facts['usb2'].update(ctl_facts_dict) |
| 367 |
if isinstance(device, self.device_helper.usb_device_type.get('usb3')): |
| 368 |
disk_ctl_facts['usb3'].update(ctl_facts_dict) |
| 369 |
|
| 370 |
return disk_ctl_facts |
| 371 |
|
| 372 |
def sanitize_disk_controller_config(self): |
| 373 |
""" |
| 374 |
Check correctness of controller configuration provided by user |
| 375 |
|
| 376 |
Return: A list of dictionary with checked controller configured |
| 377 |
""" |
| 378 |
if not self.params.get('controllers'): |
| 379 |
self.module.exit_json(changed=False, msg="No controller provided for virtual" |
| 380 |
" machine '%s' for management." % self.current_vm_obj.name) |
| 381 |
if 10 != self.params.get('sleep_time') <= 300: |
| 382 |
self.sleep_time = self.params.get('sleep_time') |
| 383 |
exec_get_unused_ctl_bus_number = False |
| 384 |
controller_config = self.params.get('controllers') |
| 385 |
for ctl_config in controller_config: |
| 386 |
if ctl_config: |
| 387 |
if ctl_config['type'] not in self.device_helper.usb_device_type.keys(): |
| 388 |
if ctl_config['state'] == 'absent' and ctl_config.get('controller_number') is None: |
| 389 |
self.module.fail_json(msg="Disk controller number is required when removing it.") |
| 390 |
if ctl_config['state'] == 'present' and not exec_get_unused_ctl_bus_number: |
| 391 |
self.get_unused_ctl_bus_number() |
| 392 |
exec_get_unused_ctl_bus_number = True |
| 393 |
# starts from hardware version 13 nvme controller supported |
| 394 |
if ctl_config['state'] == 'present' and ctl_config['type'] == 'nvme': |
| 395 |
vm_hwv = int(self.current_vm_obj.config.version.split('-')[1]) |
| 396 |
if vm_hwv < 13: |
| 397 |
self.module.fail_json(msg="Can not create new NVMe disk controller due to VM hardware version" |
| 398 |
" is '%s', not >= 13." % vm_hwv) |
| 399 |
if exec_get_unused_ctl_bus_number: |
| 400 |
for ctl_config in controller_config: |
| 401 |
if ctl_config and ctl_config['state'] == 'present' and ctl_config['type'] not in self.device_helper.usb_device_type.keys(): |
| 402 |
if ctl_config['type'] in self.device_helper.scsi_device_type.keys(): |
| 403 |
if len(self.disk_ctl_bus_num_list['scsi']) != 0: |
| 404 |
ctl_config['controller_number'] = self.disk_ctl_bus_num_list['scsi'].pop(0) |
| 405 |
else: |
| 406 |
ctl_config['controller_number'] = None |
| 407 |
|
| 408 |
elif ctl_config['type'] == 'sata' or ctl_config['type'] == 'nvme': |
| 409 |
if len(self.disk_ctl_bus_num_list.get(ctl_config['type'])) != 0: |
| 410 |
ctl_config['controller_number'] = self.disk_ctl_bus_num_list.get(ctl_config['type']).pop(0) |
| 411 |
else: |
| 412 |
ctl_config['controller_number'] = None |
| 413 |
|
| 414 |
return controller_config |
| 415 |
|
| 416 |
def configure_disk_controllers(self): |
| 417 |
""" |
| 418 |
Do disk controller management, add or remove |
| 419 |
|
| 420 |
Return: Operation result |
| 421 |
""" |
| 422 |
if self.params['gather_disk_controller_facts']: |
| 423 |
results = {'changed': False, 'failed': False, 'disk_controller_data': self.gather_disk_controller_facts()} |
| 424 |
return results |
| 425 |
|
| 426 |
controller_config = self.sanitize_disk_controller_config() |
| 427 |
for disk_ctl_config in controller_config: |
| 428 |
if disk_ctl_config and disk_ctl_config['state'] == 'present': |
| 429 |
# create new USB controller, bus number is 0 |
| 430 |
if disk_ctl_config['type'] in self.device_helper.usb_device_type.keys(): |
| 431 |
usb_exists, has_disks_attached = self.check_ctl_disk_exist(disk_ctl_config['type']) |
| 432 |
if usb_exists: |
| 433 |
self.module.warn("'%s' USB controller already exists, can not add more." % disk_ctl_config['type']) |
| 434 |
else: |
| 435 |
disk_controller_new = self.create_controller(disk_ctl_config['type'], disk_ctl_config.get('bus_sharing')) |
| 436 |
self.config_spec.deviceChange.append(disk_controller_new) |
| 437 |
self.change_detected = True |
| 438 |
# create other disk controller |
| 439 |
else: |
| 440 |
if disk_ctl_config.get('controller_number') is not None: |
| 441 |
disk_controller_new = self.create_controller( |
| 442 |
disk_ctl_config['type'], |
| 443 |
disk_ctl_config.get('bus_sharing'), |
| 444 |
disk_ctl_config.get('controller_number') |
| 445 |
) |
| 446 |
self.config_spec.deviceChange.append(disk_controller_new) |
| 447 |
self.change_detected = True |
| 448 |
else: |
| 449 |
if disk_ctl_config['type'] in self.device_helper.scsi_device_type.keys(): |
| 450 |
self.module.warn("Already 4 SCSI controllers, can not add new '%s' controller." % disk_ctl_config['type']) |
| 451 |
else: |
| 452 |
self.module.warn("Already 4 '%s' controllers, can not add new one." % disk_ctl_config['type']) |
| 453 |
elif disk_ctl_config and disk_ctl_config['state'] == 'absent': |
| 454 |
existing_ctl, has_disks_attached = self.check_ctl_disk_exist(disk_ctl_config['type'], disk_ctl_config.get('controller_number')) |
| 455 |
if existing_ctl is not None: |
| 456 |
if not has_disks_attached: |
| 457 |
ctl_spec = vim.vm.device.VirtualDeviceSpec() |
| 458 |
ctl_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.remove |
| 459 |
ctl_spec.device = existing_ctl |
| 460 |
self.config_spec.deviceChange.append(ctl_spec) |
| 461 |
self.change_detected = True |
| 462 |
else: |
| 463 |
self.module.warn("Can not remove specified controller, type '%s', bus number '%s'," |
| 464 |
" there are disks attaching to it." % (disk_ctl_config['type'], disk_ctl_config.get('controller_number'))) |
| 465 |
else: |
| 466 |
self.module.warn("Can not find specified controller to remove, type '%s', bus number '%s'." |
| 467 |
% (disk_ctl_config['type'], disk_ctl_config.get('controller_number'))) |
| 468 |
|
| 469 |
try: |
| 470 |
task = self.current_vm_obj.ReconfigVM_Task(spec=self.config_spec) |
| 471 |
wait_for_task(task) |
| 472 |
except vim.fault.InvalidDeviceSpec as e: |
| 473 |
self.module.fail_json(msg="Failed to configure controller on given virtual machine due to invalid" |
| 474 |
" device spec : %s" % to_native(e.msg), |
| 475 |
details="Please check ESXi server logs for more details.") |
| 476 |
except vim.fault.RestrictedVersion as e: |
| 477 |
self.module.fail_json(msg="Failed to reconfigure virtual machine due to" |
| 478 |
" product versioning restrictions: %s" % to_native(e.msg)) |
| 479 |
except TaskError as task_e: |
| 480 |
self.module.fail_json(msg=to_native(task_e)) |
| 481 |
|
| 482 |
if task.info.state == 'error': |
| 483 |
results = {'changed': self.change_detected, 'failed': True, 'msg': task.info.error.msg} |
| 484 |
else: |
| 485 |
if self.change_detected: |
| 486 |
time.sleep(self.sleep_time) |
| 487 |
results = {'changed': self.change_detected, 'failed': False, 'disk_controller_data': self.gather_disk_controller_facts()} |
| 488 |
|
| 489 |
return results |
| 490 |
|
| 491 |
|
| 492 |
def main(): |
| 493 |
argument_spec = base_argument_spec() |
| 494 |
argument_spec.update( |
| 495 |
name=dict(type='str'), |
| 496 |
uuid=dict(type='str'), |
| 497 |
moid=dict(type='str'), |
| 498 |
folder=dict(type='str'), |
| 499 |
datacenter=dict(type='str', default='ha-datacenter'), |
| 500 |
controllers=dict( |
| 501 |
type='list', |
| 502 |
elements='dict', |
| 503 |
required=False, |
| 504 |
options=dict( |
| 505 |
state=dict(type='str', choices=['present', 'absent'], required=True), |
| 506 |
controller_number=dict(type='int', choices=[0, 1, 2, 3], required=False), |
| 507 |
type=dict( |
| 508 |
type='str', |
| 509 |
choices=['sata', 'nvme', 'lsilogic', 'buslogic', 'lsilogicsas', 'paravirtual', 'usb2', 'usb3'], |
| 510 |
required=True, |
| 511 |
), |
| 512 |
bus_sharing=dict( |
| 513 |
type='str', |
| 514 |
choices=['noSharing', 'physicalSharing', 'virtualSharing'], |
| 515 |
required=False, |
| 516 |
default='noSharing', |
| 517 |
), |
| 518 |
), |
| 519 |
), |
| 520 |
use_instance_uuid=dict(type='bool', default=False), |
| 521 |
gather_disk_controller_facts=dict(type='bool', default=False), |
| 522 |
sleep_time=dict(type='int', default=10), |
| 523 |
) |
| 524 |
module = AnsibleModule( |
| 525 |
argument_spec=argument_spec, |
| 526 |
required_one_of=[ |
| 527 |
['name', 'uuid', 'moid'] |
| 528 |
] |
| 529 |
) |
| 530 |
|
| 531 |
if module.params['folder']: |
| 532 |
# FindByInventoryPath() does not require an absolute path |
| 533 |
# so we should leave the input folder path unmodified |
| 534 |
module.params['folder'] = module.params['folder'].rstrip('/') |
| 535 |
|
| 536 |
pyv = PyVmomiHelper(module) |
| 537 |
# Check if the VM exists before continuing |
| 538 |
vm = pyv.get_vm() |
| 539 |
|
| 540 |
if not vm: |
| 541 |
# We unable to find the virtual machine user specified |
| 542 |
# Bail out |
| 543 |
vm_id = (module.params.get('name') or module.params.get('uuid') or module.params.get('moid')) |
| 544 |
module.fail_json(msg="Unable to manage disk or USB controllers for non-existing virtual machine '%s'." % vm_id) |
| 545 |
|
| 546 |
# VM exists |
| 547 |
result = pyv.configure_disk_controllers() |
| 548 |
if result['failed']: |
| 549 |
module.fail_json(**result) |
| 550 |
else: |
| 551 |
module.exit_json(**result) |
| 552 |
|
| 553 |
|
| 554 |
if __name__ == '__main__': |
| 555 |
main() |