From 28c7a6298958098c48a43f22d6d1fbc636d9e3ae Mon Sep 17 00:00:00 2001
From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com>
Date: Tue, 27 Jun 2023 07:07:27 +0200
Subject: [PATCH] [PR #6773/3571df83 backport][stable-7] Module `proxmox_kvm`
 `restarted` state bug fix (#6797)

Module `proxmox_kvm` `restarted` state bug fix (#6773)

* Change proxmox_kvm restart to use new method

Previously, the `restarted` state used both stop and start vm functions to restart a vm.
This change introduces the a new function that utilizes the proxmox reboot endpoint instead for a more reliable method of restarting a vm.

* Create 6773-proxmox_kvm-restarted-state-bug-fix.yaml

* Fix typo

* Add link to PR

* Chanel log fragment formatting changes

* Move try/catch to `restart_vm` function

* Update changelogs/fragments/6773-proxmox_kvm-restarted-state-bug-fix.yaml

Co-authored-by: Felix Fontein <felix@fontein.de>

---------

Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit 3571df837d7c7f2c0aa5d6b02618635b0e4454cb)

Co-authored-by: Juan Estupinan <65736594+juan210012@users.noreply.github.com>
---
 ...3-proxmox_kvm-restarted-state-bug-fix.yaml |  2 ++
 plugins/modules/proxmox_kvm.py                | 29 +++++++++++++------
 2 files changed, 22 insertions(+), 9 deletions(-)
 create mode 100644 changelogs/fragments/6773-proxmox_kvm-restarted-state-bug-fix.yaml

diff --git a/changelogs/fragments/6773-proxmox_kvm-restarted-state-bug-fix.yaml b/changelogs/fragments/6773-proxmox_kvm-restarted-state-bug-fix.yaml
new file mode 100644
index 0000000000..43988f97e7
--- /dev/null
+++ b/changelogs/fragments/6773-proxmox_kvm-restarted-state-bug-fix.yaml
@@ -0,0 +1,2 @@
+bugfixes:
+  - proxmox_kvm - ``restarted`` state did not actually restart a VM in some VM configurations. The state now uses the Proxmox reboot endpoint instead of calling the ``stop_vm`` and ``start_vm`` functions (https://github.com/ansible-collections/community.general/pull/6773).
diff --git a/plugins/modules/proxmox_kvm.py b/plugins/modules/proxmox_kvm.py
index a4258a682d..3750857a92 100644
--- a/plugins/modules/proxmox_kvm.py
+++ b/plugins/modules/proxmox_kvm.py
@@ -1099,6 +1099,20 @@ class ProxmoxKvmAnsible(ProxmoxAnsible):
             return False
         return True
 
+    def restart_vm(self, vm, **status):
+        vmid = vm['vmid']
+        try:
+            proxmox_node = self.proxmox_api.nodes(vm['node'])
+            taskid = proxmox_node.qemu(vmid).status.reboot.post()
+            if not self.wait_for_task(vm['node'], taskid):
+                self.module.fail_json(msg='Reached timeout while waiting for rebooting VM. Last line in task before timeout: %s' %
+                                          proxmox_node.tasks(taskid).log.get()[:1])
+                return False
+            return True
+        except Exception as e:
+            self.module.fail_json(vmid=vmid, msg="restarting of VM %s failed with exception: %s" % (vmid, e))
+            return False
+
     def migrate_vm(self, vm, target_node):
         vmid = vm['vmid']
         proxmox_node = self.proxmox_api.nodes(vm['node'])
@@ -1458,16 +1472,13 @@ def main():
             module.fail_json(msg='VM with name = %s does not exist in cluster' % name)
 
         status = {}
-        try:
-            vm = proxmox.get_vm(vmid)
-            status['status'] = vm['status']
-            if vm['status'] == 'stopped':
-                module.exit_json(changed=False, vmid=vmid, msg="VM %s is not running" % vmid, **status)
+        vm = proxmox.get_vm(vmid)
+        status['status'] = vm['status']
+        if vm['status'] == 'stopped':
+            module.exit_json(changed=False, vmid=vmid, msg="VM %s is not running" % vmid, **status)
 
-            if proxmox.stop_vm(vm, force=module.params['force']) and proxmox.start_vm(vm):
-                module.exit_json(changed=True, vmid=vmid, msg="VM %s is restarted" % vmid, **status)
-        except Exception as e:
-            module.fail_json(vmid=vmid, msg="restarting of VM %s failed with exception: %s" % (vmid, e), **status)
+        if proxmox.restart_vm(vm):
+            module.exit_json(changed=True, vmid=vmid, msg="VM %s is restarted" % vmid, **status)
 
     elif state == 'absent':
         status = {}