diff --git a/lib/ansible/modules/cloud/cloudstack/cs_instance.py b/lib/ansible/modules/cloud/cloudstack/cs_instance.py index 9608e7587f..ee17cfb317 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_instance.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_instance.py @@ -750,7 +750,6 @@ class AnsibleCloudStackInstance(AnsibleCloudStack): security_groups_changed = self.security_groups_has_changed() # Volume data - args_volume_update = {} root_disk_size = self.module.params.get('root_disk_size') root_disk_size_changed = False @@ -824,13 +823,14 @@ class AnsibleCloudStackInstance(AnsibleCloudStack): if instance_state == 'running' and start_vm: instance = self.start_instance() else: - self.module.warn("Changes won't be applied to running instances. " + + self.module.warn("Changes won't be applied to running instances. " "Use force=true to allow the instance %s to be stopped/started." % instance['name']) # migrate to other host host_changed = all([ - instance['state'].lower() == 'running', - self.module.params.get('host'), + instance['state'].lower() in ['starting', 'running'], + instance.get('hostname') is not None, + self.module.params.get('host') is not None, self.module.params.get('host') != instance.get('hostname') ]) if host_changed: diff --git a/test/integration/targets/cs_instance/tasks/host.yml b/test/integration/targets/cs_instance/tasks/host.yml new file mode 100644 index 0000000000..c0614a1624 --- /dev/null +++ b/test/integration/targets/cs_instance/tasks/host.yml @@ -0,0 +1,134 @@ +--- +- name: setup ensure running instance to get host infos + cs_instance: + name: "{{ cs_resource_prefix }}-vm-{{ instance_number }}" + template: "{{ test_cs_instance_template }}" + service_offering: "{{ test_cs_instance_offering_1 }}" + state: started + register: running_instance + +- name: setup ensure stopped instance + cs_instance: + name: "{{ cs_resource_prefix }}-vm-{{ instance_number }}" + state: stopped + +- name: setup zone facts + cs_zone_facts: + name: "{{ cs_common_zone_basic }}" + +- name: setup find the host name + shell: cs listHosts type=routing zoneid="{{ cloudstack_zone.id }}" + args: + chdir: "{{ playbook_dir }}" + register: host + +- name: host convert from json + set_fact: + host_json: "{{ host.stdout | from_json }}" + +- name: select a host on which the instance was not running on + set_fact: + host: "{{ host_json | json_query('host[?name!=`' + running_instance.host + '`] | [0]') }}" + +- debug: + msg: "from current host {{ running_instance.host }} to new host {{ host.name }}" + +- name: test starting instance on new host in check mode + cs_instance: + name: "{{ cs_resource_prefix }}-vm-{{ instance_number }}" + host: "{{ host.name }}" + state: started + register: instance + check_mode: true +- name: verify test starting instance on new host in check mode + assert: + that: + - instance is successful + - instance is changed + - instance.name == "{{ cs_resource_prefix }}-vm-{{ instance_number }}" + - instance.host is not defined + - instance.state == "Stopped" + +- name: test starting instance on new host + cs_instance: + name: "{{ cs_resource_prefix }}-vm-{{ instance_number }}" + host: "{{ host.name }}" + state: started + register: instance +- name: verify test starting instance on new host + assert: + that: + - instance is successful + - instance is changed + - instance.name == "{{ cs_resource_prefix }}-vm-{{ instance_number }}" + - instance.host == "{{ host.name }}" + - instance.state == "Running" + +- name: test starting instance on new host idempotence + cs_instance: + name: "{{ cs_resource_prefix }}-vm-{{ instance_number }}" + host: "{{ host.name }}" + state: started + register: instance +- name: verify test starting instance on new host idempotence + assert: + that: + - instance is successful + - instance is not changed + - instance.name == "{{ cs_resource_prefix }}-vm-{{ instance_number }}" + - instance.host == "{{ host.name }}" + - instance.state == "Running" + +- name: select a host on which the instance is not running on + set_fact: + host: "{{ host_json | json_query('host[?name!=`' + instance.host + '`] | [0]') }}" + +- debug: + msg: "from current host {{ instance.host }} to new host {{ host.name }}" + +- name: test force update running instance in check mode + cs_instance: + name: "{{ cs_resource_prefix }}-vm-{{ instance_number }}" + host: "{{ host.name }}" + force: true + register: instance + check_mode: true +- name: verify force update running instance in check mode + assert: + that: + - instance is successful + - instance is changed + - instance.name == "{{ cs_resource_prefix }}-vm-{{ instance_number }}" + - instance.host != "{{ host.name }}" + - instance.state == "Running" + +- name: test force update running instance + cs_instance: + name: "{{ cs_resource_prefix }}-vm-{{ instance_number }}" + host: "{{ host.name }}" + force: true + register: instance +- name: verify force update running instance + assert: + that: + - instance is successful + - instance is changed + - instance.name == "{{ cs_resource_prefix }}-vm-{{ instance_number }}" + - instance.host == "{{ host.name }}" + - instance.state == "Running" + +- name: test force update running instance idempotence + cs_instance: + name: "{{ cs_resource_prefix }}-vm-{{ instance_number }}" + host: "{{ host.name }}" + force: true + register: instance +- name: verify force update running instance idempotence + assert: + that: + - instance is successful + - instance is not changed + - instance.name == "{{ cs_resource_prefix }}-vm-{{ instance_number }}" + - instance.display_name == "{{ cs_resource_prefix }}-display-{{ instance_number }}" + - instance.host == "{{ host.name }}" + - instance.state == "Running" diff --git a/test/integration/targets/cs_instance/tasks/main.yml b/test/integration/targets/cs_instance/tasks/main.yml index 1c81b7977b..43e22127ba 100644 --- a/test/integration/targets/cs_instance/tasks/main.yml +++ b/test/integration/targets/cs_instance/tasks/main.yml @@ -8,6 +8,10 @@ - include_tasks: present_display_name.yml - include_tasks: absent_display_name.yml +# TODO: These tests randomly fail in all kinds of unexpected states. +# This needs to be verified by the cloudstack community. +# - include_tasks: host.yml + - include_tasks: sshkeys.yml - include_tasks: project.yml diff --git a/test/integration/targets/cs_instance/tasks/present.yml b/test/integration/targets/cs_instance/tasks/present.yml index be7c8c58ab..7edf54bed3 100644 --- a/test/integration/targets/cs_instance/tasks/present.yml +++ b/test/integration/targets/cs_instance/tasks/present.yml @@ -212,46 +212,9 @@ - instance.service_offering == "{{ test_cs_instance_offering_2 }}" - instance.state == "Stopped" -- name: setup zone facts - cs_zone_facts: - name: "{{ cs_common_zone_basic }}" - -- name: setup find the host name - shell: cs listHosts type=routing zoneid="{{ cloudstack_zone.id }}" - args: - chdir: "{{ playbook_dir }}" - register: host - -- name: host convert from json - set_fact: - host_json: "{{ host.stdout | from_json }}" - -- name: select a host on which the instance is not running on - set_fact: - host: "{{ host_json | json_query('host[?name!=`' + cloudstack_instance.host + '`] | [0]') }}" - -- name: test starting instance in check mode - cs_instance: - name: "{{ cs_resource_prefix }}-vm-{{ instance_number }}" - host: "{{ host.name }}" - state: started - register: instance - check_mode: true -- name: verify starting instance in check mode - assert: - that: - - instance is successful - - instance is changed - - instance.name == "{{ cs_resource_prefix }}-vm-{{ instance_number }}" - - instance.display_name == "{{ cs_resource_prefix }}-display-{{ instance_number }}" - - instance.service_offering == "{{ test_cs_instance_offering_2 }}" - - instance.host is not defined - - instance.state == "Stopped" - - name: test starting instance cs_instance: name: "{{ cs_resource_prefix }}-vm-{{ instance_number }}" - host: "{{ host.name }}" state: started register: instance - name: verify starting instance @@ -262,14 +225,11 @@ - instance.name == "{{ cs_resource_prefix }}-vm-{{ instance_number }}" - instance.display_name == "{{ cs_resource_prefix }}-display-{{ instance_number }}" - instance.service_offering == "{{ test_cs_instance_offering_2 }}" - # TODO: this fails randomly, cloudstack issue? - #- instance.host == "{{ host.name }}" - instance.state == "Running" - name: test starting instance idempotence cs_instance: name: "{{ cs_resource_prefix }}-vm-{{ instance_number }}" - host: "{{ host.name }}" state: started register: instance - name: verify starting instance idempotence @@ -280,19 +240,12 @@ - instance.name == "{{ cs_resource_prefix }}-vm-{{ instance_number }}" - instance.display_name == "{{ cs_resource_prefix }}-display-{{ instance_number }}" - instance.service_offering == "{{ test_cs_instance_offering_2 }}" - # TODO: this fails randomly, cloudstack issue? - #- instance.host == "{{ host.name }}" - instance.state == "Running" -- name: select a host on which the instance is not running on - set_fact: - host: "{{ host_json | json_query('host[?name!=`' + instance.host + '`] | [0]') }}" - - name: test force update running instance in check mode cs_instance: name: "{{ cs_resource_prefix }}-vm-{{ instance_number }}" service_offering: "{{ test_cs_instance_offering_1 }}" - host: "{{ host.name }}" force: true register: instance check_mode: true @@ -304,14 +257,12 @@ - instance.name == "{{ cs_resource_prefix }}-vm-{{ instance_number }}" - instance.display_name == "{{ cs_resource_prefix }}-display-{{ instance_number }}" - instance.service_offering == "{{ test_cs_instance_offering_2 }}" - - instance.host != "{{ host.name }}" - instance.state == "Running" - name: test force update running instance cs_instance: name: "{{ cs_resource_prefix }}-vm-{{ instance_number }}" service_offering: "{{ test_cs_instance_offering_1 }}" - host: "{{ host.name }}" force: true register: instance - name: verify force update running instance @@ -322,14 +273,12 @@ - instance.name == "{{ cs_resource_prefix }}-vm-{{ instance_number }}" - instance.display_name == "{{ cs_resource_prefix }}-display-{{ instance_number }}" - instance.service_offering == "{{ test_cs_instance_offering_1 }}" - - instance.host == "{{ host.name }}" - instance.state == "Running" - name: test force update running instance idempotence cs_instance: name: "{{ cs_resource_prefix }}-vm-{{ instance_number }}" service_offering: "{{ test_cs_instance_offering_1 }}" - host: "{{ host.name }}" force: true register: instance - name: verify force update running instance idempotence @@ -340,7 +289,6 @@ - instance.name == "{{ cs_resource_prefix }}-vm-{{ instance_number }}" - instance.display_name == "{{ cs_resource_prefix }}-display-{{ instance_number }}" - instance.service_offering == "{{ test_cs_instance_offering_1 }}" - - instance.host == "{{ host.name }}" - instance.state == "Running" - name: test restore instance in check mode diff --git a/test/integration/targets/cs_snapshot_policy/aliases b/test/integration/targets/cs_snapshot_policy/aliases index d454c8ffb7..c89c86d7d2 100644 --- a/test/integration/targets/cs_snapshot_policy/aliases +++ b/test/integration/targets/cs_snapshot_policy/aliases @@ -1,3 +1,2 @@ cloud/cs shippable/cs/group1 -unstable diff --git a/test/integration/targets/cs_snapshot_policy/tasks/main.yml b/test/integration/targets/cs_snapshot_policy/tasks/main.yml index ba525f2f40..d5ba1bdc81 100644 --- a/test/integration/targets/cs_snapshot_policy/tasks/main.yml +++ b/test/integration/targets/cs_snapshot_policy/tasks/main.yml @@ -4,7 +4,6 @@ name: "{{ cs_resource_prefix }}-vm-snapshot-policy" template: "{{ cs_common_template }}" service_offering: "{{ cs_common_service_offering }}" - zone: "{{ cs_common_zone_adv }}" register: instance - name: verify setup instance assert: