From 9603ea297370717daa83b072f1e936594e07024c Mon Sep 17 00:00:00 2001 From: Joseph Torcasso Date: Wed, 1 Jun 2022 14:53:15 -0400 Subject: [PATCH 1/3] fix none tags bug --- plugins/modules/ec2_instance.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/plugins/modules/ec2_instance.py b/plugins/modules/ec2_instance.py index 2bc65d9a729..790f099eaae 100644 --- a/plugins/modules/ec2_instance.py +++ b/plugins/modules/ec2_instance.py @@ -1780,7 +1780,7 @@ def determine_iam_role(name_or_arn): def handle_existing(existing_matches, state): - tags = dict(module.params.get('tags', None)) + tags = dict(module.params.get('tags') or {}) purge_tags = module.params.get('purge_tags') name = module.params.get('name') @@ -1788,9 +1788,8 @@ def handle_existing(existing_matches, state): # into tags, but since tags isn't explicitly passed we'll treat it not being # set as purge_tags == False if name: - if purge_tags and tags is None: + if purge_tags and not tags: purge_tags = False - tags = {} tags.update({'Name': name}) changed = False From 29b7f9ac38efe485540b7381ba63c607e1ab2827 Mon Sep 17 00:00:00 2001 From: Joseph Torcasso Date: Wed, 1 Jun 2022 14:55:14 -0400 Subject: [PATCH 2/3] add changelog --- .../fragments/856-ec2_instance-fix-nonetype-on-no-tags.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelogs/fragments/856-ec2_instance-fix-nonetype-on-no-tags.yml diff --git a/changelogs/fragments/856-ec2_instance-fix-nonetype-on-no-tags.yml b/changelogs/fragments/856-ec2_instance-fix-nonetype-on-no-tags.yml new file mode 100644 index 00000000000..b26c512fba9 --- /dev/null +++ b/changelogs/fragments/856-ec2_instance-fix-nonetype-on-no-tags.yml @@ -0,0 +1,2 @@ +bugfixes: + - ec2_instance - fix NoneType error when no tags are input (https://github.com/ansible-collections/amazon.aws/pull/856). From c650a92332a5889ab8e28a45d2ae3d4353129f68 Mon Sep 17 00:00:00 2001 From: Joseph Torcasso Date: Wed, 1 Jun 2022 15:59:25 -0400 Subject: [PATCH 3/3] update tags logic and add tests to validate --- ...6-ec2_instance-fix-nonetype-on-no-tags.yml | 2 -- plugins/modules/ec2_instance.py | 5 +-- .../tasks/tags_and_vpc_settings.yml | 36 ++++++++++++++++++- 3 files changed, 38 insertions(+), 5 deletions(-) delete mode 100644 changelogs/fragments/856-ec2_instance-fix-nonetype-on-no-tags.yml diff --git a/changelogs/fragments/856-ec2_instance-fix-nonetype-on-no-tags.yml b/changelogs/fragments/856-ec2_instance-fix-nonetype-on-no-tags.yml deleted file mode 100644 index b26c512fba9..00000000000 --- a/changelogs/fragments/856-ec2_instance-fix-nonetype-on-no-tags.yml +++ /dev/null @@ -1,2 +0,0 @@ -bugfixes: - - ec2_instance - fix NoneType error when no tags are input (https://github.com/ansible-collections/amazon.aws/pull/856). diff --git a/plugins/modules/ec2_instance.py b/plugins/modules/ec2_instance.py index 790f099eaae..efca90e5740 100644 --- a/plugins/modules/ec2_instance.py +++ b/plugins/modules/ec2_instance.py @@ -1780,7 +1780,7 @@ def determine_iam_role(name_or_arn): def handle_existing(existing_matches, state): - tags = dict(module.params.get('tags') or {}) + tags = module.params.get('tags') purge_tags = module.params.get('purge_tags') name = module.params.get('name') @@ -1788,8 +1788,9 @@ def handle_existing(existing_matches, state): # into tags, but since tags isn't explicitly passed we'll treat it not being # set as purge_tags == False if name: - if purge_tags and not tags: + if tags is None: purge_tags = False + tags = {} tags.update({'Name': name}) changed = False diff --git a/tests/integration/targets/ec2_instance/roles/ec2_instance/tasks/tags_and_vpc_settings.yml b/tests/integration/targets/ec2_instance/roles/ec2_instance/tasks/tags_and_vpc_settings.yml index 04ecd22a241..a81e0040b42 100644 --- a/tests/integration/targets/ec2_instance/roles/ec2_instance/tasks/tags_and_vpc_settings.yml +++ b/tests/integration/targets/ec2_instance/roles/ec2_instance/tasks/tags_and_vpc_settings.yml @@ -138,11 +138,45 @@ - check_tags.instances[0].tags.Name.startswith(resource_prefix) - "'{{ check_tags.instances[0].state.name }}' in ['pending', 'running']" + - name: "Try setting purge_tags to True without specifiying tags (should NOT purge tags)" + ec2_instance: + state: present + name: "{{ resource_prefix }}-test-basic-vpc-create" + image_id: "{{ ec2_ami_id }}" + purge_tags: true + security_groups: "{{ sg.group_id }}" + vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}" + instance_type: "{{ ec2_instance_type }}" + register: _purge_tags_without_tags + + - name: Assert tags were not purged + assert: + that: + - _purge_tags_without_tags.instances[0].tags | length > 1 + + - name: "Purge all tags (aside from Name)" + ec2_instance: + state: present + name: "{{ resource_prefix }}-test-basic-vpc-create" + image_id: "{{ ec2_ami_id }}" + purge_tags: true + tags: {} + security_groups: "{{ sg.group_id }}" + vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}" + instance_type: "{{ ec2_instance_type }}" + register: _purge_tags + + - name: Assert tags were purged + assert: + that: + - _purge_tags.instances[0].tags | length == 1 + - _purge_tags.instances[0].tags.Name.startswith(resource_prefix) + - name: "Terminate instance" ec2_instance: state: absent filters: - "tag:TestId": "{{ ec2_instance_tag_TestId }}" + "tag:Name": "{{ resource_prefix }}-test-basic-vpc-create" wait: false register: result - assert: