Repository: ansible-collections/community.general

Issue:
Proxmox_kvm status check wrong info
### Summary

When proxmox_kvm want to change the state, it's check the vm current status:

        vm = proxmox.get_vm(vmid)
        status['status'] = vm['status']
        if vm['status'] == 'running':
            module.exit_json(changed=False, vmid=vmid, msg="VM %s is already running" % vmid, **status)

the get_vm() func use cluster/resources api call, but that is sometimes not fresh status (laggy), confirmed by proxmox dev: https://forum.proxmox.com/threads/inconsistent-vm-status.139390/post-622623

The check sould use the right api call:

        current = proxmox.proxmox_api.nodes(vm['node']).qemu(vmid).status.current.get()['status']
        status['status'] = current
        if current == 'running':
            module.exit_json(changed=False, vmid=vmid, msg="VM %s is already running" % vmid, **status)

### Issue Type

Bug Report

### Component Name

proxmox_kvm

### Ansible Version

2.14.2


### Community.general Version

8.2.0

### Configuration

_No response_

### OS / Environment

Ubuntu Jammy

### Steps to Reproduce


    - name: Stop VM
      community.general.proxmox_kvm:
        api_user: root@pam
        api_password: secret
        api_host: helldorado
        name: spynal
        node: sabrewulf
        state: stopped

    - name: Reconfigure VM (remove ide2, etc)

    - name: Start VM
      community.general.proxmox_kvm:
        api_user: root@pam
        api_password: secret
        api_host: helldorado
        name: spynal
        node: sabrewulf
        state: started

### Expected Results

VM started again

### Actual Results

error with: "VM 42 is already running"

### Code of Conduct

- [X] I agree to follow the Ansible Code of Conduct
Proxmox_kvm status check wrong info
### Summary

When proxmox_kvm want to change the state, it's check the vm current status:

        vm = proxmox.get_vm(vmid)
        status['status'] = vm['status']
        if vm['status'] == 'running':
            module.exit_json(changed=False, vmid=vmid, msg="VM %s is already running" % vmid, **status)

the get_vm() func use cluster/resources api call, but that is sometimes not fresh status (laggy), confirmed by proxmox dev: https://forum.proxmox.com/threads/inconsistent-vm-status.139390/post-622623

The check sould use the right api call:

        current = proxmox.proxmox_api.nodes(vm['node']).qemu(vmid).status.current.get()['status']
        status['status'] = current
        if current == 'running':
            module.exit_json(changed=False, vmid=vmid, msg="VM %s is already running" % vmid, **status)

### Issue Type

Bug Report

### Component Name

proxmox_kvm

### Ansible Version

2.14.2


### Community.general Version

8.2.0

### Configuration

_No response_

### OS / Environment

Ubuntu Jammy

### Steps to Reproduce


    - name: Stop VM
      community.general.proxmox_kvm:
        api_user: root@pam
        api_password: secret
        api_host: helldorado
        name: spynal
        node: sabrewulf
        state: stopped

    - name: Reconfigure VM (remove ide2, etc)

    - name: Start VM
      community.general.proxmox_kvm:
        api_user: root@pam
        api_password: secret
        api_host: helldorado
        name: spynal
        node: sabrewulf
        state: started

### Expected Results

VM started again

### Actual Results

error with: "VM 42 is already running"

### Code of Conduct

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


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

If these files are incorrect, 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/devel/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @Ajpantuso @Thulium-Drake @UnderGreen @helldorado @joshainglis @karmab
[click here for bot help](https://github.com/ansible/ansibullbot/blob/devel/ISSUE_HELP.md)
<!--- boilerplate: notify --->
Files identified in the description:
* [`plugins/modules/proxmox_kvm.py`](https://github.com/ansible-collections/community.general/blob/main/plugins/modules/proxmox_kvm.py)

If these files are incorrect, 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/devel/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @Ajpantuso @Thulium-Drake @UnderGreen @helldorado @joshainglis @karmab
[click here for bot help](https://github.com/ansible/ansibullbot/blob/devel/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/plugins/modules/proxmox_kvm.py b/plugins/modules/proxmox_kvm.py
--- a/plugins/modules/proxmox_kvm.py
+++ b/plugins/modules/proxmox_kvm.py
@@ -1522,8 +1522,9 @@ def main():
         status = {}
         try:
             vm = proxmox.get_vm(vmid)
-            status['status'] = vm['status']
-            if vm['status'] == 'running':
+            current = proxmox.proxmox_api.nodes(vm['node']).qemu(vmid).status.current.get()['status']
+            status['status'] = current
+            if current == 'running':
                 module.exit_json(changed=False, vmid=vmid, msg="VM %s is already running" % vmid, **status)
 
             if proxmox.start_vm(vm):
@@ -1538,9 +1539,9 @@ def main():
         status = {}
         try:
             vm = proxmox.get_vm(vmid)
-
-            status['status'] = vm['status']
-            if vm['status'] == 'stopped':
+            current = proxmox.proxmox_api.nodes(vm['node']).qemu(vmid).status.current.get()['status']
+            status['status'] = current
+            if current == 'stopped':
                 module.exit_json(changed=False, vmid=vmid, msg="VM %s is already stopped" % vmid, **status)
 
             proxmox.stop_vm(vm, force=module.params['force'], timeout=module.params['timeout'])
@@ -1570,8 +1571,9 @@ def main():
 
         status = {}
         vm = proxmox.get_vm(vmid)
-        status['status'] = vm['status']
-        if vm['status'] == 'stopped':
+        current = proxmox.proxmox_api.nodes(vm['node']).qemu(vmid).status.current.get()['status']
+        status['status'] = current
+        if current == 'stopped':
             module.exit_json(changed=False, vmid=vmid, msg="VM %s is not running" % vmid, **status)
 
         if proxmox.restart_vm(vm, force=module.params['force']):
@@ -1588,8 +1590,9 @@ def main():
                 module.exit_json(changed=False, vmid=vmid)
 
             proxmox_node = proxmox.proxmox_api.nodes(vm['node'])
-            status['status'] = vm['status']
-            if vm['status'] == 'running':
+            current = proxmox_node.qemu(vmid).status.current.get()['status']
+            status['status'] = current
+            if current == 'running':
                 if module.params['force']:
                     proxmox.stop_vm(vm, True, timeout=module.params['timeout'])
                 else:


Test patch: