Skip to content

Commit

Permalink
ec2_instance - Fix check_mode behaviour with tags (ansible-collection…
Browse files Browse the repository at this point in the history
…s#189)

* Add test for changing tags in check_mode

* ec2_instance: Fix check_mode behaviour with tags

* Add changelog fragment
  • Loading branch information
tremble authored Aug 28, 2020
1 parent 31fc869 commit 5e2d356
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/189-ec2_instance-check_mode-tags.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- ec2_instance - Fix a bug where tags were updated in check_mode.
2 changes: 2 additions & 0 deletions plugins/modules/ec2_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
wait: false
tags:
TestId: "{{ ec2_instance_tag_TestId }}"
TestTag: "Some Value"
register: basic_instance

- name: "Make basic instance (check mode)"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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"
Expand All @@ -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:
Expand All @@ -123,13 +129,35 @@
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
name: "{{ resource_prefix }}-checkmode-comparison"
vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
tags:
TestId: "{{ ec2_instance_tag_TestId }}"
TestTag: "Some Value"
check_mode: yes

- name: "fact ec2 instance"
Expand All @@ -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:
Expand Down

0 comments on commit 5e2d356

Please sign in to comment.