From 9b44109ba3091e6538dedfac2987cf31793e892d Mon Sep 17 00:00:00 2001 From: Diane Wang <41371902+Tomorrow9@users.noreply.github.com> Date: Wed, 21 Nov 2018 09:27:35 -0800 Subject: [PATCH] Fix pr44962 add check before hot add cpu or memory (#48289) --- .../modules/cloud/vmware/vmware_guest.py | 35 ++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/lib/ansible/modules/cloud/vmware/vmware_guest.py b/lib/ansible/modules/cloud/vmware/vmware_guest.py index e1a4d56a21..6b156be8c2 100644 --- a/lib/ansible/modules/cloud/vmware/vmware_guest.py +++ b/lib/ansible/modules/cloud/vmware/vmware_guest.py @@ -919,6 +919,14 @@ class PyVmomiHelper(PyVmomi): num_cpus = int(self.params['hardware']['num_cpus']) except ValueError as e: self.module.fail_json(msg="hardware.num_cpus attribute should be an integer value.") + # check VM power state and cpu hot-add/hot-remove state before re-config VM + if vm_obj and vm_obj.runtime.powerState == vim.VirtualMachinePowerState.poweredOn: + if not vm_obj.config.cpuHotRemoveEnabled and num_cpus < vm_obj.config.hardware.numCPU: + self.module.fail_json(msg="Configured cpu number is less than the cpu number of the VM, " + "cpuHotRemove is not enabled") + if not vm_obj.config.cpuHotAddEnabled and num_cpus > vm_obj.config.hardware.numCPU: + self.module.fail_json(msg="Configured cpu number is more than the cpu number of the VM, " + "cpuHotAdd is not enabled") if 'num_cpu_cores_per_socket' in self.params['hardware']: try: @@ -929,11 +937,9 @@ class PyVmomiHelper(PyVmomi): if num_cpus % num_cpu_cores_per_socket != 0: self.module.fail_json(msg="hardware.num_cpus attribute should be a multiple " "of hardware.num_cpu_cores_per_socket") - self.configspec.numCoresPerSocket = num_cpu_cores_per_socket if vm_obj is None or self.configspec.numCoresPerSocket != vm_obj.config.hardware.numCoresPerSocket: self.change_detected = True - self.configspec.numCPUs = num_cpus if vm_obj is None or self.configspec.numCPUs != vm_obj.config.hardware.numCPU: self.change_detected = True @@ -943,11 +949,19 @@ class PyVmomiHelper(PyVmomi): if 'memory_mb' in self.params['hardware']: try: - self.configspec.memoryMB = int(self.params['hardware']['memory_mb']) + memory_mb = int(self.params['hardware']['memory_mb']) except ValueError: self.module.fail_json(msg="Failed to parse hardware.memory_mb value." " Please refer the documentation and provide" " correct value.") + # check VM power state and memory hotadd state before re-config VM + if vm_obj and vm_obj.runtime.powerState == vim.VirtualMachinePowerState.poweredOn: + if vm_obj.config.memoryHotAddEnabled and memory_mb < vm_obj.config.hardware.memoryMB: + self.module.fail_json(msg="Configured memory is less than memory size of the VM, " + "operation is not supported") + elif not vm_obj.config.memoryHotAddEnabled and memory_mb != vm_obj.config.hardware.memoryMB: + self.module.fail_json(msg="memoryHotAdd is not enabled") + self.configspec.memoryMB = memory_mb if vm_obj is None or self.configspec.memoryMB != vm_obj.config.hardware.memoryMB: self.change_detected = True # memory_mb is mandatory for VM creation @@ -955,16 +969,25 @@ class PyVmomiHelper(PyVmomi): self.module.fail_json(msg="hardware.memory_mb attribute is mandatory for VM creation") if 'hotadd_memory' in self.params['hardware']: + if vm_obj and vm_obj.runtime.powerState == vim.VirtualMachinePowerState.poweredOn and \ + vm_obj.config.memoryHotAddEnabled != bool(self.params['hardware']['hotadd_memory']): + self.module.fail_json(msg="Configure hotadd memory operation is not supported when VM is power on") self.configspec.memoryHotAddEnabled = bool(self.params['hardware']['hotadd_memory']) if vm_obj is None or self.configspec.memoryHotAddEnabled != vm_obj.config.memoryHotAddEnabled: self.change_detected = True if 'hotadd_cpu' in self.params['hardware']: + if vm_obj and vm_obj.runtime.powerState == vim.VirtualMachinePowerState.poweredOn and \ + vm_obj.config.cpuHotAddEnabled != bool(self.params['hardware']['hotadd_cpu']): + self.module.fail_json(msg="Configure hotadd cpu operation is not supported when VM is power on") self.configspec.cpuHotAddEnabled = bool(self.params['hardware']['hotadd_cpu']) if vm_obj is None or self.configspec.cpuHotAddEnabled != vm_obj.config.cpuHotAddEnabled: self.change_detected = True if 'hotremove_cpu' in self.params['hardware']: + if vm_obj and vm_obj.runtime.powerState == vim.VirtualMachinePowerState.poweredOn and \ + vm_obj.config.cpuHotRemoveEnabled != bool(self.params['hardware']['hotremove_cpu']): + self.module.fail_json(msg="Configure hotremove cpu operation is not supported when VM is power on") self.configspec.cpuHotRemoveEnabled = bool(self.params['hardware']['hotremove_cpu']) if vm_obj is None or self.configspec.cpuHotRemoveEnabled != vm_obj.config.cpuHotRemoveEnabled: self.change_detected = True @@ -989,13 +1012,15 @@ class PyVmomiHelper(PyVmomi): self.change_detected = True if 'boot_firmware' in self.params['hardware']: + # boot firmware re-config can cause boot issue + if vm_obj is not None: + return boot_firmware = self.params['hardware']['boot_firmware'].lower() if boot_firmware not in ('bios', 'efi'): self.module.fail_json(msg="hardware.boot_firmware value is invalid [%s]." " Need one of ['bios', 'efi']." % boot_firmware) self.configspec.firmware = boot_firmware - if vm_obj is None or self.configspec.firmware != vm_obj.config.firmware: - self.change_detected = True + self.change_detected = True def configure_cdrom(self, vm_obj): # Configure the VM CD-ROM