From db519e10500ecace188ecef2aecda5744a4ccf26 Mon Sep 17 00:00:00 2001 From: bri <284789+b-@users.noreply.github.com> Date: Thu, 14 Sep 2023 21:12:58 -0400 Subject: [PATCH 01/11] add vmid exit value if create succeeds, we want the vmid --- plugins/modules/proxmox.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/proxmox.py b/plugins/modules/proxmox.py index 4c0220e6e55..eb9f885fb6d 100644 --- a/plugins/modules/proxmox.py +++ b/plugins/modules/proxmox.py @@ -733,7 +733,7 @@ def main(): timezone=module.params['timezone'], tags=module.params['tags']) - module.exit_json(changed=True, msg="Deployed VM %s from template %s" % (vmid, module.params['ostemplate'])) + module.exit_json(changed=True, msg="Deployed VM %s from template %s" % (vmid, module.params['ostemplate']), vmid) except Exception as e: module.fail_json(msg="Creation of %s VM %s failed with exception: %s" % (VZ_TYPE, vmid, e)) From 7028da2d15c063c0b54d5794c71ec9c7b8eab911 Mon Sep 17 00:00:00 2001 From: bri <284789+b-@users.noreply.github.com> Date: Thu, 14 Sep 2023 21:22:28 -0400 Subject: [PATCH 02/11] fix syntax --- plugins/modules/proxmox.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/proxmox.py b/plugins/modules/proxmox.py index eb9f885fb6d..204bfb1be14 100644 --- a/plugins/modules/proxmox.py +++ b/plugins/modules/proxmox.py @@ -733,7 +733,7 @@ def main(): timezone=module.params['timezone'], tags=module.params['tags']) - module.exit_json(changed=True, msg="Deployed VM %s from template %s" % (vmid, module.params['ostemplate']), vmid) + module.exit_json(changed=True, msg="Deployed VM %s from template %s" % (vmid, module.params['ostemplate']), vmid=vmid) except Exception as e: module.fail_json(msg="Creation of %s VM %s failed with exception: %s" % (VZ_TYPE, vmid, e)) From f559275f46d1fdddd0fa95d0bdee13ea51e34eb5 Mon Sep 17 00:00:00 2001 From: bri Date: Fri, 15 Sep 2023 01:43:00 +0000 Subject: [PATCH 03/11] add vmid to return codes --- plugins/modules/proxmox.py | 82 +++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/plugins/modules/proxmox.py b/plugins/modules/proxmox.py index 204bfb1be14..5a5d06ef0c4 100644 --- a/plugins/modules/proxmox.py +++ b/plugins/modules/proxmox.py @@ -469,7 +469,7 @@ def create_instance(self, vmid, node, disk, storage, cpus, memory, swap, timeout # Fail on unsupported features for option, version in minimum_version.items(): if pve_version < LooseVersion(version) and option in kwargs: - self.module.fail_json(changed=False, msg="Feature {option} is only supported in PVE {version}+, and you're using PVE {pve_version}". + self.module.fail_json(changed=False, vmid=vmid, msg="Feature {option} is only supported in PVE {version}+, and you're using PVE {pve_version}". format(option=option, version=version, pve_version=pve_version)) if VZ_TYPE == 'lxc': @@ -494,12 +494,12 @@ def create_instance(self, vmid, node, disk, storage, cpus, memory, swap, timeout re_tag = re.compile(r'^[a-z0-9_][a-z0-9_\-\+\.]*$') for tag in kwargs['tags']: if not re_tag.match(tag): - self.module.fail_json(msg='%s is not a valid tag' % tag) + self.module.fail_json(vmid=vmid, msg='%s is not a valid tag' % tag) kwargs['tags'] = ",".join(kwargs['tags']) if clone is not None: if VZ_TYPE != 'lxc': - self.module.fail_json(changed=False, msg="Clone operator is only supported for LXC enabled proxmox clusters.") + self.module.fail_json(changed=False, vmid=vmid, msg="Clone operator is only supported for LXC enabled proxmox clusters.") clone_is_template = self.is_template_container(node, clone) @@ -513,11 +513,11 @@ def create_instance(self, vmid, node, disk, storage, cpus, memory, swap, timeout create_full_copy = True elif self.module.params['storage'] is None and not clone_is_template: # Not cloning a template, but also no defined storage. This isn't possible. - self.module.fail_json(changed=False, msg="Cloned container is not a template, storage needs to be specified.") + self.module.fail_json(changed=False, vmid=vmid, msg="Cloned container is not a template, storage needs to be specified.") if self.module.params['clone_type'] == 'linked': if not clone_is_template: - self.module.fail_json(changed=False, msg="'linked' clone type is specified, but cloned container is not a template container.") + self.module.fail_json(changed=False, vmid=vmid, msg="'linked' clone type is specified, but cloned container is not a template container.") # Don't need to do more, by default create_full_copy is set to false already elif self.module.params['clone_type'] == 'opportunistic': if not clone_is_template: @@ -546,7 +546,7 @@ def create_instance(self, vmid, node, disk, storage, cpus, memory, swap, timeout return True timeout -= 1 if timeout == 0: - self.module.fail_json(msg='Reached timeout while waiting for creating VM. Last line in task before timeout: %s' % + self.module.fail_json(vmid=vmid, msg='Reached timeout while waiting for creating VM. Last line in task before timeout: %s' % proxmox_node.tasks(taskid).log.get()[:1]) time.sleep(1) @@ -559,7 +559,7 @@ def start_instance(self, vm, vmid, timeout): return True timeout -= 1 if timeout == 0: - self.module.fail_json(msg='Reached timeout while waiting for starting VM. Last line in task before timeout: %s' % + self.module.fail_json(vmid=vmid, msg='Reached timeout while waiting for starting VM. Last line in task before timeout: %s' % self.proxmox_api.nodes(vm['node']).tasks(taskid).log.get()[:1]) time.sleep(1) @@ -575,7 +575,7 @@ def stop_instance(self, vm, vmid, timeout, force): return True timeout -= 1 if timeout == 0: - self.module.fail_json(msg='Reached timeout while waiting for stopping VM. Last line in task before timeout: %s' % + self.module.fail_json(vmid=vmid, msg='Reached timeout while waiting for stopping VM. Last line in task before timeout: %s' % self.proxmox_api.nodes(vm['node']).tasks(taskid).log.get()[:1]) time.sleep(1) @@ -588,7 +588,7 @@ def umount_instance(self, vm, vmid, timeout): return True timeout -= 1 if timeout == 0: - self.module.fail_json(msg='Reached timeout while waiting for unmounting VM. Last line in task before timeout: %s' % + self.module.fail_json(vmid=vmid, msg='Reached timeout while waiting for unmounting VM. Last line in task before timeout: %s' % self.proxmox_api.nodes(vm['node']).tasks(taskid).log.get()[:1]) time.sleep(1) @@ -689,26 +689,26 @@ def main(): elif not vmid and hostname: vmid = proxmox.get_vmid(hostname) elif not vmid: - module.exit_json(changed=False, msg="Vmid could not be fetched for the following action: %s" % state) + module.exit_json(changed=False, vmid=vmid, msg="Vmid could not be fetched for the following action: %s" % state) # Create a new container if state == 'present' and clone is None: try: if proxmox.get_vm(vmid, ignore_missing=True) and not module.params['force']: - module.exit_json(changed=False, msg="VM with vmid = %s is already exists" % vmid) + module.exit_json(changed=False, vmid=vmid, msg="VM with vmid = %s is already exists" % vmid) # If no vmid was passed, there cannot be another VM named 'hostname' if (not module.params['vmid'] and proxmox.get_vmid(hostname, ignore_missing=True) and not module.params['force']): vmid = proxmox.get_vmid(hostname) - module.exit_json(changed=False, msg="VM with hostname %s already exists and has ID number %s" % (hostname, vmid)) + module.exit_json(changed=False, vmid=vmid, msg="VM with hostname %s already exists and has ID number %s" % (hostname, vmid)) elif not proxmox.get_node(node): - module.fail_json(msg="node '%s' not exists in cluster" % node) + module.fail_json(vmid=vmid, msg="node '%s' not exists in cluster" % node) elif not proxmox.content_check(node, module.params['ostemplate'], template_store): - module.fail_json(msg="ostemplate '%s' not exists on node %s and storage %s" + module.fail_json(vmid=vmid, msg="ostemplate '%s' not exists on node %s and storage %s" % (module.params['ostemplate'], node, template_store)) except Exception as e: - module.fail_json(msg="Pre-creation checks of {VZ_TYPE} VM {vmid} failed with exception: {e}".format(VZ_TYPE=VZ_TYPE, vmid=vmid, e=e)) + module.fail_json(vmid=vmid, msg="Pre-creation checks of {VZ_TYPE} VM {vmid} failed with exception: {e}".format(VZ_TYPE=VZ_TYPE, vmid=vmid, e=e)) try: proxmox.create_instance(vmid, node, disk, storage, cpus, memory, swap, timeout, clone, @@ -733,43 +733,43 @@ def main(): timezone=module.params['timezone'], tags=module.params['tags']) - module.exit_json(changed=True, msg="Deployed VM %s from template %s" % (vmid, module.params['ostemplate']), vmid=vmid) + module.exit_json(changed=True, vmid=vmid, msg="Deployed VM %s from template %s" % (vmid, module.params['ostemplate'])) except Exception as e: - module.fail_json(msg="Creation of %s VM %s failed with exception: %s" % (VZ_TYPE, vmid, e)) + module.fail_json(vmid=vmid, msg="Creation of %s VM %s failed with exception: %s" % (VZ_TYPE, vmid, e)) # Clone a container elif state == 'present' and clone is not None: try: if proxmox.get_vm(vmid, ignore_missing=True) and not module.params['force']: - module.exit_json(changed=False, msg="VM with vmid = %s is already exists" % vmid) + module.exit_json(changed=False, vmid=vmid, msg="VM with vmid = %s is already exists" % vmid) # If no vmid was passed, there cannot be another VM named 'hostname' if (not module.params['vmid'] and proxmox.get_vmid(hostname, ignore_missing=True) and not module.params['force']): vmid = proxmox.get_vmid(hostname) - module.exit_json(changed=False, msg="VM with hostname %s already exists and has ID number %s" % (hostname, vmid)) + module.exit_json(changed=False, vmid=vmid, msg="VM with hostname %s already exists and has ID number %s" % (hostname, vmid)) if not proxmox.get_vm(clone, ignore_missing=True): - module.exit_json(changed=False, msg="Container to be cloned does not exist") + module.exit_json(changed=False, vmid=vmid, msg="Container to be cloned does not exist") except Exception as e: - module.fail_json(msg="Pre-clone checks of {VZ_TYPE} VM {vmid} failed with exception: {e}".format(VZ_TYPE=VZ_TYPE, vmid=vmid, e=e)) + module.fail_json(vmid=vmid, msg="Pre-clone checks of {VZ_TYPE} VM {vmid} failed with exception: {e}".format(VZ_TYPE=VZ_TYPE, vmid=vmid, e=e)) try: proxmox.create_instance(vmid, node, disk, storage, cpus, memory, swap, timeout, clone) - module.exit_json(changed=True, msg="Cloned VM %s from %s" % (vmid, clone)) + module.exit_json(changed=True, vmid=vmid, msg="Cloned VM %s from %s" % (vmid, clone)) except Exception as e: - module.fail_json(msg="Cloning %s VM %s failed with exception: %s" % (VZ_TYPE, vmid, e)) + module.fail_json(vmid=vmid, msg="Cloning %s VM %s failed with exception: %s" % (VZ_TYPE, vmid, e)) elif state == 'started': try: vm = proxmox.get_vm(vmid) if getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'running': - module.exit_json(changed=False, msg="VM %s is already running" % vmid) + module.exit_json(changed=False, vmid=vmid, msg="VM %s is already running" % vmid) if proxmox.start_instance(vm, vmid, timeout): - module.exit_json(changed=True, msg="VM %s started" % vmid) + module.exit_json(changed=True, vmid=vmid, msg="VM %s started" % vmid) except Exception as e: - module.fail_json(msg="starting of VM %s failed with exception: %s" % (vmid, e)) + module.fail_json(vmid=vmid, msg="starting of VM %s failed with exception: %s" % (vmid, e)) elif state == 'stopped': try: @@ -778,18 +778,18 @@ def main(): if getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'mounted': if module.params['force']: if proxmox.umount_instance(vm, vmid, timeout): - module.exit_json(changed=True, msg="VM %s is shutting down" % vmid) + module.exit_json(changed=True, vmid=vmid, msg="VM %s is shutting down" % vmid) else: - module.exit_json(changed=False, msg=("VM %s is already shutdown, but mounted. " + module.exit_json(changed=False, vmid=vmid, msg=("VM %s is already shutdown, but mounted. " "You can use force option to umount it.") % vmid) if getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'stopped': - module.exit_json(changed=False, msg="VM %s is already shutdown" % vmid) + module.exit_json(changed=False, vmid=vmid, msg="VM %s is already shutdown" % vmid) if proxmox.stop_instance(vm, vmid, timeout, force=module.params['force']): - module.exit_json(changed=True, msg="VM %s is shutting down" % vmid) + module.exit_json(changed=True, vmid=vmid, msg="VM %s is shutting down" % vmid) except Exception as e: - module.fail_json(msg="stopping of VM %s failed with exception: %s" % (vmid, e)) + module.fail_json(vmid=vmid, msg="stopping of VM %s failed with exception: %s" % (vmid, e)) elif state == 'restarted': try: @@ -797,28 +797,28 @@ def main(): vm_status = getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status'] if vm_status in ['stopped', 'mounted']: - module.exit_json(changed=False, msg="VM %s is not running" % vmid) + module.exit_json(changed=False, vmid=vmid, msg="VM %s is not running" % vmid) if (proxmox.stop_instance(vm, vmid, timeout, force=module.params['force']) and proxmox.start_instance(vm, vmid, timeout)): - module.exit_json(changed=True, msg="VM %s is restarted" % vmid) + module.exit_json(changed=True, vmid=vmid, msg="VM %s is restarted" % vmid) except Exception as e: - module.fail_json(msg="restarting of VM %s failed with exception: %s" % (vmid, e)) + module.fail_json(vmid=vmid, msg="restarting of VM %s failed with exception: %s" % (vmid, e)) elif state == 'absent': if not vmid: - module.exit_json(changed=False, msg='VM with hostname = %s is already absent' % hostname) + module.exit_json(changed=False, vmid=vmid, msg='VM with hostname = %s is already absent' % hostname) try: vm = proxmox.get_vm(vmid, ignore_missing=True) if not vm: - module.exit_json(changed=False, msg="VM %s does not exist" % vmid) + module.exit_json(changed=False, vmid=vmid, msg="VM %s does not exist" % vmid) vm_status = getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status'] if vm_status == 'running': - module.exit_json(changed=False, msg="VM %s is running. Stop it before deletion." % vmid) + module.exit_json(changed=False, vmid=vmid, msg="VM %s is running. Stop it before deletion." % vmid) if vm_status == 'mounted': - module.exit_json(changed=False, msg="VM %s is mounted. Stop it with force option before deletion." % vmid) + module.exit_json(changed=False, vmid=vmid, msg="VM %s is mounted. Stop it with force option before deletion." % vmid) delete_params = {} @@ -829,15 +829,15 @@ def main(): while timeout: if proxmox.api_task_ok(vm['node'], taskid): - module.exit_json(changed=True, msg="VM %s removed" % vmid) + module.exit_json(changed=True, vmid=vmid, msg="VM %s removed" % vmid) timeout -= 1 if timeout == 0: - module.fail_json(msg='Reached timeout while waiting for removing VM. Last line in task before timeout: %s' + module.fail_json(vmid=vmid, msg='Reached timeout while waiting for removing VM. Last line in task before timeout: %s' % proxmox.proxmox_api.nodes(vm['node']).tasks(taskid).log.get()[:1]) time.sleep(1) except Exception as e: - module.fail_json(msg="deletion of VM %s failed with exception: %s" % (vmid, to_native(e))) + module.fail_json(vmid=vmid, msg="deletion of VM %s failed with exception: %s" % (vmid, to_native(e))) if __name__ == '__main__': From 74e61e3eff7085d0046d3735bac239428c3af0c6 Mon Sep 17 00:00:00 2001 From: bri Date: Fri, 15 Sep 2023 02:15:26 +0000 Subject: [PATCH 04/11] Add taskid to return, and only return vmid when it makes sense to --- plugins/modules/proxmox.py | 82 +++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/plugins/modules/proxmox.py b/plugins/modules/proxmox.py index 5a5d06ef0c4..4a5fa2e0b8e 100644 --- a/plugins/modules/proxmox.py +++ b/plugins/modules/proxmox.py @@ -469,7 +469,7 @@ def create_instance(self, vmid, node, disk, storage, cpus, memory, swap, timeout # Fail on unsupported features for option, version in minimum_version.items(): if pve_version < LooseVersion(version) and option in kwargs: - self.module.fail_json(changed=False, vmid=vmid, msg="Feature {option} is only supported in PVE {version}+, and you're using PVE {pve_version}". + self.module.fail_json(changed=False, msg="Feature {option} is only supported in PVE {version}+, and you're using PVE {pve_version}". format(option=option, version=version, pve_version=pve_version)) if VZ_TYPE == 'lxc': @@ -494,12 +494,12 @@ def create_instance(self, vmid, node, disk, storage, cpus, memory, swap, timeout re_tag = re.compile(r'^[a-z0-9_][a-z0-9_\-\+\.]*$') for tag in kwargs['tags']: if not re_tag.match(tag): - self.module.fail_json(vmid=vmid, msg='%s is not a valid tag' % tag) + self.module.fail_json(msg='%s is not a valid tag' % tag) kwargs['tags'] = ",".join(kwargs['tags']) if clone is not None: if VZ_TYPE != 'lxc': - self.module.fail_json(changed=False, vmid=vmid, msg="Clone operator is only supported for LXC enabled proxmox clusters.") + self.module.fail_json(changed=False, msg="Clone operator is only supported for LXC enabled proxmox clusters.") clone_is_template = self.is_template_container(node, clone) @@ -513,11 +513,11 @@ def create_instance(self, vmid, node, disk, storage, cpus, memory, swap, timeout create_full_copy = True elif self.module.params['storage'] is None and not clone_is_template: # Not cloning a template, but also no defined storage. This isn't possible. - self.module.fail_json(changed=False, vmid=vmid, msg="Cloned container is not a template, storage needs to be specified.") + self.module.fail_json(changed=False, msg="Cloned container is not a template, storage needs to be specified.") if self.module.params['clone_type'] == 'linked': if not clone_is_template: - self.module.fail_json(changed=False, vmid=vmid, msg="'linked' clone type is specified, but cloned container is not a template container.") + self.module.fail_json(changed=False, msg="'linked' clone type is specified, but cloned container is not a template container.") # Don't need to do more, by default create_full_copy is set to false already elif self.module.params['clone_type'] == 'opportunistic': if not clone_is_template: @@ -546,7 +546,7 @@ def create_instance(self, vmid, node, disk, storage, cpus, memory, swap, timeout return True timeout -= 1 if timeout == 0: - self.module.fail_json(vmid=vmid, msg='Reached timeout while waiting for creating VM. Last line in task before timeout: %s' % + self.module.fail_json(vmid=vmid, taskid=taskid, node=node, msg='Reached timeout while waiting for creating VM. Last line in task before timeout: %s' % proxmox_node.tasks(taskid).log.get()[:1]) time.sleep(1) @@ -559,7 +559,7 @@ def start_instance(self, vm, vmid, timeout): return True timeout -= 1 if timeout == 0: - self.module.fail_json(vmid=vmid, msg='Reached timeout while waiting for starting VM. Last line in task before timeout: %s' % + self.module.fail_json(vmid=vmid, taskid=taskid, msg='Reached timeout while waiting for starting VM. Last line in task before timeout: %s' % self.proxmox_api.nodes(vm['node']).tasks(taskid).log.get()[:1]) time.sleep(1) @@ -575,7 +575,7 @@ def stop_instance(self, vm, vmid, timeout, force): return True timeout -= 1 if timeout == 0: - self.module.fail_json(vmid=vmid, msg='Reached timeout while waiting for stopping VM. Last line in task before timeout: %s' % + self.module.fail_json(vmid=vmid, taskid=taskid, msg='Reached timeout while waiting for stopping VM. Last line in task before timeout: %s' % self.proxmox_api.nodes(vm['node']).tasks(taskid).log.get()[:1]) time.sleep(1) @@ -588,7 +588,7 @@ def umount_instance(self, vm, vmid, timeout): return True timeout -= 1 if timeout == 0: - self.module.fail_json(vmid=vmid, msg='Reached timeout while waiting for unmounting VM. Last line in task before timeout: %s' % + self.module.fail_json(vmid=vmid, taskid=taskid, msg='Reached timeout while waiting for unmounting VM. Last line in task before timeout: %s' % self.proxmox_api.nodes(vm['node']).tasks(taskid).log.get()[:1]) time.sleep(1) @@ -689,26 +689,26 @@ def main(): elif not vmid and hostname: vmid = proxmox.get_vmid(hostname) elif not vmid: - module.exit_json(changed=False, vmid=vmid, msg="Vmid could not be fetched for the following action: %s" % state) + module.exit_json(changed=False, msg="Vmid could not be fetched for the following action: %s" % state) # Create a new container if state == 'present' and clone is None: try: if proxmox.get_vm(vmid, ignore_missing=True) and not module.params['force']: - module.exit_json(changed=False, vmid=vmid, msg="VM with vmid = %s is already exists" % vmid) + module.exit_json(changed=False, vmid=vmid, taskid=taskid, msg="VM with vmid = %s is already exists" % vmid) # If no vmid was passed, there cannot be another VM named 'hostname' if (not module.params['vmid'] and proxmox.get_vmid(hostname, ignore_missing=True) and not module.params['force']): vmid = proxmox.get_vmid(hostname) - module.exit_json(changed=False, vmid=vmid, msg="VM with hostname %s already exists and has ID number %s" % (hostname, vmid)) + module.exit_json(changed=False, vmid=vmid, taskid=taskid, msg="VM with hostname %s already exists and has ID number %s" % (hostname, vmid)) elif not proxmox.get_node(node): - module.fail_json(vmid=vmid, msg="node '%s' not exists in cluster" % node) + module.fail_json(vmid=vmid, taskid=taskid, msg="node '%s' not exists in cluster" % node) elif not proxmox.content_check(node, module.params['ostemplate'], template_store): - module.fail_json(vmid=vmid, msg="ostemplate '%s' not exists on node %s and storage %s" + module.fail_json(vmid=vmid, taskid=taskid, msg="ostemplate '%s' not exists on node %s and storage %s" % (module.params['ostemplate'], node, template_store)) except Exception as e: - module.fail_json(vmid=vmid, msg="Pre-creation checks of {VZ_TYPE} VM {vmid} failed with exception: {e}".format(VZ_TYPE=VZ_TYPE, vmid=vmid, e=e)) + module.fail_json(vmid=vmid, taskid=taskid, msg="Pre-creation checks of {VZ_TYPE} VM {vmid} failed with exception: {e}".format(VZ_TYPE=VZ_TYPE, vmid=vmid, e=e)) try: proxmox.create_instance(vmid, node, disk, storage, cpus, memory, swap, timeout, clone, @@ -733,43 +733,43 @@ def main(): timezone=module.params['timezone'], tags=module.params['tags']) - module.exit_json(changed=True, vmid=vmid, msg="Deployed VM %s from template %s" % (vmid, module.params['ostemplate'])) + module.exit_json(changed=True, vmid=vmid, taskid=taskid, msg="Deployed VM %s from template %s" % (vmid, module.params['ostemplate'])) except Exception as e: - module.fail_json(vmid=vmid, msg="Creation of %s VM %s failed with exception: %s" % (VZ_TYPE, vmid, e)) + module.fail_json(vmid=vmid, taskid=taskid, msg="Creation of %s VM %s failed with exception: %s" % (VZ_TYPE, vmid, e)) # Clone a container elif state == 'present' and clone is not None: try: if proxmox.get_vm(vmid, ignore_missing=True) and not module.params['force']: - module.exit_json(changed=False, vmid=vmid, msg="VM with vmid = %s is already exists" % vmid) + module.exit_json(changed=False, vmid=vmid, taskid=taskid, msg="VM with vmid = %s is already exists" % vmid) # If no vmid was passed, there cannot be another VM named 'hostname' if (not module.params['vmid'] and proxmox.get_vmid(hostname, ignore_missing=True) and not module.params['force']): vmid = proxmox.get_vmid(hostname) - module.exit_json(changed=False, vmid=vmid, msg="VM with hostname %s already exists and has ID number %s" % (hostname, vmid)) + module.exit_json(changed=False, vmid=vmid, taskid=taskid, msg="VM with hostname %s already exists and has ID number %s" % (hostname, vmid)) if not proxmox.get_vm(clone, ignore_missing=True): - module.exit_json(changed=False, vmid=vmid, msg="Container to be cloned does not exist") + module.exit_json(changed=False, vmid=vmid, taskid=taskid, msg="Container to be cloned does not exist") except Exception as e: - module.fail_json(vmid=vmid, msg="Pre-clone checks of {VZ_TYPE} VM {vmid} failed with exception: {e}".format(VZ_TYPE=VZ_TYPE, vmid=vmid, e=e)) + module.fail_json(vmid=vmid, taskid=taskid, msg="Pre-clone checks of {VZ_TYPE} VM {vmid} failed with exception: {e}".format(VZ_TYPE=VZ_TYPE, vmid=vmid, e=e)) try: proxmox.create_instance(vmid, node, disk, storage, cpus, memory, swap, timeout, clone) - module.exit_json(changed=True, vmid=vmid, msg="Cloned VM %s from %s" % (vmid, clone)) + module.exit_json(changed=True, vmid=vmid, taskid=taskid, msg="Cloned VM %s from %s" % (vmid, clone)) except Exception as e: - module.fail_json(vmid=vmid, msg="Cloning %s VM %s failed with exception: %s" % (VZ_TYPE, vmid, e)) + module.fail_json(vmid=vmid, taskid=taskid, msg="Cloning %s VM %s failed with exception: %s" % (VZ_TYPE, vmid, e)) elif state == 'started': try: vm = proxmox.get_vm(vmid) if getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'running': - module.exit_json(changed=False, vmid=vmid, msg="VM %s is already running" % vmid) + module.exit_json(changed=False, vmid=vmid, taskid=taskid, msg="VM %s is already running" % vmid) if proxmox.start_instance(vm, vmid, timeout): - module.exit_json(changed=True, vmid=vmid, msg="VM %s started" % vmid) + module.exit_json(changed=True, vmid=vmid, taskid=taskid, msg="VM %s started" % vmid) except Exception as e: - module.fail_json(vmid=vmid, msg="starting of VM %s failed with exception: %s" % (vmid, e)) + module.fail_json(vmid=vmid, taskid=taskid, msg="starting of VM %s failed with exception: %s" % (vmid, e)) elif state == 'stopped': try: @@ -778,18 +778,18 @@ def main(): if getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'mounted': if module.params['force']: if proxmox.umount_instance(vm, vmid, timeout): - module.exit_json(changed=True, vmid=vmid, msg="VM %s is shutting down" % vmid) + module.exit_json(changed=True, vmid=vmid, taskid=taskid, msg="VM %s is shutting down" % vmid) else: - module.exit_json(changed=False, vmid=vmid, msg=("VM %s is already shutdown, but mounted. " + module.exit_json(changed=False, vmid=vmid, taskid=taskid, msg=("VM %s is already shutdown, but mounted. " "You can use force option to umount it.") % vmid) if getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'stopped': - module.exit_json(changed=False, vmid=vmid, msg="VM %s is already shutdown" % vmid) + module.exit_json(changed=False, vmid=vmid, taskid=taskid, msg="VM %s is already shutdown" % vmid) if proxmox.stop_instance(vm, vmid, timeout, force=module.params['force']): - module.exit_json(changed=True, vmid=vmid, msg="VM %s is shutting down" % vmid) + module.exit_json(changed=True, vmid=vmid, taskid=taskid, msg="VM %s is shutting down" % vmid) except Exception as e: - module.fail_json(vmid=vmid, msg="stopping of VM %s failed with exception: %s" % (vmid, e)) + module.fail_json(vmid=vmid, taskid=taskid, msg="stopping of VM %s failed with exception: %s" % (vmid, e)) elif state == 'restarted': try: @@ -797,28 +797,28 @@ def main(): vm_status = getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status'] if vm_status in ['stopped', 'mounted']: - module.exit_json(changed=False, vmid=vmid, msg="VM %s is not running" % vmid) + module.exit_json(changed=False, vmid=vmid, taskid=taskid, msg="VM %s is not running" % vmid) if (proxmox.stop_instance(vm, vmid, timeout, force=module.params['force']) and proxmox.start_instance(vm, vmid, timeout)): - module.exit_json(changed=True, vmid=vmid, msg="VM %s is restarted" % vmid) + module.exit_json(changed=True, vmid=vmid, taskid=taskid, msg="VM %s is restarted" % vmid) except Exception as e: - module.fail_json(vmid=vmid, msg="restarting of VM %s failed with exception: %s" % (vmid, e)) + module.fail_json(vmid=vmid, taskid=taskid, msg="restarting of VM %s failed with exception: %s" % (vmid, e)) elif state == 'absent': if not vmid: - module.exit_json(changed=False, vmid=vmid, msg='VM with hostname = %s is already absent' % hostname) + module.exit_json(changed=False, vmid=vmid, taskid=taskid, msg='VM with hostname = %s is already absent' % hostname) try: vm = proxmox.get_vm(vmid, ignore_missing=True) if not vm: - module.exit_json(changed=False, vmid=vmid, msg="VM %s does not exist" % vmid) + module.exit_json(changed=False, vmid=vmid, taskid=taskid, msg="VM %s does not exist" % vmid) vm_status = getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status'] if vm_status == 'running': - module.exit_json(changed=False, vmid=vmid, msg="VM %s is running. Stop it before deletion." % vmid) + module.exit_json(changed=False, vmid=vmid, taskid=taskid, msg="VM %s is running. Stop it before deletion." % vmid) if vm_status == 'mounted': - module.exit_json(changed=False, vmid=vmid, msg="VM %s is mounted. Stop it with force option before deletion." % vmid) + module.exit_json(changed=False, vmid=vmid, taskid=taskid, msg="VM %s is mounted. Stop it with force option before deletion." % vmid) delete_params = {} @@ -829,15 +829,15 @@ def main(): while timeout: if proxmox.api_task_ok(vm['node'], taskid): - module.exit_json(changed=True, vmid=vmid, msg="VM %s removed" % vmid) + module.exit_json(changed=True, vmid=vmid, taskid=taskid, msg="VM %s removed" % vmid) timeout -= 1 if timeout == 0: - module.fail_json(vmid=vmid, msg='Reached timeout while waiting for removing VM. Last line in task before timeout: %s' + module.fail_json(vmid=vmid, taskid=taskid, msg='Reached timeout while waiting for removing VM. Last line in task before timeout: %s' % proxmox.proxmox_api.nodes(vm['node']).tasks(taskid).log.get()[:1]) time.sleep(1) except Exception as e: - module.fail_json(vmid=vmid, msg="deletion of VM %s failed with exception: %s" % (vmid, to_native(e))) + module.fail_json(vmid=vmid, taskid=taskid, msg="deletion of VM %s failed with exception: %s" % (vmid, to_native(e))) if __name__ == '__main__': From 6f76b1eeb6b3b8564bb672bfedf941498bbb2254 Mon Sep 17 00:00:00 2001 From: bri Date: Fri, 15 Sep 2023 02:17:03 +0000 Subject: [PATCH 05/11] add changelog fragment with temporary filename --- changelogs/fragments/xxxx-proxmox-return-vmid-and-taskid | 2 ++ plugins/modules/proxmox.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/xxxx-proxmox-return-vmid-and-taskid diff --git a/changelogs/fragments/xxxx-proxmox-return-vmid-and-taskid b/changelogs/fragments/xxxx-proxmox-return-vmid-and-taskid new file mode 100644 index 00000000000..f2f44fe6c71 --- /dev/null +++ b/changelogs/fragments/xxxx-proxmox-return-vmid-and-taskid @@ -0,0 +1,2 @@ +minor_changes: + - proxmox - add vmid and taskid to return codes diff --git a/plugins/modules/proxmox.py b/plugins/modules/proxmox.py index 4a5fa2e0b8e..2068a72beb1 100644 --- a/plugins/modules/proxmox.py +++ b/plugins/modules/proxmox.py @@ -546,7 +546,7 @@ def create_instance(self, vmid, node, disk, storage, cpus, memory, swap, timeout return True timeout -= 1 if timeout == 0: - self.module.fail_json(vmid=vmid, taskid=taskid, node=node, msg='Reached timeout while waiting for creating VM. Last line in task before timeout: %s' % + self.module.fail_json(vmid=vmid, taskid=taskid, msg='Reached timeout while waiting for creating VM. Last line in task before timeout: %s' % proxmox_node.tasks(taskid).log.get()[:1]) time.sleep(1) From 86d32efa30984576a85ea287e8266806389eb0b2 Mon Sep 17 00:00:00 2001 From: bri Date: Fri, 15 Sep 2023 02:43:23 +0000 Subject: [PATCH 06/11] Add pr number to fragment --- ...return-vmid-and-taskid => 7263-proxmox-return-vmid-and-taskid} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename changelogs/fragments/{xxxx-proxmox-return-vmid-and-taskid => 7263-proxmox-return-vmid-and-taskid} (100%) diff --git a/changelogs/fragments/xxxx-proxmox-return-vmid-and-taskid b/changelogs/fragments/7263-proxmox-return-vmid-and-taskid similarity index 100% rename from changelogs/fragments/xxxx-proxmox-return-vmid-and-taskid rename to changelogs/fragments/7263-proxmox-return-vmid-and-taskid From 24a16458cac4fed6e03460181059d765bc97fac2 Mon Sep 17 00:00:00 2001 From: bri Date: Fri, 15 Sep 2023 03:09:51 +0000 Subject: [PATCH 07/11] fix PEP8 E501: line too long --- plugins/modules/proxmox.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/plugins/modules/proxmox.py b/plugins/modules/proxmox.py index 2068a72beb1..f83d3253847 100644 --- a/plugins/modules/proxmox.py +++ b/plugins/modules/proxmox.py @@ -708,7 +708,8 @@ def main(): module.fail_json(vmid=vmid, taskid=taskid, msg="ostemplate '%s' not exists on node %s and storage %s" % (module.params['ostemplate'], node, template_store)) except Exception as e: - module.fail_json(vmid=vmid, taskid=taskid, msg="Pre-creation checks of {VZ_TYPE} VM {vmid} failed with exception: {e}".format(VZ_TYPE=VZ_TYPE, vmid=vmid, e=e)) + module.fail_json(vmid=vmid, taskid=taskid, + msg="Pre-creation checks of {VZ_TYPE} VM {vmid} failed with exception: {e}".format(VZ_TYPE=VZ_TYPE, vmid=vmid, e=e)) try: proxmox.create_instance(vmid, node, disk, storage, cpus, memory, swap, timeout, clone, @@ -751,7 +752,8 @@ def main(): if not proxmox.get_vm(clone, ignore_missing=True): module.exit_json(changed=False, vmid=vmid, taskid=taskid, msg="Container to be cloned does not exist") except Exception as e: - module.fail_json(vmid=vmid, taskid=taskid, msg="Pre-clone checks of {VZ_TYPE} VM {vmid} failed with exception: {e}".format(VZ_TYPE=VZ_TYPE, vmid=vmid, e=e)) + module.fail_json(vmid=vmid, taskid=taskid, + msg="Pre-clone checks of {VZ_TYPE} VM {vmid} failed with exception: {e}".format(VZ_TYPE=VZ_TYPE, vmid=vmid, e=e)) try: proxmox.create_instance(vmid, node, disk, storage, cpus, memory, swap, timeout, clone) From a1557aaeb51a3732b5944be7f33d29742b9d5b5b Mon Sep 17 00:00:00 2001 From: bri Date: Fri, 15 Sep 2023 03:51:14 +0000 Subject: [PATCH 08/11] oops, I knew I still missed something... --- ...n-vmid-and-taskid => 7263-proxmox-return-vmid-and-taskid.yaml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename changelogs/fragments/{7263-proxmox-return-vmid-and-taskid => 7263-proxmox-return-vmid-and-taskid.yaml} (100%) diff --git a/changelogs/fragments/7263-proxmox-return-vmid-and-taskid b/changelogs/fragments/7263-proxmox-return-vmid-and-taskid.yaml similarity index 100% rename from changelogs/fragments/7263-proxmox-return-vmid-and-taskid rename to changelogs/fragments/7263-proxmox-return-vmid-and-taskid.yaml From 5ae879adeb74143564762b50cd6e8e30288108d0 Mon Sep 17 00:00:00 2001 From: bri <284789+b-@users.noreply.github.com> Date: Fri, 15 Sep 2023 01:52:17 -0400 Subject: [PATCH 09/11] Update 7263-proxmox-return-vmid-and-taskid.yaml Co-authored-by: Felix Fontein --- changelogs/fragments/7263-proxmox-return-vmid-and-taskid.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelogs/fragments/7263-proxmox-return-vmid-and-taskid.yaml b/changelogs/fragments/7263-proxmox-return-vmid-and-taskid.yaml index f2f44fe6c71..0ee3638d908 100644 --- a/changelogs/fragments/7263-proxmox-return-vmid-and-taskid.yaml +++ b/changelogs/fragments/7263-proxmox-return-vmid-and-taskid.yaml @@ -1,2 +1,2 @@ minor_changes: - - proxmox - add vmid and taskid to return codes + - proxmox - add ``vmid`` and ``taskid`` to return values (https://github.com/ansible-collections/community.general/pull/7263). From 149992e935ba4797f13adad5bab9a7751e69e82b Mon Sep 17 00:00:00 2001 From: bri Date: Fri, 15 Sep 2023 22:00:59 +0000 Subject: [PATCH 10/11] removed taskid from places it's not defined --- .../7263-proxmox-return-vmid-and-taskid.yaml | 2 +- plugins/modules/proxmox.py | 58 +++++++++---------- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/changelogs/fragments/7263-proxmox-return-vmid-and-taskid.yaml b/changelogs/fragments/7263-proxmox-return-vmid-and-taskid.yaml index 0ee3638d908..01b2ceeaf68 100644 --- a/changelogs/fragments/7263-proxmox-return-vmid-and-taskid.yaml +++ b/changelogs/fragments/7263-proxmox-return-vmid-and-taskid.yaml @@ -1,2 +1,2 @@ minor_changes: - - proxmox - add ``vmid`` and ``taskid`` to return values (https://github.com/ansible-collections/community.general/pull/7263). + - proxmox - add ``vmid`` (and ``taskid`` when possible) to return values (https://github.com/ansible-collections/community.general/pull/7263). diff --git a/plugins/modules/proxmox.py b/plugins/modules/proxmox.py index f83d3253847..da1cd866784 100644 --- a/plugins/modules/proxmox.py +++ b/plugins/modules/proxmox.py @@ -695,20 +695,20 @@ def main(): if state == 'present' and clone is None: try: if proxmox.get_vm(vmid, ignore_missing=True) and not module.params['force']: - module.exit_json(changed=False, vmid=vmid, taskid=taskid, msg="VM with vmid = %s is already exists" % vmid) + module.exit_json(changed=False, vmid=vmid, msg="VM with vmid = %s is already exists" % vmid) # If no vmid was passed, there cannot be another VM named 'hostname' if (not module.params['vmid'] and proxmox.get_vmid(hostname, ignore_missing=True) and not module.params['force']): vmid = proxmox.get_vmid(hostname) - module.exit_json(changed=False, vmid=vmid, taskid=taskid, msg="VM with hostname %s already exists and has ID number %s" % (hostname, vmid)) + module.exit_json(changed=False, vmid=vmid, msg="VM with hostname %s already exists and has ID number %s" % (hostname, vmid)) elif not proxmox.get_node(node): - module.fail_json(vmid=vmid, taskid=taskid, msg="node '%s' not exists in cluster" % node) + module.fail_json(vmid=vmid, msg="node '%s' not exists in cluster" % node) elif not proxmox.content_check(node, module.params['ostemplate'], template_store): - module.fail_json(vmid=vmid, taskid=taskid, msg="ostemplate '%s' not exists on node %s and storage %s" + module.fail_json(vmid=vmid, msg="ostemplate '%s' not exists on node %s and storage %s" % (module.params['ostemplate'], node, template_store)) except Exception as e: - module.fail_json(vmid=vmid, taskid=taskid, + module.fail_json(vmid=vmid, msg="Pre-creation checks of {VZ_TYPE} VM {vmid} failed with exception: {e}".format(VZ_TYPE=VZ_TYPE, vmid=vmid, e=e)) try: @@ -734,44 +734,44 @@ def main(): timezone=module.params['timezone'], tags=module.params['tags']) - module.exit_json(changed=True, vmid=vmid, taskid=taskid, msg="Deployed VM %s from template %s" % (vmid, module.params['ostemplate'])) + module.exit_json(changed=True, vmid=vmid, msg="Deployed VM %s from template %s" % (vmid, module.params['ostemplate'])) except Exception as e: - module.fail_json(vmid=vmid, taskid=taskid, msg="Creation of %s VM %s failed with exception: %s" % (VZ_TYPE, vmid, e)) + module.fail_json(vmid=vmid, msg="Creation of %s VM %s failed with exception: %s" % (VZ_TYPE, vmid, e)) # Clone a container elif state == 'present' and clone is not None: try: if proxmox.get_vm(vmid, ignore_missing=True) and not module.params['force']: - module.exit_json(changed=False, vmid=vmid, taskid=taskid, msg="VM with vmid = %s is already exists" % vmid) + module.exit_json(changed=False, vmid=vmid, msg="VM with vmid = %s is already exists" % vmid) # If no vmid was passed, there cannot be another VM named 'hostname' if (not module.params['vmid'] and proxmox.get_vmid(hostname, ignore_missing=True) and not module.params['force']): vmid = proxmox.get_vmid(hostname) - module.exit_json(changed=False, vmid=vmid, taskid=taskid, msg="VM with hostname %s already exists and has ID number %s" % (hostname, vmid)) + module.exit_json(changed=False, vmid=vmid, msg="VM with hostname %s already exists and has ID number %s" % (hostname, vmid)) if not proxmox.get_vm(clone, ignore_missing=True): - module.exit_json(changed=False, vmid=vmid, taskid=taskid, msg="Container to be cloned does not exist") + module.exit_json(changed=False, vmid=vmid, msg="Container to be cloned does not exist") except Exception as e: - module.fail_json(vmid=vmid, taskid=taskid, + module.fail_json(vmid=vmid, msg="Pre-clone checks of {VZ_TYPE} VM {vmid} failed with exception: {e}".format(VZ_TYPE=VZ_TYPE, vmid=vmid, e=e)) try: proxmox.create_instance(vmid, node, disk, storage, cpus, memory, swap, timeout, clone) - module.exit_json(changed=True, vmid=vmid, taskid=taskid, msg="Cloned VM %s from %s" % (vmid, clone)) + module.exit_json(changed=True, vmid=vmid, msg="Cloned VM %s from %s" % (vmid, clone)) except Exception as e: - module.fail_json(vmid=vmid, taskid=taskid, msg="Cloning %s VM %s failed with exception: %s" % (VZ_TYPE, vmid, e)) + module.fail_json(vmid=vmid, msg="Cloning %s VM %s failed with exception: %s" % (VZ_TYPE, vmid, e)) elif state == 'started': try: vm = proxmox.get_vm(vmid) if getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'running': - module.exit_json(changed=False, vmid=vmid, taskid=taskid, msg="VM %s is already running" % vmid) + module.exit_json(changed=False, vmid=vmid, msg="VM %s is already running" % vmid) if proxmox.start_instance(vm, vmid, timeout): - module.exit_json(changed=True, vmid=vmid, taskid=taskid, msg="VM %s started" % vmid) + module.exit_json(changed=True, vmid=vmid, msg="VM %s started" % vmid) except Exception as e: - module.fail_json(vmid=vmid, taskid=taskid, msg="starting of VM %s failed with exception: %s" % (vmid, e)) + module.fail_json(vmid=vmid, msg="starting of VM %s failed with exception: %s" % (vmid, e)) elif state == 'stopped': try: @@ -780,18 +780,18 @@ def main(): if getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'mounted': if module.params['force']: if proxmox.umount_instance(vm, vmid, timeout): - module.exit_json(changed=True, vmid=vmid, taskid=taskid, msg="VM %s is shutting down" % vmid) + module.exit_json(changed=True, vmid=vmid, msg="VM %s is shutting down" % vmid) else: - module.exit_json(changed=False, vmid=vmid, taskid=taskid, msg=("VM %s is already shutdown, but mounted. " + module.exit_json(changed=False, vmid=vmid, msg=("VM %s is already shutdown, but mounted. " "You can use force option to umount it.") % vmid) if getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'stopped': - module.exit_json(changed=False, vmid=vmid, taskid=taskid, msg="VM %s is already shutdown" % vmid) + module.exit_json(changed=False, vmid=vmid, msg="VM %s is already shutdown" % vmid) if proxmox.stop_instance(vm, vmid, timeout, force=module.params['force']): - module.exit_json(changed=True, vmid=vmid, taskid=taskid, msg="VM %s is shutting down" % vmid) + module.exit_json(changed=True, vmid=vmid, msg="VM %s is shutting down" % vmid) except Exception as e: - module.fail_json(vmid=vmid, taskid=taskid, msg="stopping of VM %s failed with exception: %s" % (vmid, e)) + module.fail_json(vmid=vmid, msg="stopping of VM %s failed with exception: %s" % (vmid, e)) elif state == 'restarted': try: @@ -799,28 +799,28 @@ def main(): vm_status = getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status'] if vm_status in ['stopped', 'mounted']: - module.exit_json(changed=False, vmid=vmid, taskid=taskid, msg="VM %s is not running" % vmid) + module.exit_json(changed=False, vmid=vmid, msg="VM %s is not running" % vmid) if (proxmox.stop_instance(vm, vmid, timeout, force=module.params['force']) and proxmox.start_instance(vm, vmid, timeout)): - module.exit_json(changed=True, vmid=vmid, taskid=taskid, msg="VM %s is restarted" % vmid) + module.exit_json(changed=True, vmid=vmid, msg="VM %s is restarted" % vmid) except Exception as e: - module.fail_json(vmid=vmid, taskid=taskid, msg="restarting of VM %s failed with exception: %s" % (vmid, e)) + module.fail_json(vmid=vmid, msg="restarting of VM %s failed with exception: %s" % (vmid, e)) elif state == 'absent': if not vmid: - module.exit_json(changed=False, vmid=vmid, taskid=taskid, msg='VM with hostname = %s is already absent' % hostname) + module.exit_json(changed=False, vmid=vmid, msg='VM with hostname = %s is already absent' % hostname) try: vm = proxmox.get_vm(vmid, ignore_missing=True) if not vm: - module.exit_json(changed=False, vmid=vmid, taskid=taskid, msg="VM %s does not exist" % vmid) + module.exit_json(changed=False, vmid=vmid, msg="VM %s does not exist" % vmid) vm_status = getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status'] if vm_status == 'running': - module.exit_json(changed=False, vmid=vmid, taskid=taskid, msg="VM %s is running. Stop it before deletion." % vmid) + module.exit_json(changed=False, vmid=vmid, msg="VM %s is running. Stop it before deletion." % vmid) if vm_status == 'mounted': - module.exit_json(changed=False, vmid=vmid, taskid=taskid, msg="VM %s is mounted. Stop it with force option before deletion." % vmid) + module.exit_json(changed=False, vmid=vmid, msg="VM %s is mounted. Stop it with force option before deletion." % vmid) delete_params = {} @@ -839,7 +839,7 @@ def main(): time.sleep(1) except Exception as e: - module.fail_json(vmid=vmid, taskid=taskid, msg="deletion of VM %s failed with exception: %s" % (vmid, to_native(e))) + module.fail_json(vmid=vmid, msg="deletion of VM %s failed with exception: %s" % (vmid, to_native(e))) if __name__ == '__main__': From 9e418a9a614509a3556b3cf6092a1e910061fc8c Mon Sep 17 00:00:00 2001 From: bri Date: Sat, 16 Sep 2023 20:46:55 +0000 Subject: [PATCH 11/11] finally fixed sanity test errors Okay, so maybe just waiting for CI to run the tests was a terrible idea. I installed `inotifywait`, set up a venv for the tests, and in my editor's terminal pane I ran the following, letting it run every save until it exited: until ansible-test sanity proxmox ; do inotifywait --event modify plugins/modules/proxmox.py done --- plugins/modules/proxmox.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/plugins/modules/proxmox.py b/plugins/modules/proxmox.py index da1cd866784..5ef842a2a33 100644 --- a/plugins/modules/proxmox.py +++ b/plugins/modules/proxmox.py @@ -546,7 +546,7 @@ def create_instance(self, vmid, node, disk, storage, cpus, memory, swap, timeout return True timeout -= 1 if timeout == 0: - self.module.fail_json(vmid=vmid, taskid=taskid, msg='Reached timeout while waiting for creating VM. Last line in task before timeout: %s' % + self.module.fail_json(vmid=vmid, node=node, msg='Reached timeout while waiting for creating VM. Last line in task before timeout: %s' % proxmox_node.tasks(taskid).log.get()[:1]) time.sleep(1) @@ -708,8 +708,7 @@ def main(): module.fail_json(vmid=vmid, msg="ostemplate '%s' not exists on node %s and storage %s" % (module.params['ostemplate'], node, template_store)) except Exception as e: - module.fail_json(vmid=vmid, - msg="Pre-creation checks of {VZ_TYPE} VM {vmid} failed with exception: {e}".format(VZ_TYPE=VZ_TYPE, vmid=vmid, e=e)) + module.fail_json(vmid=vmid, msg="Pre-creation checks of {VZ_TYPE} VM {vmid} failed with exception: {e}".format(VZ_TYPE=VZ_TYPE, vmid=vmid, e=e)) try: proxmox.create_instance(vmid, node, disk, storage, cpus, memory, swap, timeout, clone, @@ -752,8 +751,7 @@ def main(): if not proxmox.get_vm(clone, ignore_missing=True): module.exit_json(changed=False, vmid=vmid, msg="Container to be cloned does not exist") except Exception as e: - module.fail_json(vmid=vmid, - msg="Pre-clone checks of {VZ_TYPE} VM {vmid} failed with exception: {e}".format(VZ_TYPE=VZ_TYPE, vmid=vmid, e=e)) + module.fail_json(vmid=vmid, msg="Pre-clone checks of {VZ_TYPE} VM {vmid} failed with exception: {e}".format(VZ_TYPE=VZ_TYPE, vmid=vmid, e=e)) try: proxmox.create_instance(vmid, node, disk, storage, cpus, memory, swap, timeout, clone) @@ -782,8 +780,8 @@ def main(): if proxmox.umount_instance(vm, vmid, timeout): module.exit_json(changed=True, vmid=vmid, msg="VM %s is shutting down" % vmid) else: - module.exit_json(changed=False, vmid=vmid, msg=("VM %s is already shutdown, but mounted. " - "You can use force option to umount it.") % vmid) + module.exit_json(changed=False, vmid=vmid, + msg=("VM %s is already shutdown, but mounted. You can use force option to umount it.") % vmid) if getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'stopped': module.exit_json(changed=False, vmid=vmid, msg="VM %s is already shutdown" % vmid)