From 48d842412366193706e5111add1fb7898ad48bd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A9ri=20Le=20Bouder?= Date: Fri, 15 Jan 2021 13:10:01 -0500 Subject: [PATCH] ec2_vol: preset the name tag of the volume Closes: https://github.com/ansible-collections/amazon.aws/issues/229 --- .../242_ec2_vol-preset-the-name-tag-of-the-volume.yaml | 3 +++ plugins/modules/ec2_vol.py | 5 +++-- tests/integration/targets/ec2_vol/tasks/tests.yml | 6 ++++++ 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/242_ec2_vol-preset-the-name-tag-of-the-volume.yaml diff --git a/changelogs/fragments/242_ec2_vol-preset-the-name-tag-of-the-volume.yaml b/changelogs/fragments/242_ec2_vol-preset-the-name-tag-of-the-volume.yaml new file mode 100644 index 00000000000..efb2a72e676 --- /dev/null +++ b/changelogs/fragments/242_ec2_vol-preset-the-name-tag-of-the-volume.yaml @@ -0,0 +1,3 @@ +--- +bugfixes: +- ec2_vol - create or update now preserves the existing tags, including Name (https://github.com/ansible-collections/amazon.aws/issues/229) diff --git a/plugins/modules/ec2_vol.py b/plugins/modules/ec2_vol.py index 794e72a5299..c0df95b9697 100644 --- a/plugins/modules/ec2_vol.py +++ b/plugins/modules/ec2_vol.py @@ -781,8 +781,9 @@ def main(): else: volume, changed = create_volume(module, ec2_conn, zone=zone) - tags['Name'] = name - final_tags, tags_changed = ensure_tags(module, ec2_conn, volume['volume_id'], 'volume', tags, False) + if name: + tags['Name'] = name + final_tags, tags_changed = ensure_tags(module, ec2_conn, volume['volume_id'], 'volume', tags, True) if detach_vol_flag: volume, changed = detach_volume(module, ec2_conn, volume_dict=volume) diff --git a/tests/integration/targets/ec2_vol/tasks/tests.yml b/tests/integration/targets/ec2_vol/tasks/tests.yml index d7d5bf3c684..346e8590a7b 100644 --- a/tests/integration/targets/ec2_vol/tasks/tests.yml +++ b/tests/integration/targets/ec2_vol/tasks/tests.yml @@ -74,6 +74,7 @@ - "'attachment_set' in volume1.volume" - "'instance_id' in volume1.volume.attachment_set" - not volume1.volume.attachment_set.instance_id + - not ("Name" in volume1.volume.tags) - not volume1.volume.encrypted # no idempotency check needed here @@ -102,6 +103,7 @@ - volume2.volume_type == 'io1' - volume2.volume.iops == 101 - volume2.volume.size == 4 + - volume2.volume.tags.Name == "{{ resource_prefix }}" - volume2.volume.encrypted - name: create another volume (override module defaults) (idempotent) @@ -208,6 +210,7 @@ volume_size: 1 volume_type: gp2 tags: + my_tag: foo ResourcePrefix: "{{ resource_prefix }}" register: new_vol_attach_result @@ -220,6 +223,7 @@ - new_vol_attach_result.volume.attachment_set.status in ['attached', 'attaching'] - new_vol_attach_result.volume.attachment_set.instance_id == test_instance.instance_ids[0] - new_vol_attach_result.volume.attachment_set.device == '/dev/sdh' + - new_vol_attach_result.volume.tags.my_tag == 'foo' - name: attach a new volume to an instance (idempotent) ec2_vol: @@ -337,6 +341,8 @@ that: - changed_gp3_volume.volume_type == 'gp3' - changed_gp3_volume.changed + # Ensure our tag is still here + - changed_gp3_volume.volume.tags.my_tag == 'foo' - name: volume must be from type gp3 (idempotent) ec2_vol: