From 248e2ff321156f43535b4d24de926b6557f5efae Mon Sep 17 00:00:00 2001 From: nxet Date: Wed, 28 Feb 2024 21:36:22 +0100 Subject: [PATCH] [FIX] proxmox_kvm: fetch vm status from node-specific API endpoint to ensure fresh state (#7953) * proxmox_kvm: fetch vm status from node-specific API endpoint to ensure fresh state, fixes #7817 * changelog fragments * Fix changelog fragment. --------- Co-authored-by: Felix Fontein --- .../7953-proxmox_kvm-fix_status_check.yml | 2 ++ plugins/modules/proxmox_kvm.py | 21 +++++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) create mode 100644 changelogs/fragments/7953-proxmox_kvm-fix_status_check.yml diff --git a/changelogs/fragments/7953-proxmox_kvm-fix_status_check.yml b/changelogs/fragments/7953-proxmox_kvm-fix_status_check.yml new file mode 100644 index 0000000000..10f8e6d26a --- /dev/null +++ b/changelogs/fragments/7953-proxmox_kvm-fix_status_check.yml @@ -0,0 +1,2 @@ +bugfixes: + - proxmox_kvm - fixed status check getting from node-specific API endpoint (https://github.com/ansible-collections/community.general/issues/7817). diff --git a/plugins/modules/proxmox_kvm.py b/plugins/modules/proxmox_kvm.py index d180180cb4..8779dcdc1f 100644 --- a/plugins/modules/proxmox_kvm.py +++ b/plugins/modules/proxmox_kvm.py @@ -1547,8 +1547,9 @@ def main(): status = {} try: vm = proxmox.get_vm(vmid) - status['status'] = vm['status'] - if vm['status'] == 'running': + current = proxmox.proxmox_api.nodes(vm['node']).qemu(vmid).status.current.get()['status'] + status['status'] = current + if current == 'running': module.exit_json(changed=False, vmid=vmid, msg="VM %s is already running" % vmid, **status) if proxmox.start_vm(vm): @@ -1563,9 +1564,9 @@ def main(): status = {} try: vm = proxmox.get_vm(vmid) - - status['status'] = vm['status'] - if vm['status'] == 'stopped': + current = proxmox.proxmox_api.nodes(vm['node']).qemu(vmid).status.current.get()['status'] + status['status'] = current + if current == 'stopped': module.exit_json(changed=False, vmid=vmid, msg="VM %s is already stopped" % vmid, **status) proxmox.stop_vm(vm, force=module.params['force'], timeout=module.params['timeout']) @@ -1595,8 +1596,9 @@ def main(): status = {} vm = proxmox.get_vm(vmid) - status['status'] = vm['status'] - if vm['status'] == 'stopped': + current = proxmox.proxmox_api.nodes(vm['node']).qemu(vmid).status.current.get()['status'] + status['status'] = current + if current == 'stopped': module.exit_json(changed=False, vmid=vmid, msg="VM %s is not running" % vmid, **status) if proxmox.restart_vm(vm, force=module.params['force']): @@ -1613,8 +1615,9 @@ def main(): module.exit_json(changed=False, vmid=vmid) proxmox_node = proxmox.proxmox_api.nodes(vm['node']) - status['status'] = vm['status'] - if vm['status'] == 'running': + current = proxmox_node.qemu(vmid).status.current.get()['status'] + status['status'] = current + if current == 'running': if module.params['force']: proxmox.stop_vm(vm, True, timeout=module.params['timeout']) else: