mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
[PR #6570/b133aa40 backport][stable-7] proxmox_kvm | Expose timeout param to stopped state (#6599)
proxmox_kvm | Expose timeout param to stopped state (#6570)
* Expose timeout param to stopped state
Forcefully stop virtual machine using timeout param for proxmox vm
shutdown api call.
* Add changelog fragment
* Typo fix in timeout param description
* Update changelogs/fragments/6570-handle-shutdown-timeout.yaml
Co-authored-by: Felix Fontein <felix@fontein.de>
* Update plugins/modules/proxmox_kvm.py
Co-authored-by: Felix Fontein <felix@fontein.de>
* Revert back exception message
---------
Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit b133aa40c6
)
Co-authored-by: Sergei Antipov <s.antipov@mulesoft.com>
This commit is contained in:
parent
307a291b57
commit
07f854fff1
2 changed files with 12 additions and 5 deletions
3
changelogs/fragments/6570-handle-shutdown-timeout.yaml
Normal file
3
changelogs/fragments/6570-handle-shutdown-timeout.yaml
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
---
|
||||||
|
minor_changes:
|
||||||
|
- proxmox_kvm - re-use ``timeout`` module param to forcefully shutdown a virtual machine when ``state`` is ``stopped`` (https://github.com/ansible-collections/community.general/issues/6257).
|
|
@ -444,7 +444,7 @@ options:
|
||||||
- Indicates desired state of the instance.
|
- Indicates desired state of the instance.
|
||||||
- If C(current), the current state of the VM will be fetched. You can access it with C(results.status)
|
- If C(current), the current state of the VM will be fetched. You can access it with C(results.status)
|
||||||
type: str
|
type: str
|
||||||
choices: ['present', 'started', 'absent', 'stopped', 'restarted','current']
|
choices: ['present', 'started', 'absent', 'stopped', 'restarted', 'current']
|
||||||
default: present
|
default: present
|
||||||
storage:
|
storage:
|
||||||
description:
|
description:
|
||||||
|
@ -480,6 +480,7 @@ options:
|
||||||
timeout:
|
timeout:
|
||||||
description:
|
description:
|
||||||
- Timeout for operations.
|
- Timeout for operations.
|
||||||
|
- When used with I(state=stopped) the option sets a graceful timeout for VM stop after which a VM will be forcefully stopped.
|
||||||
type: int
|
type: int
|
||||||
default: 30
|
default: 30
|
||||||
tpmstate0:
|
tpmstate0:
|
||||||
|
@ -907,6 +908,9 @@ class ProxmoxKvmAnsible(ProxmoxAnsible):
|
||||||
|
|
||||||
def wait_for_task(self, node, taskid):
|
def wait_for_task(self, node, taskid):
|
||||||
timeout = self.module.params['timeout']
|
timeout = self.module.params['timeout']
|
||||||
|
if self.module.params['state'] == 'stopped':
|
||||||
|
# Increase task timeout in case of stopped state to be sure it waits longer than VM stop operation itself
|
||||||
|
timeout += 10
|
||||||
|
|
||||||
while timeout:
|
while timeout:
|
||||||
if self.api_task_ok(node, taskid):
|
if self.api_task_ok(node, taskid):
|
||||||
|
@ -1085,10 +1089,10 @@ class ProxmoxKvmAnsible(ProxmoxAnsible):
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def stop_vm(self, vm, force):
|
def stop_vm(self, vm, force, timeout):
|
||||||
vmid = vm['vmid']
|
vmid = vm['vmid']
|
||||||
proxmox_node = self.proxmox_api.nodes(vm['node'])
|
proxmox_node = self.proxmox_api.nodes(vm['node'])
|
||||||
taskid = proxmox_node.qemu(vmid).status.shutdown.post(forceStop=(1 if force else 0))
|
taskid = proxmox_node.qemu(vmid).status.shutdown.post(forceStop=(1 if force else 0), timeout=timeout)
|
||||||
if not self.wait_for_task(vm['node'], taskid):
|
if not self.wait_for_task(vm['node'], taskid):
|
||||||
self.module.fail_json(msg='Reached timeout while waiting for stopping VM. Last line in task before timeout: %s' %
|
self.module.fail_json(msg='Reached timeout while waiting for stopping VM. Last line in task before timeout: %s' %
|
||||||
proxmox_node.tasks(taskid).log.get()[:1])
|
proxmox_node.tasks(taskid).log.get()[:1])
|
||||||
|
@ -1325,7 +1329,7 @@ def main():
|
||||||
elif proxmox.get_vmid(name, ignore_missing=True) and not (update or clone):
|
elif proxmox.get_vmid(name, ignore_missing=True) and not (update or clone):
|
||||||
module.exit_json(changed=False, vmid=proxmox.get_vmid(name), msg="VM with name <%s> already exists" % name)
|
module.exit_json(changed=False, vmid=proxmox.get_vmid(name), msg="VM with name <%s> already exists" % name)
|
||||||
elif not node:
|
elif not node:
|
||||||
module.fail.json(msg='node is mandatory for creating/updating VM')
|
module.fail_json(msg='node is mandatory for creating/updating VM')
|
||||||
elif update and not any([vmid, name]):
|
elif update and not any([vmid, name]):
|
||||||
module.fail_json(msg='vmid or name is mandatory for updating VM')
|
module.fail_json(msg='vmid or name is mandatory for updating VM')
|
||||||
elif not proxmox.get_node(node):
|
elif not proxmox.get_node(node):
|
||||||
|
@ -1442,7 +1446,7 @@ def main():
|
||||||
if vm['status'] == 'stopped':
|
if vm['status'] == 'stopped':
|
||||||
module.exit_json(changed=False, vmid=vmid, msg="VM %s is already stopped" % vmid, **status)
|
module.exit_json(changed=False, vmid=vmid, msg="VM %s is already stopped" % vmid, **status)
|
||||||
|
|
||||||
if proxmox.stop_vm(vm, force=module.params['force']):
|
if proxmox.stop_vm(vm, force=module.params['force'], timeout=module.params['timeout']):
|
||||||
module.exit_json(changed=True, vmid=vmid, msg="VM %s is shutting down" % vmid, **status)
|
module.exit_json(changed=True, vmid=vmid, msg="VM %s is shutting down" % vmid, **status)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
module.fail_json(vmid=vmid, msg="stopping of VM %s failed with exception: %s" % (vmid, e), **status)
|
module.fail_json(vmid=vmid, msg="stopping of VM %s failed with exception: %s" % (vmid, e), **status)
|
||||||
|
|
Loading…
Reference in a new issue