Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proxmox_kvm Allow vm hibernation #9653

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- proxmox_kvm - allow hibernation and suspending of VMs (https://github.com/ansible-collections/community.general/issues/9620, https://github.com/ansible-collections/community.general/pull/9653).
32 changes: 30 additions & 2 deletions plugins/modules/proxmox_kvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,9 @@
- Indicates desired state of the instance.
- If V(current), the current state of the VM will be fetched. You can access it with C(results.status).
- V(template) was added in community.general 8.1.0.
Copy link
Collaborator

@felixfontein felixfontein Jan 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- V(template) was added in community.general 8.1.0.
- V(template) was added in community.general 8.1.0.
- V(paused) and V(hibernated) were added in community.general 10.4.0.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed typo in Felix's comment: version is 10.4.0 not 11.4.0 ;-)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed in commit 739193e

- V(paused) and V(hibernated) were added in community.general 10.4.0.
type: str
choices: ['present', 'started', 'absent', 'stopped', 'restarted', 'current', 'template']
choices: ['present', 'started', 'absent', 'stopped', 'restarted', 'current', 'template', 'paused', 'hibernated']
default: present
storage:
description:
Expand Down Expand Up @@ -1208,6 +1209,16 @@ def migrate_vm(self, vm, target_node):
return False
return True

def suspend_vm(self, vm, timeout, todisk):
vmid = vm['vmid']
proxmox_node = self.proxmox_api.nodes(vm['node'])
taskid = proxmox_node.qemu(vmid).status.suspend.post(todisk=(1 if todisk else 0), timeout=timeout)
if not self.wait_for_task(vm['node'], taskid):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any option for the user so that they do not have to wait for the operation to complete? As in a a fire-and-forget kind of operation. Apparently not, just making sure.

If indeed it has no such option, I would suggest adding one (on a different PR).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that would be possible to implement, but the other states also doesn't have a "fire and forget". So Yeah I think that should be another PR implementing that for all possble states

self.module.fail_json(msg='Reached timeout while waiting for suspending VM. Last line in task before timeout: %s' %
proxmox_node.tasks(taskid).log.get()[:1])
return False
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is never executed - fail_json() ends up raising an exception, and rips through the call stack. Please remove.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which line do you mean exactly? I tried to stay in line with the existing code base

return True


def main():
module_args = proxmox_auth_argument_spec()
Expand Down Expand Up @@ -1287,7 +1298,7 @@ def main():
sshkeys=dict(type='str', no_log=False),
startdate=dict(type='str'),
startup=dict(),
state=dict(default='present', choices=['present', 'absent', 'stopped', 'started', 'restarted', 'current', 'template']),
state=dict(default='present', choices=['present', 'absent', 'stopped', 'started', 'restarted', 'current', 'template', 'paused', 'hibernated']),
storage=dict(type='str'),
tablet=dict(type='bool'),
tags=dict(type='list', elements='str'),
Expand Down Expand Up @@ -1611,6 +1622,23 @@ def main():
if status:
module.exit_json(changed=False, vmid=vmid, msg="VM %s with vmid = %s is %s" % (name, vmid, current), **status)

elif state in ['paused', 'hibernated']:
if not vmid:
module.fail_json(msg='VM with name = %s does not exist in cluster' % name)

status = {}
try:
vm = proxmox.get_vm(vmid)
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 not running" % vmid, **status)

proxmox.suspend_vm(vm, force=module.params['force'], timeout=module.params['timeout'], todisk=(state == 'hibernated'))
module.exit_json(changed=True, vmid=vmid, msg="VM %s is suspending" % vmid, **status)
except Exception as e:
module.fail_json(vmid=vmid, msg="suspending of VM %s failed with exception: %s" % (vmid, e), **status)


if __name__ == '__main__':
main()