Skip to content

Commit

Permalink
modules/proxmox_kvm: initial support for online migrations (#6448)
Browse files Browse the repository at this point in the history
* modules/proxmox_kvm: initial support for online migrations

* modules/proxmox_kvm: add version_added and changelog fragment

* modules/proxmox_kvm: update migrate description

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>

* modules/proxmox_kvm: update failure message

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

* modules/proxmox_kvm: lowercase example boolean

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

---------

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
Co-authored-by: Felix Fontein <felix@fontein.de>
  • Loading branch information
3 people committed May 6, 2023
1 parent 9c11230 commit fe224a6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 2 additions & 0 deletions changelogs/fragments/6448-proxmox-kvm-migration-support.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- proxmox_kvm - adds ``migrate`` parameter to manage online migrations between hosts (https://github.com/ansible-collections/community.general/pull/6448)
41 changes: 40 additions & 1 deletion plugins/modules/proxmox_kvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,12 @@
- Memory size in MB for instance.
- This option has no default unless I(proxmox_default_behavior) is set to C(compatiblity); then the default is C(512).
type: int
migrate:
description:
- Migrate the VM to I(node) if it is on another node.
type: bool
default: false
version_added: 7.0.0
migrate_downtime:
description:
- Sets maximum tolerated downtime (in seconds) for migrations.
Expand Down Expand Up @@ -800,6 +806,15 @@
name: spynal
node: sabrewulf
revert: 'template,cpulimit'
- name: Migrate VM on second node
community.general.proxmox_kvm:
api_user: root@pam
api_password: secret
api_host: helldorado
name: spynal
node: sabrewulf-2
migrate: true
'''

RETURN = '''
Expand Down Expand Up @@ -1062,6 +1077,16 @@ def stop_vm(self, vm, force):
return False
return True

def migrate_vm(self, vm, target_node):
vmid = vm['vmid']
proxmox_node = self.proxmox_api.nodes(vm['node'])
taskid = proxmox_node.qemu(vmid).migrate.post(vmid=vmid, node=vm['node'], target=target_node, online=1)
if not self.wait_for_task(vm['node'], taskid):
self.module.fail_json(msg='Reached timeout while waiting for migrating VM. Last line in task before timeout: %s' %
proxmox_node.tasks(taskid).log.get()[:1])
return False
return True


def main():
module_args = proxmox_auth_argument_spec()
Expand Down Expand Up @@ -1109,6 +1134,7 @@ def main():
lock=dict(choices=['migrate', 'backup', 'snapshot', 'rollback']),
machine=dict(type='str'),
memory=dict(type='int'),
migrate=dict(type='bool', default=False),
migrate_downtime=dict(type='int'),
migrate_speed=dict(type='int'),
name=dict(type='str'),
Expand Down Expand Up @@ -1168,6 +1194,7 @@ def main():
cpu = module.params['cpu']
cores = module.params['cores']
delete = module.params['delete']
migrate = module.params['migrate']
memory = module.params['memory']
name = module.params['name']
newid = module.params['newid']
Expand Down Expand Up @@ -1209,7 +1236,7 @@ def main():
# If vmid is not defined then retrieve its value from the vm name,
# the cloned vm name or retrieve the next free VM id from ProxmoxAPI.
if not vmid:
if state == 'present' and not update and not clone and not delete and not revert:
if state == 'present' and not update and not clone and not delete and not revert and not migrate:
try:
vmid = proxmox.get_nextvmid()
except Exception:
Expand Down Expand Up @@ -1256,6 +1283,18 @@ def main():
except Exception as e:
module.fail_json(vmid=vmid, msg='Unable to revert settings on VM {0} with vmid {1}: Maybe is not a pending task... '.format(name, vmid) + str(e))

if migrate:
try:
vm = proxmox.get_vm(vmid)
vm_node = vm['node']
if node != vm_node:
proxmox.migrate_vm(vm, node)
module.exit_json(changed=True, vmid=vmid, msg="VM {0} has been migrated from {1} to {2}".format(vmid, vm_node, node))
else:
module.exit_json(changed=False, vmid=vmid, msg="VM {0} is already on {1}".format(vmid, node))
except Exception as e:
module.fail_json(vmid=vmid, msg='Unable to migrate VM {0} from {1} to {2}: {3}'.format(vmid, vm_node, node, e))

if state == 'present':
try:
if proxmox.get_vm(vmid, ignore_missing=True) and not (update or clone):
Expand Down

0 comments on commit fe224a6

Please sign in to comment.