From 43c1d9f66c3c2b4dec13371ee07ad39286f18b3d Mon Sep 17 00:00:00 2001 From: Sergei Antipov Date: Mon, 24 Jul 2023 02:27:35 -0400 Subject: [PATCH] [PR #6981/f9448574 backport][stable-6] [proxmox_kvm] Don't create VM if name is used without vmid (#7003) * [proxmox_kvm] Don't create VM if name is used without vmid (#6981) * [proxmox_kvm] Don't create VM if name is used without vmid * Add changelog and unit tests (cherry picked from commit f9448574bd43611d71932f0d88bc67585d528f2f) * Remove semantic markup --- ...-fix-vm-creation-when-only-name-provided.yml | 2 ++ plugins/modules/proxmox_kvm.py | 17 +++++++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) create mode 100644 changelogs/fragments/6981-proxmox-fix-vm-creation-when-only-name-provided.yml 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)