From 5be37018a2c9bba52f5b2fa68cd98e894270c03d Mon Sep 17 00:00:00 2001 From: Ondra Machacek Date: Mon, 28 Aug 2017 14:20:55 +0200 Subject: [PATCH] cloud: ovirt: present state shouldn't change state if not needed (#28214) This PR fixes: https://github.com/ansible/ansible/issues/27382 --- .../modules/cloud/ovirt/ovirt_hosts.py | 31 ++++---- lib/ansible/modules/cloud/ovirt/ovirt_vms.py | 75 ++++++++++--------- 2 files changed, 57 insertions(+), 49 deletions(-) diff --git a/lib/ansible/modules/cloud/ovirt/ovirt_hosts.py b/lib/ansible/modules/cloud/ovirt/ovirt_hosts.py index 4c343d5dfd..175f5b54bd 100644 --- a/lib/ansible/modules/cloud/ovirt/ovirt_hosts.py +++ b/lib/ansible/modules/cloud/ovirt/ovirt_hosts.py @@ -109,6 +109,13 @@ options: - "Enable or disable power management of the host." - "For more comprehensive setup of PM use C(ovirt_host_pm) module." version_added: 2.4 + activate: + description: + - "If C(state) is I(present) activate the host." + - "This parameter is good to disable, when you don't want to change + the state of host when using I(present) C(state)." + default: True + version_added: 2.4 iscsi: description: - "If C(state) is I(iscsidiscover) it means that the iscsi attribute is being @@ -295,12 +302,6 @@ class HostsModule(BaseModule): wait_condition=lambda h: h.status == hoststate.MAINTENANCE, ) - def post_update(self, entity): - if entity.status != hoststate.UP and self.param('state') == 'present': - if not self._module.check_mode: - self._service.host_service(entity.id).activate() - self.changed = True - def post_reinstall(self, host): wait( service=self._service.service(host.id), @@ -382,6 +383,7 @@ def main(): kernel_params=dict(default=None, type='list'), hosted_engine=dict(default=None, choices=['deploy', 'undeploy']), power_management_enabled=dict(default=None, type='bool'), + activate=dict(default=True, type='bool'), iscsi=dict(default=None, type='dict'), ) module = AnsibleModule( @@ -407,17 +409,20 @@ def main(): state = module.params['state'] host = control_state(hosts_module) if state == 'present': - hosts_module.create( + ret = hosts_module.create( deploy_hosted_engine=( module.params.get('hosted_engine') == 'deploy' ) if module.params.get('hosted_engine') is not None else None, + result_state=(lambda h: h.status == hoststate.UP) if host is None else None, + fail_condition=failed_state if host is None else lambda h: False, ) - ret = hosts_module.action( - action='activate', - action_condition=lambda h: h.status == hoststate.MAINTENANCE, - wait_condition=lambda h: h.status == hoststate.UP, - fail_condition=failed_state, - ) + if module.params['activate'] and host is not None: + ret = hosts_module.action( + action='activate', + action_condition=lambda h: h.status != hoststate.UP, + wait_condition=lambda h: h.status == hoststate.UP, + fail_condition=failed_state, + ) elif state == 'absent': ret = hosts_module.remove() elif state == 'maintenance': diff --git a/lib/ansible/modules/cloud/ovirt/ovirt_vms.py b/lib/ansible/modules/cloud/ovirt/ovirt_vms.py index d554034ff5..433cdfbf2a 100644 --- a/lib/ansible/modules/cloud/ovirt/ovirt_vms.py +++ b/lib/ansible/modules/cloud/ovirt/ovirt_vms.py @@ -1182,7 +1182,7 @@ def main(): vm = vms_module.search_entity() control_state(vm, vms_service, module) - if state == 'present' or state == 'running' or state == 'next_run': + if state in ('present', 'running', 'next_run'): if module.params['xen'] or module.params['kvm'] or module.params['vmware']: vms_module.changed = import_vm(module, connection) @@ -1200,46 +1200,49 @@ def main(): clone=module.params['clone'], clone_permissions=module.params['clone_permissions'], ) - initialization = _get_initialization(sysprep, cloud_init, cloud_init_nics) - ret = vms_module.action( - action='start', - post_action=vms_module._post_start_action, - action_condition=lambda vm: ( - vm.status not in [ - otypes.VmStatus.MIGRATING, - otypes.VmStatus.POWERING_UP, - otypes.VmStatus.REBOOT_IN_PROGRESS, - otypes.VmStatus.WAIT_FOR_LAUNCH, - otypes.VmStatus.UP, - otypes.VmStatus.RESTORING_STATE, - ] - ), - wait_condition=lambda vm: vm.status == otypes.VmStatus.UP, - # Start action kwargs: - use_cloud_init=cloud_init is not None or len(cloud_init_nics) > 0, - use_sysprep=sysprep is not None, - vm=otypes.Vm( - placement_policy=otypes.VmPlacementPolicy( - hosts=[otypes.Host(name=module.params['host'])] - ) if module.params['host'] else None, - initialization=initialization, - os=otypes.OperatingSystem( - cmdline=module.params.get('kernel_params'), - initrd=module.params.get('initrd_path'), - kernel=module.params.get('kernel_path'), + + # Run the VM if it was just created, else don't run it: + if state == 'running' or vm is None: + initialization = _get_initialization(sysprep, cloud_init, cloud_init_nics) + ret = vms_module.action( + action='start', + post_action=vms_module._post_start_action, + action_condition=lambda vm: ( + vm.status not in [ + otypes.VmStatus.MIGRATING, + otypes.VmStatus.POWERING_UP, + otypes.VmStatus.REBOOT_IN_PROGRESS, + otypes.VmStatus.WAIT_FOR_LAUNCH, + otypes.VmStatus.UP, + otypes.VmStatus.RESTORING_STATE, + ] + ), + wait_condition=lambda vm: vm.status == otypes.VmStatus.UP, + # Start action kwargs: + use_cloud_init=cloud_init is not None or len(cloud_init_nics) > 0, + use_sysprep=sysprep is not None, + vm=otypes.Vm( + placement_policy=otypes.VmPlacementPolicy( + hosts=[otypes.Host(name=module.params['host'])] + ) if module.params['host'] else None, + initialization=initialization, + os=otypes.OperatingSystem( + cmdline=module.params.get('kernel_params'), + initrd=module.params.get('initrd_path'), + kernel=module.params.get('kernel_path'), + ) if ( + module.params.get('kernel_params') + or module.params.get('initrd_path') + or module.params.get('kernel_path') + ) else None, ) if ( module.params.get('kernel_params') or module.params.get('initrd_path') or module.params.get('kernel_path') + or module.params.get('host') + or initialization ) else None, - ) if ( - module.params.get('kernel_params') - or module.params.get('initrd_path') - or module.params.get('kernel_path') - or module.params.get('host') - or initialization - ) else None, - ) + ) if state == 'next_run': # Apply next run configuration, if needed: