From 5b179e9fc773db0c5b3d514997f06e4a978428ed Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Mon, 13 Mar 2023 12:15:16 +0000 Subject: [PATCH] Handle ec2_vol.tags when the associated instance already exists (#1071) (#1423) [PR #1071/ffd06e97 backport][stable-5] Handle ec2_vol.tags when the associated instance already exists This is a backport of PR #1071 as merged into main (ffd06e9). Signed-off-by: GomathiselviS gomathiselvi@gmail.com SUMMARY Fixes #1067 Fixes: #1421 ISSUE TYPE Bugfix Pull Request COMPONENT NAME ec2_vol.py ADDITIONAL INFORMATION Reviewed-by: Mark Chappell --- .../1071-ec2_vol_tags_idempotent.yaml | 3 ++ plugins/modules/ec2_vol.py | 4 +- .../targets/ec2_vol/tasks/main.yml | 40 +++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/1071-ec2_vol_tags_idempotent.yaml diff --git a/changelogs/fragments/1071-ec2_vol_tags_idempotent.yaml b/changelogs/fragments/1071-ec2_vol_tags_idempotent.yaml new file mode 100644 index 00000000000..b572254d5f7 --- /dev/null +++ b/changelogs/fragments/1071-ec2_vol_tags_idempotent.yaml @@ -0,0 +1,3 @@ +--- +bugfixes: +- ec2_vol - handle ec2_vol.tags when the associated instance already exists (https://github.com/ansible-collections/amazon.aws/pull/1071). diff --git a/plugins/modules/ec2_vol.py b/plugins/modules/ec2_vol.py index adefaf73731..9e6c38a14aa 100644 --- a/plugins/modules/ec2_vol.py +++ b/plugins/modules/ec2_vol.py @@ -804,7 +804,6 @@ def main(): mapped_device = get_mapped_block_device(instance_dict=inst, device_name=device_name) if mapped_device: other_volume_mapped = False - if volume: if volume['volume_id'] != mapped_device['ebs']['volume_id']: other_volume_mapped = True @@ -823,10 +822,11 @@ def main(): final_tags = None tags_changed = False - if volume: volume, changed = update_volume(module, ec2_conn, volume) if name: + if not tags: + tags = boto3_tag_list_to_ansible_dict(volume.get('tags')) tags['Name'] = name final_tags, tags_changed = ensure_tags(module, ec2_conn, volume['volume_id'], 'volume', tags, module.params.get('purge_tags')) else: diff --git a/tests/integration/targets/ec2_vol/tasks/main.yml b/tests/integration/targets/ec2_vol/tasks/main.yml index c502903d7b4..0b77b1571cc 100644 --- a/tests/integration/targets/ec2_vol/tasks/main.yml +++ b/tests/integration/targets/ec2_vol/tasks/main.yml @@ -61,6 +61,21 @@ that: - test_instance_2.changed + - name: create another ec2 instance + ec2_instance: + name: "{{ instance_name }}-3" + vpc_subnet_id: "{{ testing_subnet.subnet.id }}" + instance_type: t3.nano + image_id: "{{ ec2_ami_id }}" + tags: + ResourcePrefix: "{{ resource_prefix }}" + register: test_instance_3 + + - name: check task return attributes + assert: + that: + - test_instance_3.changed + # # ==== ec2_vol tests =============================================== - name: create a volume (validate module defaults - check_mode) @@ -899,6 +914,28 @@ - 'test_instance.instance_ids[0] in vol_attach_result.volume.attachment_set | map(attribute="instance_id") | list' - 'test_instance_2.instance_ids[0] in vol_attach_result.volume.attachment_set | map(attribute="instance_id") | list' + - name: create a volume without tags + ec2_vol: + volume_size: 5 + zone: "{{ availability_zone }}" + instance: "{{ test_instance_3.instance_ids[0] }}" + register: volume_without_tag + + - assert: + that: + - volume_without_tag.changed + + # idempotency check without tags + - name: create a volume without tags (idempotency check) + ec2_vol: + volume_size: 5 + zone: "{{ availability_zone }}" + instance: "{{ test_instance_3.instance_ids[0] }}" + register: volume_without_tag + + - assert: + that: + - not volume_without_tag.changed # ==== Cleanup ============================================================ always: @@ -910,6 +947,7 @@ with_items: - "{{ test_instance.instance_ids[0] }}" - "{{ test_instance_2.instance_ids[0] }}" + - "{{ test_instance_3.instance_ids[0] }}" register: pre_delete - debug: @@ -924,6 +962,7 @@ with_items: - "{{ test_instance.instance_ids[0] }}" - "{{ test_instance_2.instance_ids[0] }}" + - "{{ test_instance_3.instance_ids[0] }}" ignore_errors: yes - name: delete volumes @@ -940,6 +979,7 @@ - "{{ dot_volume }}" - "{{ gp3_volume }}" - "{{ multi_attach_disk }}" + - "{{ volume_without_tag }}" - name: delete snapshot ec2_snapshot: