From a4983ce38a59fc7a4c1ee8e05934548cf37deb64 Mon Sep 17 00:00:00 2001 From: Gerben Welter Date: Mon, 24 Jan 2022 19:52:32 +0100 Subject: [PATCH] one_vm: add release action (#4036) * one_vm: add release action Previously you could create VMs with the `vm_start_on_hold` parameter but then ansible couldn't release the VMs so they would be scheduled to run. This PR adds the ability to release VMs which are in the 'HOLD' state. * Add changelog fragment * Update changelogs/fragments/4036-onevm-add-release-action.yaml Co-authored-by: Felix Fontein * Update plugins/modules/cloud/opennebula/one_vm.py Co-authored-by: Felix Fontein * Make releasing a VM part of the running state When `state: running` is specified the code checks if the VM is in a 'HOLD' state and will release the VM when needed. Co-authored-by: Gerben Welter Co-authored-by: Felix Fontein --- .../4036-onevm-add-release-action.yaml | 2 ++ plugins/modules/cloud/opennebula/one_vm.py | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 changelogs/fragments/4036-onevm-add-release-action.yaml diff --git a/changelogs/fragments/4036-onevm-add-release-action.yaml b/changelogs/fragments/4036-onevm-add-release-action.yaml new file mode 100644 index 0000000000..bc61278b4a --- /dev/null +++ b/changelogs/fragments/4036-onevm-add-release-action.yaml @@ -0,0 +1,2 @@ +minor_changes: + - opennebula - add the release action for VMs in the ``HOLD`` state (https://github.com/ansible-collections/community.general/pull/4036). diff --git a/plugins/modules/cloud/opennebula/one_vm.py b/plugins/modules/cloud/opennebula/one_vm.py index fa3d4abaab..86061f73cb 100644 --- a/plugins/modules/cloud/opennebula/one_vm.py +++ b/plugins/modules/cloud/opennebula/one_vm.py @@ -1260,6 +1260,11 @@ def resume_vm(module, client, vm): vm = client.vm.info(vm.ID) changed = False + state = vm.STATE + if state in [VM_STATES.index('HOLD')]: + changed = release_vm(module, client, vm) + return changed + lcm_state = vm.LCM_STATE if lcm_state == LCM_STATES.index('SHUTDOWN_POWEROFF'): module.fail_json(msg="Cannot perform action 'resume' because this action is not available " + @@ -1282,6 +1287,23 @@ def resume_vms(module, client, vms): return changed +def release_vm(module, client, vm): + vm = client.vm.info(vm.ID) + changed = False + + state = vm.STATE + if state != VM_STATES.index('HOLD'): + module.fail_json(msg="Cannot perform action 'release' because this action is not available " + + "because VM is not in state 'HOLD'.") + else: + changed = True + + if changed and not module.check_mode: + client.vm.action('release', vm.ID) + + return changed + + def check_name_attribute(module, attributes): if attributes.get("NAME"): import re