From 63702ff4d7003e061b50791dade02f60c62c2d10 Mon Sep 17 00:00:00 2001 From: Mark Chappell Date: Fri, 28 Aug 2020 02:12:41 +0200 Subject: [PATCH] ec2_instance - Fix check_mode behaviour with tags (#189) * Add test for changing tags in check_mode * ec2_instance: Fix check_mode behaviour with tags * Add changelog fragment --- plugins/modules/ec2_instance.py | 2 ++ .../ec2_instance/tasks/checkmode_tests.yml | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/plugins/modules/ec2_instance.py b/plugins/modules/ec2_instance.py index ddedd379573..e87f64cdf29 100644 --- a/plugins/modules/ec2_instance.py +++ b/plugins/modules/ec2_instance.py @@ -894,6 +894,8 @@ def manage_tags(match, new_tags, purge_tags, ec2): old_tags, new_tags, purge_tags=purge_tags, ) + if module.check_mode: + return bool(tags_to_delete or tags_to_set) if tags_to_set: ec2.create_tags( Resources=[match['InstanceId']], diff --git a/tests/integration/targets/ec2_instance/roles/ec2_instance/tasks/checkmode_tests.yml b/tests/integration/targets/ec2_instance/roles/ec2_instance/tasks/checkmode_tests.yml index b161eca636e..e13ad44063b 100644 --- a/tests/integration/targets/ec2_instance/roles/ec2_instance/tasks/checkmode_tests.yml +++ b/tests/integration/targets/ec2_instance/roles/ec2_instance/tasks/checkmode_tests.yml @@ -10,6 +10,7 @@ wait: false tags: TestId: "{{ ec2_instance_tag_TestId }}" + TestTag: "Some Value" register: basic_instance - name: "Make basic instance (check mode)" @@ -22,6 +23,7 @@ vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}" tags: TestId: "{{ ec2_instance_tag_TestId }}" + TestTag: "Some Value" check_mode: yes - name: "fact presented ec2 instance" @@ -49,6 +51,7 @@ vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}" tags: TestId: "{{ ec2_instance_tag_TestId }}" + TestTag: "Some Value" check_mode: yes - name: "fact ec2 instance" @@ -69,6 +72,7 @@ vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}" tags: TestId: "{{ ec2_instance_tag_TestId }}" + TestTag: "Some Value" register: instance_stop until: not instance_stop.failed retries: 10 @@ -91,6 +95,7 @@ vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}" tags: TestId: "{{ ec2_instance_tag_TestId }}" + TestTag: "Some Value" check_mode: yes - name: "fact ec2 instance" @@ -111,6 +116,7 @@ vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}" tags: TestId: "{{ ec2_instance_tag_TestId }}" + TestTag: "Some Value" - name: "fact ec2 instance." ec2_instance_info: @@ -123,6 +129,27 @@ that: - '"{{ confirm_runninginstance_fact.instances[0].state.name }}" == "running"' + - name: "Tag instance." + ec2_instance: + state: running + name: "{{ resource_prefix }}-checkmode-comparison" + vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}" + tags: + TestId: "{{ ec2_instance_tag_TestId }}" + TestTag: "Some Other Value" + check_mode: yes + + - name: "fact ec2 instance." + ec2_instance_info: + filters: + "tag:Name": "{{ resource_prefix }}-checkmode-comparison" + register: confirm_not_tagged + + - name: "Verify that it hasn't been re-tagged." + assert: + that: + - '"{{ confirm_not_tagged.instances[0].tags.TestTag }}" == "Some Value"' + - name: "Terminate instance in check mode." ec2_instance: state: absent @@ -130,6 +157,7 @@ vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}" tags: TestId: "{{ ec2_instance_tag_TestId }}" + TestTag: "Some Value" check_mode: yes - name: "fact ec2 instance" @@ -150,6 +178,7 @@ vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}" tags: TestId: "{{ ec2_instance_tag_TestId }}" + TestTag: "Some Value" - name: "fact ec2 instance" ec2_instance_info: