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
pull/27649/head
DavidVentura 7 years ago committed by John R Barker
parent c85f363aaa
commit c85b36d220

@ -344,7 +344,7 @@ def get_nextvmid(module, proxmox):
def get_vmid(proxmox, hostname): 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): def get_instance(proxmox, vmid):
@ -517,7 +517,10 @@ def main():
if not vmid and state == 'present': if not vmid and state == 'present':
vmid = get_nextvmid(module, proxmox) vmid = get_nextvmid(module, proxmox)
elif not vmid and hostname: 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: 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, msg="Vmid could not be fetched for the following action: %s" % state)

Loading…
Cancel
Save