From c85b36d22070dcd712d598b3a478a6984a447f16 Mon Sep 17 00:00:00 2001 From: DavidVentura Date: Wed, 2 Aug 2017 11:42:05 -0300 Subject: [PATCH] Fix Proxmox module crashing if the hostname doesn't exist and there's no vmid (#21305) * fail the execution instead of panicking when the hostname is not found and the vmid was not provided * return an empty vmid list if the hostname doesn't exist --- lib/ansible/modules/cloud/misc/proxmox.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/ansible/modules/cloud/misc/proxmox.py b/lib/ansible/modules/cloud/misc/proxmox.py index 27ed2690828..8b2e5e87c7c 100644 --- a/lib/ansible/modules/cloud/misc/proxmox.py +++ b/lib/ansible/modules/cloud/misc/proxmox.py @@ -344,7 +344,7 @@ def get_nextvmid(module, proxmox): def get_vmid(proxmox, hostname): - return [vm['vmid'] for vm in proxmox.cluster.resources.get(type='vm') if vm['name'] == hostname] + return [vm['vmid'] for vm in proxmox.cluster.resources.get(type='vm') if 'name' in vm and vm['name'] == hostname] def get_instance(proxmox, vmid): @@ -517,7 +517,10 @@ def main(): if not vmid and state == 'present': vmid = get_nextvmid(module, proxmox) elif not vmid and hostname: - vmid = get_vmid(proxmox, hostname)[0] + hosts = get_vmid(proxmox, hostname) + if len(hosts) == 0: + module.fail_json(msg="Vmid could not be fetched => Hostname doesn't exist (action: %s)" % state) + vmid = hosts[0] elif not vmid: module.exit_json(changed=False, msg="Vmid could not be fetched for the following action: %s" % state)