Repository: ansible-collections/community.general

Issue:
proxmox_kvm: Wrong vmid returned if VM already exists
### Summary

Running a task with `state: present` against a VM that already exists, returns the `vmid` but incremented by one.

I think this line looks suspicious: https://github.com/ansible-collections/community.general/blob/main/plugins/modules/cloud/misc/proxmox_kvm.py#L1176

### Issue Type

Bug Report

### Component Name

proxmox_kvm.py

### Ansible Version

ansible 2.9.15
  config file = /home/user/src/ansible/ansible.cfg
  configured module search path = ['/home/user/src/ansible/library']
  ansible python module location = /home/user/.local/lib/python3.7/site-packages/ansible
  executable location = /home/user/.local/bin/ansible
  python version = 3.7.3 (default, Jan 22 2021, 20:04:44) [GCC 8.3.0]


### Configuration

ANSIBLE_PIPELINING(/home/user/src/ansible.cfg) = True
CACHE_PLUGIN(/home/user/src/ansible.cfg) = jsonfile
CACHE_PLUGIN_CONNECTION(/home/user/src/ansible.cfg) = /tmp/ansible_facts
CACHE_PLUGIN_TIMEOUT(/home/user/src/ansible.cfg) = 300
DEFAULT_FORKS(/home/user/src/ansible.cfg) = 50
DEFAULT_INVENTORY_PLUGIN_PATH(/home/user/src/ansible.cfg) = ['/usr/share/ansible/plugins/inventory', '/home/user/src/plugins/inventory']
DEFAULT_LOG_PATH(/home/user/src/ansible.cfg) = /home/user/src/ansible.log
DEFAULT_MODULE_PATH(/home/user/src/ansible.cfg) = ['/home/user/src/library']
DEFAULT_NO_TARGET_SYSLOG(/home/user/src/ansible.cfg) = True
DEFAULT_SCP_IF_SSH(/home/user/src/ansible.cfg) = True
DEFAULT_STRATEGY(/home/user/src/ansible.cfg) = linear
INTERPRETER_PYTHON(/home/user/src/ansible.cfg) = auto
INVENTORY_ANY_UNPARSED_IS_FAILED(/home/user/src/ansible.cfg) = True


### OS / Environment

Debian 10

### Steps to Reproduce

```
- name: Ensure VM exists
  community.general.proxmox_kvm:
   name: my-vm
   state: present
  register: output
  
- debug: var=output.vmid

- name: Ensure VM exists again
  community.general.proxmox_kvm:
   name: my-vm
   state: present
  register: output2

- debug: var=output2.vmid
```

### Expected Results

```
TASK [Ensure VM exists] 
changed: [my-vm -> my-proxmox]

TASK [debug] 
ok: [my-vm -> my-proxmox] => {
    "stboot_vm.vmid": "101"
}

TASK [Ensure VM exists again] 
ok: [my-vm -> my-proxmox]

TASK [debug] 
ok: [my-vm -> my-proxmox] => {
    "stboot_vm2.vmid": "101"
}
```

### Actual Results

```
TASK [Ensure VM exists] 
changed: [my-vm -> my-proxmox]

TASK [debug] 
ok: [my-vm -> my-proxmox] => {
    "stboot_vm.vmid": "101"
}

TASK [Ensure VM exists again] 
ok: [my-vm -> my-proxmox]

TASK [debug] 
ok: [my-vm -> my-proxmox] => {
    "stboot_vm2.vmid": "102"
}
```

### Code of Conduct

- [X] I agree to follow the Ansible Code of Conduct


Discussion:
Files identified in the description:
* [`plugins/modules/cloud/misc/proxmox_kvm.py`](https://github.com/ansible-collections/community.general/blob/master/plugins/modules/cloud/misc/proxmox_kvm.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @Ajpantuso @Thulium-Drake @helldorado @joshainglis @karmab @tleguern
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
The issue is the second case below.
```python
if get_vm(proxmox, vmid) and not (update or clone): # First condition is false since next vm does not exist yet
     module.exit_json(changed=False, vmid=vmid, msg="VM with vmid <%s> already exists" % vmid)
elif get_vmid(proxmox, name) and not (update or clone): # vmid is returned, but not updated
     module.exit_json(changed=False, vmid=vmid, msg="VM with name <%s> already exists" % name)
     
     # Should be
     module.exit_json(changed=False, vmid=get_vmid(proxmox, name), msg="VM with name <%s> already exists" % name)
```
@robert-mullvad @Ajpantuso Are you interested in creating a PR fixing this?
> @robert-mullvad @Ajpantuso Are you interested in creating a PR fixing this?

See #2648, just wanted to capture some before/after evidence first.
#2648 fixes the problem in my case. Thank you @Ajpantuso!

Resolving patch:
diff --git a/plugins/modules/cloud/misc/proxmox_kvm.py b/plugins/modules/cloud/misc/proxmox_kvm.py
--- a/plugins/modules/cloud/misc/proxmox_kvm.py
+++ b/plugins/modules/cloud/misc/proxmox_kvm.py
@@ -1225,7 +1225,7 @@ def main():
             if get_vm(proxmox, vmid) and not (update or clone):
                 module.exit_json(changed=False, vmid=vmid, msg="VM with vmid <%s> already exists" % vmid)
             elif get_vmid(proxmox, name) and not (update or clone):
-                module.exit_json(changed=False, vmid=vmid, msg="VM with name <%s> already exists" % name)
+                module.exit_json(changed=False, vmid=get_vmid(proxmox, name)[0], msg="VM with name <%s> already exists" % name)
             elif not (node, name):
                 module.fail_json(msg='node, name is mandatory for creating/updating vm')
             elif not node_check(proxmox, node):


Test patch: