From db2aac42540a9af69fe58ac055c2e5b97923701b Mon Sep 17 00:00:00 2001 From: miyuk Date: Tue, 12 Jul 2022 11:48:32 +0900 Subject: [PATCH 1/5] add int parse handling --- plugins/module_utils/proxmox.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/module_utils/proxmox.py b/plugins/module_utils/proxmox.py index 56f0b35e7f5..ed40fbe95f8 100644 --- a/plugins/module_utils/proxmox.py +++ b/plugins/module_utils/proxmox.py @@ -127,7 +127,8 @@ def get_vmid(self, name, ignore_missing=False, choose_first_if_multiple=False): return vms[0] def get_vm(self, vmid, ignore_missing=False): - vms = [vm for vm in self.proxmox_api.cluster.resources.get(type='vm') if vm['vmid'] == int(vmid)] + vmid = int(vmid) if vmid is not None else None + vms = [vm for vm in self.proxmox_api.cluster.resources.get(type='vm') if vmid and vm['vmid'] == vmid] if vms: return vms[0] From 1779bc6dad26ab6a31bd6f4c223e275db3bb0714 Mon Sep 17 00:00:00 2001 From: miyuk Date: Wed, 13 Jul 2022 23:49:45 +0900 Subject: [PATCH 2/5] Revert "add int parse handling" This reverts commit db2aac42540a9af69fe58ac055c2e5b97923701b. --- plugins/module_utils/proxmox.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plugins/module_utils/proxmox.py b/plugins/module_utils/proxmox.py index ed40fbe95f8..56f0b35e7f5 100644 --- a/plugins/module_utils/proxmox.py +++ b/plugins/module_utils/proxmox.py @@ -127,8 +127,7 @@ def get_vmid(self, name, ignore_missing=False, choose_first_if_multiple=False): return vms[0] def get_vm(self, vmid, ignore_missing=False): - vmid = int(vmid) if vmid is not None else None - vms = [vm for vm in self.proxmox_api.cluster.resources.get(type='vm') if vmid and vm['vmid'] == vmid] + vms = [vm for vm in self.proxmox_api.cluster.resources.get(type='vm') if vm['vmid'] == int(vmid)] if vms: return vms[0] From 6e1aafbcd1def7de98a1674ae0950ddd09daaa45 Mon Sep 17 00:00:00 2001 From: miyuk Date: Thu, 14 Jul 2022 00:21:48 +0900 Subject: [PATCH 3/5] fix: vmid check if state is absent --- plugins/modules/cloud/misc/proxmox.py | 2 ++ plugins/modules/cloud/misc/proxmox_kvm.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/plugins/modules/cloud/misc/proxmox.py b/plugins/modules/cloud/misc/proxmox.py index da8783e16de..dccfdc7b673 100644 --- a/plugins/modules/cloud/misc/proxmox.py +++ b/plugins/modules/cloud/misc/proxmox.py @@ -743,6 +743,8 @@ def main(): module.fail_json(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) try: vm = proxmox.get_vm(vmid, ignore_missing=True) if not vm: diff --git a/plugins/modules/cloud/misc/proxmox_kvm.py b/plugins/modules/cloud/misc/proxmox_kvm.py index abc63f24c34..2b38e899645 100644 --- a/plugins/modules/cloud/misc/proxmox_kvm.py +++ b/plugins/modules/cloud/misc/proxmox_kvm.py @@ -1370,6 +1370,8 @@ def main(): elif state == 'absent': status = {} + if not vmid: + module.exit_json(changed=False, msg='VM with name = %s is already absent' % name) try: vm = proxmox.get_vm(vmid, ignore_missing=True) if not vm: From e992bc3ff3586e9c21568962c94baaebd4fd2e11 Mon Sep 17 00:00:00 2001 From: miyuk Date: Thu, 14 Jul 2022 21:41:16 +0900 Subject: [PATCH 4/5] add changelogs fragments --- changelogs/fragments/4945-fix-get_vm-int-parse-handling.yaml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 changelogs/fragments/4945-fix-get_vm-int-parse-handling.yaml diff --git a/changelogs/fragments/4945-fix-get_vm-int-parse-handling.yaml b/changelogs/fragments/4945-fix-get_vm-int-parse-handling.yaml new file mode 100644 index 00000000000..8c9abf57f85 --- /dev/null +++ b/changelogs/fragments/4945-fix-get_vm-int-parse-handling.yaml @@ -0,0 +1,3 @@ +bugfixes: + - proxmox - fix error handling for get vm by name when state absent (https://github.com/ansible-collections/community.general/pull/4945). + - proxmox_kvm - fix error handling for get vm by name when state absent (https://github.com/ansible-collections/community.general/pull/4945). From f928f64ca60ebeb66ad478bfac7881cbd6280d92 Mon Sep 17 00:00:00 2001 From: miyuk Date: Thu, 14 Jul 2022 23:21:55 +0900 Subject: [PATCH 5/5] Update changelogs/fragments/4945-fix-get_vm-int-parse-handling.yaml Co-authored-by: Felix Fontein --- changelogs/fragments/4945-fix-get_vm-int-parse-handling.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/changelogs/fragments/4945-fix-get_vm-int-parse-handling.yaml b/changelogs/fragments/4945-fix-get_vm-int-parse-handling.yaml index 8c9abf57f85..1a563f6c93f 100644 --- a/changelogs/fragments/4945-fix-get_vm-int-parse-handling.yaml +++ b/changelogs/fragments/4945-fix-get_vm-int-parse-handling.yaml @@ -1,3 +1,3 @@ bugfixes: - - proxmox - fix error handling for get vm by name when state absent (https://github.com/ansible-collections/community.general/pull/4945). - - proxmox_kvm - fix error handling for get vm by name when state absent (https://github.com/ansible-collections/community.general/pull/4945). + - proxmox - fix error handling when getting VM by name when ``state=absent`` (https://github.com/ansible-collections/community.general/pull/4945). + - proxmox_kvm - fix error handling when getting VM by name when ``state=absent`` (https://github.com/ansible-collections/community.general/pull/4945).