Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PR #1071/ffd06e97 backport][stable-5] Handle ec2_vol.tags when the associated instance already exists #1423

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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