mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
[proxmox] return vmid and taskid (#7263)
* add vmid exit value if create succeeds, we want the vmid * fix syntax * add vmid to return codes * Add taskid to return, and only return vmid when it makes sense to * add changelog fragment with temporary filename * Add pr number to fragment * fix PEP8 E501: line too long * oops, I knew I still missed something... * Update 7263-proxmox-return-vmid-and-taskid.yaml Co-authored-by: Felix Fontein <felix@fontein.de> * removed taskid from places it's not defined * finally fixed sanity test errors Okay, so maybe just waiting for CI to run the tests was a terrible idea. I installed `inotifywait`, set up a venv for the tests, and in my editor's terminal pane I ran the following, letting it run every save until it exited: until ansible-test sanity proxmox ; do inotifywait --event modify plugins/modules/proxmox.py done --------- Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
parent
f4237dde1b
commit
28b3da88a9
2 changed files with 38 additions and 36 deletions
|
@ -0,0 +1,2 @@
|
||||||
|
minor_changes:
|
||||||
|
- proxmox - add ``vmid`` (and ``taskid`` when possible) to return values (https://github.com/ansible-collections/community.general/pull/7263).
|
|
@ -546,7 +546,7 @@ class ProxmoxLxcAnsible(ProxmoxAnsible):
|
||||||
return True
|
return True
|
||||||
timeout -= 1
|
timeout -= 1
|
||||||
if timeout == 0:
|
if timeout == 0:
|
||||||
self.module.fail_json(msg='Reached timeout while waiting for creating VM. Last line in task before timeout: %s' %
|
self.module.fail_json(vmid=vmid, node=node, msg='Reached timeout while waiting for creating VM. Last line in task before timeout: %s' %
|
||||||
proxmox_node.tasks(taskid).log.get()[:1])
|
proxmox_node.tasks(taskid).log.get()[:1])
|
||||||
|
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
@ -559,7 +559,7 @@ class ProxmoxLxcAnsible(ProxmoxAnsible):
|
||||||
return True
|
return True
|
||||||
timeout -= 1
|
timeout -= 1
|
||||||
if timeout == 0:
|
if timeout == 0:
|
||||||
self.module.fail_json(msg='Reached timeout while waiting for starting VM. Last line in task before timeout: %s' %
|
self.module.fail_json(vmid=vmid, taskid=taskid, msg='Reached timeout while waiting for starting VM. Last line in task before timeout: %s' %
|
||||||
self.proxmox_api.nodes(vm['node']).tasks(taskid).log.get()[:1])
|
self.proxmox_api.nodes(vm['node']).tasks(taskid).log.get()[:1])
|
||||||
|
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
@ -575,7 +575,7 @@ class ProxmoxLxcAnsible(ProxmoxAnsible):
|
||||||
return True
|
return True
|
||||||
timeout -= 1
|
timeout -= 1
|
||||||
if timeout == 0:
|
if timeout == 0:
|
||||||
self.module.fail_json(msg='Reached timeout while waiting for stopping VM. Last line in task before timeout: %s' %
|
self.module.fail_json(vmid=vmid, taskid=taskid, msg='Reached timeout while waiting for stopping VM. Last line in task before timeout: %s' %
|
||||||
self.proxmox_api.nodes(vm['node']).tasks(taskid).log.get()[:1])
|
self.proxmox_api.nodes(vm['node']).tasks(taskid).log.get()[:1])
|
||||||
|
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
@ -588,7 +588,7 @@ class ProxmoxLxcAnsible(ProxmoxAnsible):
|
||||||
return True
|
return True
|
||||||
timeout -= 1
|
timeout -= 1
|
||||||
if timeout == 0:
|
if timeout == 0:
|
||||||
self.module.fail_json(msg='Reached timeout while waiting for unmounting VM. Last line in task before timeout: %s' %
|
self.module.fail_json(vmid=vmid, taskid=taskid, msg='Reached timeout while waiting for unmounting VM. Last line in task before timeout: %s' %
|
||||||
self.proxmox_api.nodes(vm['node']).tasks(taskid).log.get()[:1])
|
self.proxmox_api.nodes(vm['node']).tasks(taskid).log.get()[:1])
|
||||||
|
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
@ -695,20 +695,20 @@ def main():
|
||||||
if state == 'present' and clone is None:
|
if state == 'present' and clone is None:
|
||||||
try:
|
try:
|
||||||
if proxmox.get_vm(vmid, ignore_missing=True) and not module.params['force']:
|
if proxmox.get_vm(vmid, ignore_missing=True) and not module.params['force']:
|
||||||
module.exit_json(changed=False, msg="VM with vmid = %s is already exists" % vmid)
|
module.exit_json(changed=False, vmid=vmid, msg="VM with vmid = %s is already exists" % vmid)
|
||||||
# If no vmid was passed, there cannot be another VM named 'hostname'
|
# If no vmid was passed, there cannot be another VM named 'hostname'
|
||||||
if (not module.params['vmid'] and
|
if (not module.params['vmid'] and
|
||||||
proxmox.get_vmid(hostname, ignore_missing=True) and
|
proxmox.get_vmid(hostname, ignore_missing=True) and
|
||||||
not module.params['force']):
|
not module.params['force']):
|
||||||
vmid = proxmox.get_vmid(hostname)
|
vmid = proxmox.get_vmid(hostname)
|
||||||
module.exit_json(changed=False, msg="VM with hostname %s already exists and has ID number %s" % (hostname, vmid))
|
module.exit_json(changed=False, vmid=vmid, msg="VM with hostname %s already exists and has ID number %s" % (hostname, vmid))
|
||||||
elif not proxmox.get_node(node):
|
elif not proxmox.get_node(node):
|
||||||
module.fail_json(msg="node '%s' not exists in cluster" % node)
|
module.fail_json(vmid=vmid, msg="node '%s' not exists in cluster" % node)
|
||||||
elif not proxmox.content_check(node, module.params['ostemplate'], template_store):
|
elif not proxmox.content_check(node, module.params['ostemplate'], template_store):
|
||||||
module.fail_json(msg="ostemplate '%s' not exists on node %s and storage %s"
|
module.fail_json(vmid=vmid, msg="ostemplate '%s' not exists on node %s and storage %s"
|
||||||
% (module.params['ostemplate'], node, template_store))
|
% (module.params['ostemplate'], node, template_store))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
module.fail_json(msg="Pre-creation checks of {VZ_TYPE} VM {vmid} failed with exception: {e}".format(VZ_TYPE=VZ_TYPE, vmid=vmid, e=e))
|
module.fail_json(vmid=vmid, msg="Pre-creation checks of {VZ_TYPE} VM {vmid} failed with exception: {e}".format(VZ_TYPE=VZ_TYPE, vmid=vmid, e=e))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
proxmox.create_instance(vmid, node, disk, storage, cpus, memory, swap, timeout, clone,
|
proxmox.create_instance(vmid, node, disk, storage, cpus, memory, swap, timeout, clone,
|
||||||
|
@ -733,43 +733,43 @@ def main():
|
||||||
timezone=module.params['timezone'],
|
timezone=module.params['timezone'],
|
||||||
tags=module.params['tags'])
|
tags=module.params['tags'])
|
||||||
|
|
||||||
module.exit_json(changed=True, msg="Deployed VM %s from template %s" % (vmid, module.params['ostemplate']))
|
module.exit_json(changed=True, vmid=vmid, msg="Deployed VM %s from template %s" % (vmid, module.params['ostemplate']))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
module.fail_json(msg="Creation of %s VM %s failed with exception: %s" % (VZ_TYPE, vmid, e))
|
module.fail_json(vmid=vmid, msg="Creation of %s VM %s failed with exception: %s" % (VZ_TYPE, vmid, e))
|
||||||
|
|
||||||
# Clone a container
|
# Clone a container
|
||||||
elif state == 'present' and clone is not None:
|
elif state == 'present' and clone is not None:
|
||||||
try:
|
try:
|
||||||
if proxmox.get_vm(vmid, ignore_missing=True) and not module.params['force']:
|
if proxmox.get_vm(vmid, ignore_missing=True) and not module.params['force']:
|
||||||
module.exit_json(changed=False, msg="VM with vmid = %s is already exists" % vmid)
|
module.exit_json(changed=False, vmid=vmid, msg="VM with vmid = %s is already exists" % vmid)
|
||||||
# If no vmid was passed, there cannot be another VM named 'hostname'
|
# If no vmid was passed, there cannot be another VM named 'hostname'
|
||||||
if (not module.params['vmid'] and
|
if (not module.params['vmid'] and
|
||||||
proxmox.get_vmid(hostname, ignore_missing=True) and
|
proxmox.get_vmid(hostname, ignore_missing=True) and
|
||||||
not module.params['force']):
|
not module.params['force']):
|
||||||
vmid = proxmox.get_vmid(hostname)
|
vmid = proxmox.get_vmid(hostname)
|
||||||
module.exit_json(changed=False, msg="VM with hostname %s already exists and has ID number %s" % (hostname, vmid))
|
module.exit_json(changed=False, vmid=vmid, msg="VM with hostname %s already exists and has ID number %s" % (hostname, vmid))
|
||||||
if not proxmox.get_vm(clone, ignore_missing=True):
|
if not proxmox.get_vm(clone, ignore_missing=True):
|
||||||
module.exit_json(changed=False, msg="Container to be cloned does not exist")
|
module.exit_json(changed=False, vmid=vmid, msg="Container to be cloned does not exist")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
module.fail_json(msg="Pre-clone checks of {VZ_TYPE} VM {vmid} failed with exception: {e}".format(VZ_TYPE=VZ_TYPE, vmid=vmid, e=e))
|
module.fail_json(vmid=vmid, msg="Pre-clone checks of {VZ_TYPE} VM {vmid} failed with exception: {e}".format(VZ_TYPE=VZ_TYPE, vmid=vmid, e=e))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
proxmox.create_instance(vmid, node, disk, storage, cpus, memory, swap, timeout, clone)
|
proxmox.create_instance(vmid, node, disk, storage, cpus, memory, swap, timeout, clone)
|
||||||
|
|
||||||
module.exit_json(changed=True, msg="Cloned VM %s from %s" % (vmid, clone))
|
module.exit_json(changed=True, vmid=vmid, msg="Cloned VM %s from %s" % (vmid, clone))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
module.fail_json(msg="Cloning %s VM %s failed with exception: %s" % (VZ_TYPE, vmid, e))
|
module.fail_json(vmid=vmid, msg="Cloning %s VM %s failed with exception: %s" % (VZ_TYPE, vmid, e))
|
||||||
|
|
||||||
elif state == 'started':
|
elif state == 'started':
|
||||||
try:
|
try:
|
||||||
vm = proxmox.get_vm(vmid)
|
vm = proxmox.get_vm(vmid)
|
||||||
if getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'running':
|
if getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'running':
|
||||||
module.exit_json(changed=False, msg="VM %s is already running" % vmid)
|
module.exit_json(changed=False, vmid=vmid, msg="VM %s is already running" % vmid)
|
||||||
|
|
||||||
if proxmox.start_instance(vm, vmid, timeout):
|
if proxmox.start_instance(vm, vmid, timeout):
|
||||||
module.exit_json(changed=True, msg="VM %s started" % vmid)
|
module.exit_json(changed=True, vmid=vmid, msg="VM %s started" % vmid)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
module.fail_json(msg="starting of VM %s failed with exception: %s" % (vmid, e))
|
module.fail_json(vmid=vmid, msg="starting of VM %s failed with exception: %s" % (vmid, e))
|
||||||
|
|
||||||
elif state == 'stopped':
|
elif state == 'stopped':
|
||||||
try:
|
try:
|
||||||
|
@ -778,18 +778,18 @@ def main():
|
||||||
if getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'mounted':
|
if getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'mounted':
|
||||||
if module.params['force']:
|
if module.params['force']:
|
||||||
if proxmox.umount_instance(vm, vmid, timeout):
|
if proxmox.umount_instance(vm, vmid, timeout):
|
||||||
module.exit_json(changed=True, msg="VM %s is shutting down" % vmid)
|
module.exit_json(changed=True, vmid=vmid, msg="VM %s is shutting down" % vmid)
|
||||||
else:
|
else:
|
||||||
module.exit_json(changed=False, msg=("VM %s is already shutdown, but mounted. "
|
module.exit_json(changed=False, vmid=vmid,
|
||||||
"You can use force option to umount it.") % vmid)
|
msg=("VM %s is already shutdown, but mounted. You can use force option to umount it.") % vmid)
|
||||||
|
|
||||||
if getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'stopped':
|
if getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status'] == 'stopped':
|
||||||
module.exit_json(changed=False, msg="VM %s is already shutdown" % vmid)
|
module.exit_json(changed=False, vmid=vmid, msg="VM %s is already shutdown" % vmid)
|
||||||
|
|
||||||
if proxmox.stop_instance(vm, vmid, timeout, force=module.params['force']):
|
if proxmox.stop_instance(vm, vmid, timeout, force=module.params['force']):
|
||||||
module.exit_json(changed=True, msg="VM %s is shutting down" % vmid)
|
module.exit_json(changed=True, vmid=vmid, msg="VM %s is shutting down" % vmid)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
module.fail_json(msg="stopping of VM %s failed with exception: %s" % (vmid, e))
|
module.fail_json(vmid=vmid, msg="stopping of VM %s failed with exception: %s" % (vmid, e))
|
||||||
|
|
||||||
elif state == 'restarted':
|
elif state == 'restarted':
|
||||||
try:
|
try:
|
||||||
|
@ -797,28 +797,28 @@ def main():
|
||||||
|
|
||||||
vm_status = getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status']
|
vm_status = getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status']
|
||||||
if vm_status in ['stopped', 'mounted']:
|
if vm_status in ['stopped', 'mounted']:
|
||||||
module.exit_json(changed=False, msg="VM %s is not running" % vmid)
|
module.exit_json(changed=False, vmid=vmid, msg="VM %s is not running" % vmid)
|
||||||
|
|
||||||
if (proxmox.stop_instance(vm, vmid, timeout, force=module.params['force']) and
|
if (proxmox.stop_instance(vm, vmid, timeout, force=module.params['force']) and
|
||||||
proxmox.start_instance(vm, vmid, timeout)):
|
proxmox.start_instance(vm, vmid, timeout)):
|
||||||
module.exit_json(changed=True, msg="VM %s is restarted" % vmid)
|
module.exit_json(changed=True, vmid=vmid, msg="VM %s is restarted" % vmid)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
module.fail_json(msg="restarting of VM %s failed with exception: %s" % (vmid, e))
|
module.fail_json(vmid=vmid, msg="restarting of VM %s failed with exception: %s" % (vmid, e))
|
||||||
|
|
||||||
elif state == 'absent':
|
elif state == 'absent':
|
||||||
if not vmid:
|
if not vmid:
|
||||||
module.exit_json(changed=False, msg='VM with hostname = %s is already absent' % hostname)
|
module.exit_json(changed=False, vmid=vmid, msg='VM with hostname = %s is already absent' % hostname)
|
||||||
try:
|
try:
|
||||||
vm = proxmox.get_vm(vmid, ignore_missing=True)
|
vm = proxmox.get_vm(vmid, ignore_missing=True)
|
||||||
if not vm:
|
if not vm:
|
||||||
module.exit_json(changed=False, msg="VM %s does not exist" % vmid)
|
module.exit_json(changed=False, vmid=vmid, msg="VM %s does not exist" % vmid)
|
||||||
|
|
||||||
vm_status = getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status']
|
vm_status = getattr(proxmox.proxmox_api.nodes(vm['node']), VZ_TYPE)(vmid).status.current.get()['status']
|
||||||
if vm_status == 'running':
|
if vm_status == 'running':
|
||||||
module.exit_json(changed=False, msg="VM %s is running. Stop it before deletion." % vmid)
|
module.exit_json(changed=False, vmid=vmid, msg="VM %s is running. Stop it before deletion." % vmid)
|
||||||
|
|
||||||
if vm_status == 'mounted':
|
if vm_status == 'mounted':
|
||||||
module.exit_json(changed=False, msg="VM %s is mounted. Stop it with force option before deletion." % vmid)
|
module.exit_json(changed=False, vmid=vmid, msg="VM %s is mounted. Stop it with force option before deletion." % vmid)
|
||||||
|
|
||||||
delete_params = {}
|
delete_params = {}
|
||||||
|
|
||||||
|
@ -829,15 +829,15 @@ def main():
|
||||||
|
|
||||||
while timeout:
|
while timeout:
|
||||||
if proxmox.api_task_ok(vm['node'], taskid):
|
if proxmox.api_task_ok(vm['node'], taskid):
|
||||||
module.exit_json(changed=True, msg="VM %s removed" % vmid)
|
module.exit_json(changed=True, vmid=vmid, taskid=taskid, msg="VM %s removed" % vmid)
|
||||||
timeout -= 1
|
timeout -= 1
|
||||||
if timeout == 0:
|
if timeout == 0:
|
||||||
module.fail_json(msg='Reached timeout while waiting for removing VM. Last line in task before timeout: %s'
|
module.fail_json(vmid=vmid, taskid=taskid, msg='Reached timeout while waiting for removing VM. Last line in task before timeout: %s'
|
||||||
% proxmox.proxmox_api.nodes(vm['node']).tasks(taskid).log.get()[:1])
|
% proxmox.proxmox_api.nodes(vm['node']).tasks(taskid).log.get()[:1])
|
||||||
|
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
module.fail_json(msg="deletion of VM %s failed with exception: %s" % (vmid, to_native(e)))
|
module.fail_json(vmid=vmid, msg="deletion of VM %s failed with exception: %s" % (vmid, to_native(e)))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in a new issue