Skip to content

Commit

Permalink
ec2_vol: preserve the name tag of the volume (#242)
Browse files Browse the repository at this point in the history
* ec2_vol: preset the name tag of the volume
* ec2_vol - add purge_tags parameter
  • Loading branch information
goneri authored Mar 12, 2021
1 parent 9907865 commit 6d0a274
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- ec2_vol - add the ``purge_tags`` option (https://github.com/ansible-collections/amazon.aws/pull/242).
Original file line number Diff line number Diff line change
@@ -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)
19 changes: 14 additions & 5 deletions plugins/modules/ec2_vol.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@
- tag:value pairs to add to the volume after creation.
default: {}
type: dict
purge_tags:
description: Whether to remove existing tags that aren't passed in the I(tags) parameter
default: false
type: bool
version_added: 1.5.0
modify_volume:
description:
- The volume won't be modify unless this key is C(true).
Expand Down Expand Up @@ -606,7 +611,7 @@ def get_mapped_block_device(instance_dict=None, device_name=None):
return mapped_block_device


def ensure_tags(module, connection, res_id, res_type, tags, add_only):
def ensure_tags(module, connection, res_id, res_type, tags, purge_tags):
changed = False

filters = ansible_dict_to_boto3_filter_list({'resource-id': res_id, 'resource-type': res_type})
Expand All @@ -616,7 +621,6 @@ def ensure_tags(module, connection, res_id, res_type, tags, add_only):
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg="Couldn't describe tags")

purge_tags = bool(not add_only)
to_update, to_delete = compare_aws_tags(boto3_tag_list_to_ansible_dict(cur_tags.get('Tags')), tags, purge_tags)
final_tags = boto3_tag_list_to_ansible_dict(cur_tags.get('Tags'))

Expand Down Expand Up @@ -680,7 +684,8 @@ def main():
state=dict(default='present', choices=['absent', 'present', 'list']),
tags=dict(default={}, type='dict'),
modify_volume=dict(default=False, type='bool'),
throughput=dict(type='int')
throughput=dict(type='int'),
purge_tags=dict(type='bool', default=False),
)

module = AnsibleAWSModule(argument_spec=argument_spec)
Expand Down Expand Up @@ -777,8 +782,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, module.params.get('purge_tags'))

if detach_vol_flag:
volume, changed = detach_volume(module, ec2_conn, volume_dict=volume)
Expand All @@ -788,6 +794,9 @@ def main():
# Add device, volume_id and volume_type parameters separately to maintain backward compatibility
volume_info = get_volume_info(volume, tags=final_tags)

if tags_changed:
changed = True

module.exit_json(changed=changed, volume=volume_info, device=volume_info['attachment_set']['device'],
volume_id=volume_info['id'], volume_type=volume_info['type'])
elif state == 'absent':
Expand Down
44 changes: 44 additions & 0 deletions tests/integration/targets/ec2_vol/tasks/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
- volume1.volume.tags.ResourcePrefix == "{{ resource_prefix }}"

Expand Down Expand Up @@ -103,6 +104,7 @@
- volume2.volume_type == 'io1'
- volume2.volume.iops == 101
- volume2.volume.size == 4
- volume2.volume.tags.Name == "{{ resource_prefix }}"
- volume2.volume.encrypted
- volume2.volume.tags.ResourcePrefix == "{{ resource_prefix }}"

Expand Down Expand Up @@ -209,7 +211,12 @@
device_name: /dev/sdh
volume_size: 1
volume_type: gp2
name: '{{ resource_prefix }} - sdh'
tags:
"lowercase spaced": 'hello cruel world'
"Title Case": 'Hello Cruel World'
CamelCase: 'SimpleCamelCase'
snake_case: 'simple_snake_case'
ResourcePrefix: "{{ resource_prefix }}"
register: new_vol_attach_result

Expand All @@ -222,6 +229,12 @@
- 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["lowercase spaced"] == 'hello cruel world'
- new_vol_attach_result.volume.tags["Title Case"] == 'Hello Cruel World'
- new_vol_attach_result.volume.tags["CamelCase"] == 'SimpleCamelCase'
- new_vol_attach_result.volume.tags["snake_case"] == 'simple_snake_case'
- new_vol_attach_result.volume.tags["Name"] == '{{ resource_prefix }} - sdh'


- name: attach a new volume to an instance (idempotent)
ec2_vol:
Expand All @@ -240,6 +253,32 @@
- "not new_vol_attach_result_idem.changed"
- "'Volume mapping for /dev/sdh already exists' in new_vol_attach_result_idem.msg"

- name: change some tag values
ec2_vol:
instance: "{{ test_instance.instance_ids[0] }}"
id: "{{ new_vol_attach_result.volume.id }}"
device_name: /dev/sdh
volume_size: 1
volume_type: gp2
tags:
"lowercase spaced": 'hello cruel world ❤️'
"Title Case": 'Hello Cruel World ❤️'
CamelCase: 'SimpleCamelCase ❤️'
snake_case: 'simple_snake_case ❤️'
register: new_vol_attach_result

- name: check task return attributes
assert:
that:
- new_vol_attach_result.changed
- new_vol_attach_result.volume.tags["lowercase spaced"] == 'hello cruel world ❤️'
- new_vol_attach_result.volume.tags["Title Case"] == 'Hello Cruel World ❤️'
- new_vol_attach_result.volume.tags["CamelCase"] == 'SimpleCamelCase ❤️'
- new_vol_attach_result.volume.tags["snake_case"] == 'simple_snake_case ❤️'
- new_vol_attach_result.volume.tags["ResourcePrefix"] == resource_prefix
- new_vol_attach_result.volume.tags["Name"] == '{{ resource_prefix }} - sdh'


- name: create a volume from a snapshot and attach to the instance
ec2_vol:
instance: "{{ test_instance.instance_ids[0] }}"
Expand Down Expand Up @@ -339,6 +378,11 @@
that:
- changed_gp3_volume.volume_type == 'gp3'
- changed_gp3_volume.changed
# Ensure our tags are still here
- new_vol_attach_result.volume.tags["lowercase spaced"] == 'hello cruel world ❤️'
- new_vol_attach_result.volume.tags["Title Case"] == 'Hello Cruel World ❤️'
- new_vol_attach_result.volume.tags["CamelCase"] == 'SimpleCamelCase ❤️'
- new_vol_attach_result.volume.tags["snake_case"] == 'simple_snake_case ❤️'

- name: volume must be from type gp3 (idempotent)
ec2_vol:
Expand Down

0 comments on commit 6d0a274

Please sign in to comment.