| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, Ansible Project |
| 5 |
# Copyright: (c) 2018, Diane Wang <[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 |
|
| 11 |
__metaclass__ = type |
| 12 |
|
| 13 |
DOCUMENTATION = r""" |
| 14 |
--- |
| 15 |
module: vmware_guest_vgpu |
| 16 |
short_description: Modify vGPU video card profile of the specified virtual machine in the given vCenter infrastructure |
| 17 |
description: |
| 18 |
- This module is used to reconfigure vGPU card profile of the given virtual machine. |
| 19 |
- VM must be power off M(community.vmware.vmware_guest_powerstate) module can perform that task. |
| 20 |
author: |
| 21 |
- Mohamed Alibi (@Medalibi) |
| 22 |
- Unknown (@matancarmeli7) |
| 23 |
options: |
| 24 |
name: |
| 25 |
description: |
| 26 |
- Name of the virtual machine. |
| 27 |
- This is a required parameter, if parameter O(uuid) or O(moid) is not supplied. |
| 28 |
type: str |
| 29 |
uuid: |
| 30 |
description: |
| 31 |
- UUID of the instance to gather facts if known, this is VMware's unique identifier. |
| 32 |
- This is a required parameter, if parameter O(name) or O(moid) is not supplied. |
| 33 |
type: str |
| 34 |
moid: |
| 35 |
description: |
| 36 |
- Managed Object ID of the instance to manage if known, this is a unique identifier only within a single vCenter instance. |
| 37 |
- This is required if O(name) or O(uuid) is not supplied. |
| 38 |
type: str |
| 39 |
folder: |
| 40 |
description: |
| 41 |
- Destination folder, absolute or relative path to find an existing guest. |
| 42 |
- This is a required parameter, only if multiple VMs are found with same name. |
| 43 |
- The folder should include the datacenter. ESXi server's datacenter is ha-datacenter. |
| 44 |
- 'Examples:' |
| 45 |
- ' folder: /ha-datacenter/vm' |
| 46 |
- ' folder: ha-datacenter/vm' |
| 47 |
- ' folder: /datacenter1/vm' |
| 48 |
- ' folder: datacenter1/vm' |
| 49 |
- ' folder: /datacenter1/vm/folder1' |
| 50 |
- ' folder: datacenter1/vm/folder1' |
| 51 |
- ' folder: /folder1/datacenter1/vm' |
| 52 |
- ' folder: folder1/datacenter1/vm' |
| 53 |
- ' folder: /folder1/datacenter1/vm/folder2' |
| 54 |
type: str |
| 55 |
datacenter: |
| 56 |
default: ha-datacenter |
| 57 |
description: |
| 58 |
- The datacenter name to which virtual machine belongs to. |
| 59 |
type: str |
| 60 |
state: |
| 61 |
default: present |
| 62 |
choices: [ 'present', 'absent' ] |
| 63 |
description: |
| 64 |
- vGPU profile state. |
| 65 |
- When V(present), the selected vGPU profile will be added if the VM hosted ESXi host NVIDIA GPU offer it. |
| 66 |
- When V(absent), the selected vGPU profile gets removed from the VM. |
| 67 |
type: str |
| 68 |
vgpu: |
| 69 |
description: |
| 70 |
- A supported vGPU profile depending on the GPU model. Required for any operation. |
| 71 |
type: str |
| 72 |
force: |
| 73 |
description: |
| 74 |
- Force operation. |
| 75 |
default: false |
| 76 |
type: bool |
| 77 |
use_instance_uuid: |
| 78 |
description: |
| 79 |
- Whether to use the VMware instance UUID rather than the BIOS UUID. |
| 80 |
default: false |
| 81 |
type: bool |
| 82 |
cluster: |
| 83 |
description: |
| 84 |
- The cluster name where the virtual machine is running. |
| 85 |
type: str |
| 86 |
esxi_hostname: |
| 87 |
description: |
| 88 |
- The ESXi hostname where the virtual machine is running. |
| 89 |
type: str |
| 90 |
extends_documentation_fragment: |
| 91 |
- vmware.vmware.base_options |
| 92 |
""" |
| 93 |
|
| 94 |
EXAMPLES = r""" |
| 95 |
- name: Add vGPU profile to VM |
| 96 |
community.vmware.vmware_guest_vgpu: |
| 97 |
hostname: "{{ vcenter_hostname }}" |
| 98 |
username: "{{ vcenter_username }}" |
| 99 |
password: "{{ vcenter_password }}" |
| 100 |
datacenter: "{{ datacenter_name }}" |
| 101 |
validate_certs: false |
| 102 |
name: UbuntuTest |
| 103 |
vgpu: 'grid_m10-8q' |
| 104 |
state: present |
| 105 |
delegate_to: localhost |
| 106 |
register: vgpu_info |
| 107 |
|
| 108 |
- name: Remove vGPU profile to VM |
| 109 |
community.vmware.vmware_guest_vgpu: |
| 110 |
hostname: "{{ vcenter_hostname }}" |
| 111 |
username: "{{ vcenter_username }}" |
| 112 |
password: "{{ vcenter_password }}" |
| 113 |
datacenter: "{{ datacenter_name }}" |
| 114 |
validate_certs: false |
| 115 |
name: UbuntuTest |
| 116 |
vgpu: 'grid_m10-8q' |
| 117 |
state: absent |
| 118 |
delegate_to: localhost |
| 119 |
register: vgpu_info |
| 120 |
""" |
| 121 |
|
| 122 |
RETURN = r""" |
| 123 |
vgpu_info: |
| 124 |
description: metadata about the virtual machine's vGPU profile |
| 125 |
returned: always |
| 126 |
type: dict |
| 127 |
sample: { |
| 128 |
"vgpu": { |
| 129 |
"Controller_Key": 100, |
| 130 |
"Key": 13000, |
| 131 |
"Label": "PCI device 0", |
| 132 |
"Summary": "NVIDIA GRID vGPU grid_m10-8q", |
| 133 |
"Unit_Number": 18, |
| 134 |
"Vgpu": "grid_m10-8q" |
| 135 |
} |
| 136 |
} |
| 137 |
""" |
| 138 |
|
| 139 |
try: |
| 140 |
from pyVmomi import vim |
| 141 |
except ImportError: |
| 142 |
pass |
| 143 |
|
| 144 |
from ansible.module_utils.basic import AnsibleModule |
| 145 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi, wait_for_task |
| 146 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 147 |
|
| 148 |
|
| 149 |
class PyVmomiHelper(PyVmomi): |
| 150 |
def __init__(self, module): |
| 151 |
super(PyVmomiHelper, self).__init__(module) |
| 152 |
|
| 153 |
def _gather_vgpu_profile_facts(self, vm_obj): |
| 154 |
""" |
| 155 |
Gather facts about VM's vGPU profile settings |
| 156 |
Args: |
| 157 |
vm_obj: Managed object of virtual machine |
| 158 |
Returns: vGPU profile and its facts |
| 159 |
""" |
| 160 |
vgpu_info = dict() |
| 161 |
for vgpu_VirtualDevice_obj in vm_obj.config.hardware.device: |
| 162 |
if hasattr(vgpu_VirtualDevice_obj.backing, "vgpu"): |
| 163 |
vgpu_info = dict( |
| 164 |
Vgpu=vgpu_VirtualDevice_obj.backing.vgpu, |
| 165 |
Key=vgpu_VirtualDevice_obj.key, |
| 166 |
Summary=vgpu_VirtualDevice_obj.deviceInfo.summary, |
| 167 |
Label=vgpu_VirtualDevice_obj.deviceInfo.label, |
| 168 |
Unit_Number=vgpu_VirtualDevice_obj.unitNumber, |
| 169 |
Controller_Key=vgpu_VirtualDevice_obj.controllerKey, |
| 170 |
) |
| 171 |
break |
| 172 |
return vgpu_info |
| 173 |
|
| 174 |
def _vgpu_absent(self, vm_obj): |
| 175 |
""" |
| 176 |
Remove vGPU profile of virtual machine. |
| 177 |
Args: |
| 178 |
vm_obj: Managed object of virtual machine |
| 179 |
Returns: Operation results and vGPU facts |
| 180 |
""" |
| 181 |
result = {} |
| 182 |
vgpu_prfl = self.params["vgpu"] |
| 183 |
vgpu_VirtualDevice_obj = self._get_vgpu_VirtualDevice_object(vm_obj, vgpu_prfl) |
| 184 |
if vgpu_VirtualDevice_obj is None: |
| 185 |
changed = False |
| 186 |
failed = False |
| 187 |
else: |
| 188 |
vgpu_fact = self._gather_vgpu_profile_facts(vm_obj) |
| 189 |
changed, failed = self._remove_vgpu_profile_from_vm( |
| 190 |
vm_obj, vgpu_VirtualDevice_obj, vgpu_prfl |
| 191 |
) |
| 192 |
result = {"changed": changed, "failed": failed, "vgpu": vgpu_fact} |
| 193 |
return result |
| 194 |
|
| 195 |
def _remove_vgpu_profile_from_vm(self, vm_obj, vgpu_VirtualDevice_obj, vgpu_prfl): |
| 196 |
""" |
| 197 |
Remove vGPU profile of virtual machine |
| 198 |
Args: |
| 199 |
vm_obj: Managed object of virtual machine |
| 200 |
vgpu_VirtualDevice_obj: vGPU profile object holding its facts |
| 201 |
vgpu_prfl: vGPU profile name |
| 202 |
Returns: Operation results |
| 203 |
""" |
| 204 |
changed = False |
| 205 |
failed = False |
| 206 |
vm_current_vgpu_profile = self._get_vgpu_profile_in_the_vm(vm_obj) |
| 207 |
if vgpu_prfl in vm_current_vgpu_profile: |
| 208 |
vdspec = vim.vm.device.VirtualDeviceSpec() |
| 209 |
vmConfigSpec = vim.vm.ConfigSpec() |
| 210 |
vdspec.operation = "remove" |
| 211 |
vdspec.device = vgpu_VirtualDevice_obj |
| 212 |
vmConfigSpec.deviceChange.append(vdspec) |
| 213 |
|
| 214 |
try: |
| 215 |
task = vm_obj.ReconfigVM_Task(spec=vmConfigSpec) |
| 216 |
wait_for_task(task) |
| 217 |
changed = True |
| 218 |
return changed, failed |
| 219 |
except Exception as exc: |
| 220 |
failed = True |
| 221 |
self.module.fail_json( |
| 222 |
msg="Failed to delete vGPU profile" |
| 223 |
" '%s' from vm %s." % (vgpu_prfl, vm_obj.name), |
| 224 |
detail=exc.msg, |
| 225 |
) |
| 226 |
return changed, failed |
| 227 |
|
| 228 |
def _vgpu_present(self, vm_obj): |
| 229 |
""" |
| 230 |
Add vGPU profile to virtual machine. |
| 231 |
Args: |
| 232 |
vm_obj: Managed object of virtual machine |
| 233 |
Returns: Operation results and vGPU facts |
| 234 |
""" |
| 235 |
result = {} |
| 236 |
vgpu_prfl = self.params["vgpu"] |
| 237 |
vgpu_profile_name = self._get_vgpu_profiles_name(vm_obj, vgpu_prfl) |
| 238 |
if vgpu_profile_name is None: |
| 239 |
self.module.fail_json( |
| 240 |
msg="vGPU Profile '%s'" " does not exist." % vgpu_prfl |
| 241 |
) |
| 242 |
|
| 243 |
changed, failed = self._add_vgpu_profile_to_vm( |
| 244 |
vm_obj, vgpu_profile_name, vgpu_prfl |
| 245 |
) |
| 246 |
vgpu_fact = self._gather_vgpu_profile_facts(vm_obj) |
| 247 |
result = {"changed": changed, "failed": failed, "vgpu": vgpu_fact} |
| 248 |
return result |
| 249 |
|
| 250 |
def _add_vgpu_profile_to_vm(self, vm_obj, vgpu_profile_name, vgpu_prfl): |
| 251 |
""" |
| 252 |
Add vGPU profile of virtual machine |
| 253 |
Args: |
| 254 |
vm_obj: Managed object of virtual machine |
| 255 |
vgpu_profile_name: vGPU profile object name from ESXi server list |
| 256 |
vgpu_prfl: vGPU profile name |
| 257 |
Returns: Operation results |
| 258 |
""" |
| 259 |
changed = False |
| 260 |
failed = False |
| 261 |
vm_current_vgpu_profile = self._get_vgpu_profile_in_the_vm(vm_obj) |
| 262 |
if self.params["force"] or vgpu_prfl not in vm_current_vgpu_profile: |
| 263 |
vgpu_p = vgpu_profile_name.vgpu |
| 264 |
backing = vim.VirtualPCIPassthroughVmiopBackingInfo(vgpu=vgpu_p) |
| 265 |
summary = "NVIDIA GRID vGPU " + vgpu_prfl |
| 266 |
deviceInfo = vim.Description(summary=summary, label="PCI device 0") |
| 267 |
hba_object = vim.VirtualPCIPassthrough( |
| 268 |
backing=backing, deviceInfo=deviceInfo |
| 269 |
) |
| 270 |
new_device_config = vim.VirtualDeviceConfigSpec(device=hba_object) |
| 271 |
new_device_config.operation = "add" |
| 272 |
vmConfigSpec = vim.vm.ConfigSpec() |
| 273 |
vmConfigSpec.deviceChange = [new_device_config] |
| 274 |
vmConfigSpec.memoryReservationLockedToMax = True |
| 275 |
|
| 276 |
try: |
| 277 |
task = vm_obj.ReconfigVM_Task(spec=vmConfigSpec) |
| 278 |
wait_for_task(task) |
| 279 |
changed = True |
| 280 |
except Exception as exc: |
| 281 |
failed = True |
| 282 |
self.module.fail_json( |
| 283 |
msg="Failed to add vGPU Profile" |
| 284 |
" '%s' to vm %s." % (vgpu_prfl, vm_obj.name), |
| 285 |
detail=exc.msg, |
| 286 |
) |
| 287 |
else: |
| 288 |
return changed, failed |
| 289 |
return changed, failed |
| 290 |
|
| 291 |
def _get_vgpu_profile_in_the_vm(self, vm_obj): |
| 292 |
""" |
| 293 |
Get vGPU profile object of virtual machine |
| 294 |
Args: |
| 295 |
vm_obj: Managed object of virtual machine |
| 296 |
Returns: vGPU profile name |
| 297 |
""" |
| 298 |
vm_current_vgpu_profile = [] |
| 299 |
for vgpu_VirtualDevice_obj in vm_obj.config.hardware.device: |
| 300 |
if hasattr(vgpu_VirtualDevice_obj.backing, "vgpu"): |
| 301 |
vm_current_vgpu_profile.append(vgpu_VirtualDevice_obj.backing.vgpu) |
| 302 |
return vm_current_vgpu_profile |
| 303 |
|
| 304 |
def _get_vgpu_VirtualDevice_object(self, vm_obj, vgpu_prfl): |
| 305 |
""" |
| 306 |
Get current vGPU profile object of virtual machine |
| 307 |
Args: |
| 308 |
vm_obj: Managed object of virtual machine |
| 309 |
vgpu_prfl: vGPU profile name |
| 310 |
Returns: vGPU profile name of virtual machine |
| 311 |
""" |
| 312 |
for vgpu_VirtualDevice_obj in vm_obj.config.hardware.device: |
| 313 |
if hasattr(vgpu_VirtualDevice_obj.backing, "vgpu"): |
| 314 |
if vgpu_VirtualDevice_obj.backing.vgpu == vgpu_prfl: |
| 315 |
return vgpu_VirtualDevice_obj |
| 316 |
return None |
| 317 |
|
| 318 |
def _get_vgpu_profiles_name(self, vm_obj, vgpu_prfl): |
| 319 |
""" |
| 320 |
Get matched vGPU profile object of ESXi host |
| 321 |
Args: |
| 322 |
vm_obj: Managed object of virtual machine |
| 323 |
vgpu_prfl: vGPU profile name |
| 324 |
Returns: vGPU profile object |
| 325 |
""" |
| 326 |
vm_host = vm_obj.runtime.host |
| 327 |
vgpu_profiles = vm_host.config.sharedGpuCapabilities |
| 328 |
for vgpu_profile_name in vgpu_profiles: |
| 329 |
if vgpu_profile_name.vgpu == vgpu_prfl: |
| 330 |
return vgpu_profile_name |
| 331 |
return None |
| 332 |
|
| 333 |
|
| 334 |
def main(): |
| 335 |
argument_spec = base_argument_spec() |
| 336 |
argument_spec.update( |
| 337 |
name=dict(type="str"), |
| 338 |
uuid=dict(type="str"), |
| 339 |
use_instance_uuid=dict(type="bool", default=False), |
| 340 |
moid=dict(type="str"), |
| 341 |
folder=dict(type="str"), |
| 342 |
datacenter=dict(type="str", default="ha-datacenter"), |
| 343 |
esxi_hostname=dict(type="str"), |
| 344 |
cluster=dict(type="str"), |
| 345 |
vgpu=dict(type="str"), |
| 346 |
force=dict(type="bool", default=False), |
| 347 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 348 |
) |
| 349 |
|
| 350 |
module = AnsibleModule( |
| 351 |
argument_spec=argument_spec, |
| 352 |
mutually_exclusive=[["cluster", "esxi_hostname"]], |
| 353 |
required_one_of=[["name", "uuid", "moid"]], |
| 354 |
) |
| 355 |
|
| 356 |
pyv = PyVmomiHelper(module) |
| 357 |
vm = pyv.get_vm() |
| 358 |
|
| 359 |
if not vm: |
| 360 |
vm_id = ( |
| 361 |
module.params.get("uuid") |
| 362 |
or module.params.get("name") |
| 363 |
or module.params.get("moid") |
| 364 |
) |
| 365 |
module.fail_json( |
| 366 |
msg="Unable to manage vGPU profile for non-existing VM %s" % vm_id |
| 367 |
) |
| 368 |
|
| 369 |
if module.params["state"] == "present": |
| 370 |
result = pyv._vgpu_present(vm) |
| 371 |
elif module.params["state"] == "absent": |
| 372 |
result = pyv._vgpu_absent(vm) |
| 373 |
|
| 374 |
if "failed" not in result: |
| 375 |
result["failed"] = False |
| 376 |
|
| 377 |
if result["failed"]: |
| 378 |
module.fail_json(**result) |
| 379 |
else: |
| 380 |
module.exit_json(**result) |
| 381 |
|
| 382 |
|
| 383 |
if __name__ == "__main__": |
| 384 |
main() |