mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
* Added VMID to all returns
Also added in the docs promised return of MAC and devices when state ==
current.
Fixes: #1641
* Revert devices and mac as get_vminfo works differently then I expected
* Added status output to a few more calls
* Update RETURNS docs
* Remove vmid where it is not available
* Added changelog fragment
* Update changelogs/fragments/1715-proxmox_kvm-add-vmid-to-returns.yml
You're right, this message is way better then mine.. :-)
Co-authored-by: Felix Fontein <felix@fontein.de>
Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit 1cce279424
)
Co-authored-by: Jeffrey van Pelt <jeff@vanpelt.one>
This commit is contained in:
parent
bc3435b993
commit
6c50119eab
2 changed files with 33 additions and 53 deletions
|
@ -0,0 +1,2 @@
|
||||||
|
minor_changes:
|
||||||
|
- proxmox_kvm module - actually implemented ``vmid`` and ``status`` return values. Updated documentation to reflect current situation (https://github.com/ansible-collections/community.general/issues/1410, https://github.com/ansible-collections/community.general/pull/1715).
|
|
@ -730,27 +730,6 @@ EXAMPLES = '''
|
||||||
'''
|
'''
|
||||||
|
|
||||||
RETURN = '''
|
RETURN = '''
|
||||||
devices:
|
|
||||||
description: The list of devices created or used.
|
|
||||||
returned: success
|
|
||||||
type: dict
|
|
||||||
sample: '
|
|
||||||
{
|
|
||||||
"ide0": "VMS_LVM:vm-115-disk-1",
|
|
||||||
"ide1": "VMs:115/vm-115-disk-3.raw",
|
|
||||||
"virtio0": "VMS_LVM:vm-115-disk-2",
|
|
||||||
"virtio1": "VMs:115/vm-115-disk-1.qcow2",
|
|
||||||
"virtio2": "VMs:115/vm-115-disk-2.raw"
|
|
||||||
}'
|
|
||||||
mac:
|
|
||||||
description: List of mac address created and net[n] attached. Useful when you want to use provision systems like Foreman via PXE.
|
|
||||||
returned: success
|
|
||||||
type: dict
|
|
||||||
sample: '
|
|
||||||
{
|
|
||||||
"net0": "3E:6E:97:D2:31:9F",
|
|
||||||
"net1": "B6:A1:FC:EF:78:A4"
|
|
||||||
}'
|
|
||||||
vmid:
|
vmid:
|
||||||
description: The VM vmid.
|
description: The VM vmid.
|
||||||
returned: success
|
returned: success
|
||||||
|
@ -759,7 +738,6 @@ vmid:
|
||||||
status:
|
status:
|
||||||
description:
|
description:
|
||||||
- The current virtual machine status.
|
- The current virtual machine status.
|
||||||
- Returned only when C(state=current)
|
|
||||||
returned: success
|
returned: success
|
||||||
type: dict
|
type: dict
|
||||||
sample: '{
|
sample: '{
|
||||||
|
@ -1201,36 +1179,36 @@ def main():
|
||||||
|
|
||||||
# Ensure source VM id exists when cloning
|
# Ensure source VM id exists when cloning
|
||||||
if not get_vm(proxmox, vmid):
|
if not get_vm(proxmox, vmid):
|
||||||
module.fail_json(msg='VM with vmid = %s does not exist in cluster' % vmid)
|
module.fail_json(vmid=vmid, msg='VM with vmid = %s does not exist in cluster' % vmid)
|
||||||
|
|
||||||
# Ensure the choosen VM name doesn't already exist when cloning
|
# Ensure the choosen VM name doesn't already exist when cloning
|
||||||
if get_vmid(proxmox, name):
|
if get_vmid(proxmox, name):
|
||||||
module.exit_json(changed=False, msg="VM with name <%s> already exists" % name)
|
module.exit_json(changed=False, vmid=vmid, msg="VM with name <%s> already exists" % name)
|
||||||
|
|
||||||
# Ensure the choosen VM id doesn't already exist when cloning
|
# Ensure the choosen VM id doesn't already exist when cloning
|
||||||
if get_vm(proxmox, newid):
|
if get_vm(proxmox, newid):
|
||||||
module.exit_json(changed=False, msg="vmid %s with VM name %s already exists" % (newid, name))
|
module.exit_json(changed=False, vmid=vmid, msg="vmid %s with VM name %s already exists" % (newid, name))
|
||||||
|
|
||||||
if delete is not None:
|
if delete is not None:
|
||||||
try:
|
try:
|
||||||
settings(module, proxmox, vmid, node, name, delete=delete)
|
settings(module, proxmox, vmid, node, name, delete=delete)
|
||||||
module.exit_json(changed=True, msg="Settings has deleted on VM {0} with vmid {1}".format(name, vmid))
|
module.exit_json(changed=True, vmid=vmid, msg="Settings has deleted on VM {0} with vmid {1}".format(name, vmid))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
module.fail_json(msg='Unable to delete settings on VM {0} with vmid {1}: '.format(name, vmid) + str(e))
|
module.fail_json(vmid=vmid, msg='Unable to delete settings on VM {0} with vmid {1}: '.format(name, vmid) + str(e))
|
||||||
|
|
||||||
if revert is not None:
|
if revert is not None:
|
||||||
try:
|
try:
|
||||||
settings(module, proxmox, vmid, node, name, revert=revert)
|
settings(module, proxmox, vmid, node, name, revert=revert)
|
||||||
module.exit_json(changed=True, msg="Settings has reverted on VM {0} with vmid {1}".format(name, vmid))
|
module.exit_json(changed=True, vmid=vmid, msg="Settings has reverted on VM {0} with vmid {1}".format(name, vmid))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
module.fail_json(msg='Unable to revert settings on VM {0} with vmid {1}: Maybe is not a pending task... '.format(name, vmid) + str(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 state == 'present':
|
if state == 'present':
|
||||||
try:
|
try:
|
||||||
if get_vm(proxmox, vmid) and not (update or clone):
|
if get_vm(proxmox, vmid) and not (update or clone):
|
||||||
module.exit_json(changed=False, msg="VM with vmid <%s> already exists" % vmid)
|
module.exit_json(changed=False, vmid=vmid, msg="VM with vmid <%s> already exists" % vmid)
|
||||||
elif get_vmid(proxmox, name) and not (update or clone):
|
elif get_vmid(proxmox, name) and not (update or clone):
|
||||||
module.exit_json(changed=False, msg="VM with name <%s> already exists" % name)
|
module.exit_json(changed=False, vmid=vmid, msg="VM with name <%s> already exists" % name)
|
||||||
elif not (node, name):
|
elif not (node, name):
|
||||||
module.fail_json(msg='node, name is mandatory for creating/updating vm')
|
module.fail_json(msg='node, name is mandatory for creating/updating vm')
|
||||||
elif not node_check(proxmox, node):
|
elif not node_check(proxmox, node):
|
||||||
|
@ -1303,18 +1281,18 @@ def main():
|
||||||
scsi=module.params['scsi'],
|
scsi=module.params['scsi'],
|
||||||
virtio=module.params['virtio'])
|
virtio=module.params['virtio'])
|
||||||
if update:
|
if update:
|
||||||
module.exit_json(changed=True, msg="VM %s with vmid %s updated" % (name, vmid))
|
module.exit_json(changed=True, vmid=vmid, msg="VM %s with vmid %s updated" % (name, vmid))
|
||||||
elif clone is not None:
|
elif clone is not None:
|
||||||
module.exit_json(changed=True, msg="VM %s with newid %s cloned from vm with vmid %s" % (name, newid, vmid))
|
module.exit_json(changed=True, vmid=vmid, msg="VM %s with newid %s cloned from vm with vmid %s" % (name, newid, vmid))
|
||||||
else:
|
else:
|
||||||
module.exit_json(changed=True, msg="VM %s with vmid %s deployed" % (name, vmid), **results)
|
module.exit_json(changed=True, vmid=vmid, msg="VM %s with vmid %s deployed" % (name, vmid), **results)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if update:
|
if update:
|
||||||
module.fail_json(msg="Unable to update vm {0} with vmid {1}=".format(name, vmid) + str(e))
|
module.fail_json(vmid=vmid, msg="Unable to update vm {0} with vmid {1}=".format(name, vmid) + str(e))
|
||||||
elif clone is not None:
|
elif clone is not None:
|
||||||
module.fail_json(msg="Unable to clone vm {0} from vmid {1}=".format(name, vmid) + str(e))
|
module.fail_json(vmid=vmid, msg="Unable to clone vm {0} from vmid {1}=".format(name, vmid) + str(e))
|
||||||
else:
|
else:
|
||||||
module.fail_json(msg="creation of qemu VM %s with vmid %s failed with exception=%s" % (name, vmid, e))
|
module.fail_json(vmid=vmid, msg="creation of qemu VM %s with vmid %s failed with exception=%s" % (name, vmid, e))
|
||||||
|
|
||||||
elif state == 'started':
|
elif state == 'started':
|
||||||
try:
|
try:
|
||||||
|
@ -1322,14 +1300,14 @@ def main():
|
||||||
module.fail_json(msg='VM with name = %s does not exist in cluster' % name)
|
module.fail_json(msg='VM with name = %s does not exist in cluster' % name)
|
||||||
vm = get_vm(proxmox, vmid)
|
vm = get_vm(proxmox, vmid)
|
||||||
if not vm:
|
if not vm:
|
||||||
module.fail_json(msg='VM with vmid <%s> does not exist in cluster' % vmid)
|
module.fail_json(vmid=vmid, msg='VM with vmid <%s> does not exist in cluster' % vmid)
|
||||||
if vm[0]['status'] == 'running':
|
if vm[0]['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, **status)
|
||||||
|
|
||||||
if start_vm(module, proxmox, vm):
|
if start_vm(module, proxmox, vm):
|
||||||
module.exit_json(changed=True, msg="VM %s started" % vmid)
|
module.exit_json(changed=True, vmid=vmid, msg="VM %s started" % vmid, **status)
|
||||||
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), **status)
|
||||||
|
|
||||||
elif state == 'stopped':
|
elif state == 'stopped':
|
||||||
try:
|
try:
|
||||||
|
@ -1338,15 +1316,15 @@ def main():
|
||||||
|
|
||||||
vm = get_vm(proxmox, vmid)
|
vm = get_vm(proxmox, vmid)
|
||||||
if not vm:
|
if not vm:
|
||||||
module.fail_json(msg='VM with vmid = %s does not exist in cluster' % vmid)
|
module.fail_json(vmid=vmid, msg='VM with vmid = %s does not exist in cluster' % vmid)
|
||||||
|
|
||||||
if vm[0]['status'] == 'stopped':
|
if vm[0]['status'] == 'stopped':
|
||||||
module.exit_json(changed=False, msg="VM %s is already stopped" % vmid)
|
module.exit_json(changed=False, vmid=vmid, msg="VM %s is already stopped" % vmid, **status)
|
||||||
|
|
||||||
if stop_vm(module, proxmox, vm, force=module.params['force']):
|
if stop_vm(module, proxmox, vm, 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, **status)
|
||||||
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), **status)
|
||||||
|
|
||||||
elif state == 'restarted':
|
elif state == 'restarted':
|
||||||
try:
|
try:
|
||||||
|
@ -1355,33 +1333,33 @@ def main():
|
||||||
|
|
||||||
vm = get_vm(proxmox, vmid)
|
vm = get_vm(proxmox, vmid)
|
||||||
if not vm:
|
if not vm:
|
||||||
module.fail_json(msg='VM with vmid = %s does not exist in cluster' % vmid)
|
module.fail_json(vmid=vmid, msg='VM with vmid = %s does not exist in cluster' % vmid)
|
||||||
if vm[0]['status'] == 'stopped':
|
if vm[0]['status'] == 'stopped':
|
||||||
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, **status)
|
||||||
|
|
||||||
if stop_vm(module, proxmox, vm, force=module.params['force']) and start_vm(module, proxmox, vm):
|
if stop_vm(module, proxmox, vm, force=module.params['force']) and start_vm(module, proxmox, vm):
|
||||||
module.exit_json(changed=True, msg="VM %s is restarted" % vmid)
|
module.exit_json(changed=True, vmid=vmid, msg="VM %s is restarted" % vmid, **status)
|
||||||
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), **status)
|
||||||
|
|
||||||
elif state == 'absent':
|
elif state == 'absent':
|
||||||
try:
|
try:
|
||||||
vm = get_vm(proxmox, vmid)
|
vm = get_vm(proxmox, vmid)
|
||||||
if not vm:
|
if not vm:
|
||||||
module.exit_json(changed=False)
|
module.exit_json(changed=False, vmid=vmid)
|
||||||
|
|
||||||
proxmox_node = proxmox.nodes(vm[0]['node'])
|
proxmox_node = proxmox.nodes(vm[0]['node'])
|
||||||
if vm[0]['status'] == 'running':
|
if vm[0]['status'] == 'running':
|
||||||
if module.params['force']:
|
if module.params['force']:
|
||||||
stop_vm(module, proxmox, vm, True)
|
stop_vm(module, proxmox, vm, True)
|
||||||
else:
|
else:
|
||||||
module.exit_json(changed=False, msg="VM %s is running. Stop it before deletion or use force=yes." % vmid)
|
module.exit_json(changed=False, vmid=vmid, msg="VM %s is running. Stop it before deletion or use force=yes." % vmid)
|
||||||
taskid = proxmox_node.qemu.delete(vmid)
|
taskid = proxmox_node.qemu.delete(vmid)
|
||||||
if not wait_for_task(module, proxmox, vm[0]['node'], taskid):
|
if not wait_for_task(module, proxmox, vm[0]['node'], taskid):
|
||||||
module.fail_json(msg='Reached timeout while waiting for removing VM. Last line in task before timeout: %s' %
|
module.fail_json(msg='Reached timeout while waiting for removing VM. Last line in task before timeout: %s' %
|
||||||
proxmox_node.tasks(taskid).log.get()[:1])
|
proxmox_node.tasks(taskid).log.get()[:1])
|
||||||
else:
|
else:
|
||||||
module.exit_json(changed=True, msg="VM %s removed" % vmid)
|
module.exit_json(changed=True, vmid=vmid, msg="VM %s removed" % vmid)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
module.fail_json(msg="deletion of VM %s failed with exception: %s" % (vmid, e))
|
module.fail_json(msg="deletion of VM %s failed with exception: %s" % (vmid, e))
|
||||||
|
|
||||||
|
@ -1395,7 +1373,7 @@ def main():
|
||||||
current = proxmox.nodes(vm[0]['node']).qemu(vmid).status.current.get()['status']
|
current = proxmox.nodes(vm[0]['node']).qemu(vmid).status.current.get()['status']
|
||||||
status['status'] = current
|
status['status'] = current
|
||||||
if status:
|
if status:
|
||||||
module.exit_json(changed=False, msg="VM %s with vmid = %s is %s" % (name, vmid, current), **status)
|
module.exit_json(changed=False, vmid=vmid, msg="VM %s with vmid = %s is %s" % (name, vmid, current), **status)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in a new issue