Skip to content

Commit

Permalink
Handle ec2_vol.tags when the associated instance already exists (#1071)…
Browse files Browse the repository at this point in the history
… (#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
  • Loading branch information
patchback[bot] committed Mar 13, 2023
1 parent ece9c05 commit 5b179e9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/1071-ec2_vol_tags_idempotent.yaml
Original file line number Diff line number Diff line change
@@ -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).
4 changes: 2 additions & 2 deletions plugins/modules/ec2_vol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
40 changes: 40 additions & 0 deletions tests/integration/targets/ec2_vol/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -940,6 +979,7 @@
- "{{ dot_volume }}"
- "{{ gp3_volume }}"
- "{{ multi_attach_disk }}"
- "{{ volume_without_tag }}"

- name: delete snapshot
ec2_snapshot:
Expand Down

0 comments on commit 5b179e9

Please sign in to comment.