diff --git a/changelogs/fragments/6981-proxmox-fix-vm-creation-when-only-name-provided.yml b/changelogs/fragments/6981-proxmox-fix-vm-creation-when-only-name-provided.yml new file mode 100644 index 0000000000..0dbd617734 --- /dev/null +++ b/changelogs/fragments/6981-proxmox-fix-vm-creation-when-only-name-provided.yml @@ -0,0 +1,2 @@ +bugfixes: + - proxmox_kvm - when ``name`` option is provided without ``vmid`` and VM with that name already exists then no new VM will be created (https://github.com/ansible-collections/community.general/issues/6911, https://github.com/ansible-collections/community.general/pull/6981). diff --git a/plugins/modules/proxmox_kvm.py b/plugins/modules/proxmox_kvm.py index 73ebd8ba0d..817fb8f410 100644 --- a/plugins/modules/proxmox_kvm.py +++ b/plugins/modules/proxmox_kvm.py @@ -281,8 +281,9 @@ options: type: int name: description: - - Specifies the VM name. Only used on the configuration web interface. - - Required only for C(state=present). + - Specifies the VM name. Name could be non-unique across the cluster. + - Required only for I(state=present). + - With I(state=present) if I(vmid) not provided and VM with name exists in the cluster then no changes will be made. type: str nameservers: description: @@ -1210,10 +1211,14 @@ def main(): # the cloned vm name or retrieve the next free VM id from ProxmoxAPI. if not vmid: if state == 'present' and not update and not clone and not delete and not revert: - try: - vmid = proxmox.get_nextvmid() - except Exception: - module.fail_json(msg="Can't get the next vmid for VM {0} automatically. Ensure your cluster state is good".format(name)) + existing_vmid = proxmox.get_vmid(name, ignore_missing=True) + if existing_vmid: + vmid = existing_vmid + else: + try: + vmid = proxmox.get_nextvmid() + except Exception: + module.fail_json(msg="Can't get the next vmid for VM {0} automatically. Ensure your cluster state is good".format(name)) else: clone_target = clone or name vmid = proxmox.get_vmid(clone_target, ignore_missing=True)