While attempting to extend the disk of a Virtual Machine (vm) using PyVmomi, the task shows as failed in vCenter but completes the disk resize.
The Virtual Machine's host disconnects.
The Reconfigure Virtual Machine task shows as failed with "An error occurred while communicating with the remote host." on vCenter.
Reconfiguration of Virtual Machine functions as expected through manual execution in the GUI.
Occurs if the reconfiguration task calls for a new Virtual Disk device instead of accessing the Virtual Disk that already exists on the vm.
To resolve this issue, make sure to call the appropriate Virtual Disk device when setting the spec for the reconfigure task. Below is an example of what that looks like.
def modify_vm_disk(new_vm, disk_size_gb): content = service_instance.content new_vm_name = None disk_spec = None vm_view = content.viewManager.CreateContainerView(content.rootFolder, [vim.VirtualMachine], True) vm_list = vm_view.view disk_to_resize = None for vm in vm_list: if vm.name == new_vm: for device in vm.config.hardware.device: if isinstance(device, vim.vm.device.VirtualDisk): if device.deviceInfo.label == "Hard disk 1": disk_to_resize = device break new_vm_name = vm spec = vim.vm.ConfigSpec() spec_deviceChange_0 = vim.vm.device.VirtualDeviceSpec() spec_deviceChange_0.device = disk_to_resize spec_deviceChange_0.device.capacityInKB = disk_size_gb * 1024 * 1024 spec_deviceChange_0.operation = vim.vm.device.VirtualDeviceSpec.Operation.edit spec.deviceChange = [spec_deviceChange_0] task = new_vm_name.Reconfigure(spec=spec)
For more information on using vSphere SDKs and pyVmomi, refer to: