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 --->
cc @krauthosting
[click here for bot help](https://github.com/ansible/ansibullbot/blob/devel/ISSUE_HELP.md)
<!--- boilerplate: notify --->
@nxet LGTM! In the future, it might be a good idea to make a helper method for this in module_utils, so that other modules can reuse it.
@felixfontein Please merge :hugs: 
@krauthosting I cannot merge an issue :) Please approve the corresponding PR.
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 --->
cc @krauthosting
[click here for bot help](https://github.com/ansible/ansibullbot/blob/devel/ISSUE_HELP.md)
<!--- boilerplate: notify --->
@nxet LGTM! In the future, it might be a good idea to make a helper method for this in module_utils, so that other modules can reuse it.
@felixfontein Please merge :hugs: 
@krauthosting I cannot merge an issue :) Please approve the corresponding PR.

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
@@ -1466,8 +1466,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):
@@ -1482,9 +1483,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)
 
             if proxmox.stop_vm(vm, force=module.params['force'], timeout=module.params['timeout']):
@@ -1498,8 +1499,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']):
@@ -1516,8 +1518,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: