diff --git a/changelogs/fragments/603-ec2_vol-add-tags-on-creation.yml b/changelogs/fragments/603-ec2_vol-add-tags-on-creation.yml new file mode 100644 index 00000000000..256616a8bbc --- /dev/null +++ b/changelogs/fragments/603-ec2_vol-add-tags-on-creation.yml @@ -0,0 +1,2 @@ +minor_changes: + - ec2_vol - tag volume on creation (https://github.com/ansible-collections/amazon.aws/pull/603). \ No newline at end of file diff --git a/plugins/modules/ec2_vol.py b/plugins/modules/ec2_vol.py index cea01de96ff..8c67cfa48fe 100644 --- a/plugins/modules/ec2_vol.py +++ b/plugins/modules/ec2_vol.py @@ -264,6 +264,8 @@ from ..module_utils.ec2 import ensure_ec2_tags from ..module_utils.ec2 import AWSRetry from ..module_utils.core import is_boto3_error_code +from ..module_utils.tagging import boto3_tag_specifications + try: import botocore @@ -455,6 +457,8 @@ def create_volume(module, ec2_conn, zone): snapshot = module.params.get('snapshot') throughput = module.params.get('throughput') multi_attach = module.params.get('multi_attach') + tags = module.params.get('tags') + name = module.params.get('name') volume = get_volume(module, ec2_conn) @@ -485,9 +489,16 @@ def create_volume(module, ec2_conn, zone): if throughput: additional_params['Throughput'] = int(throughput) + if multi_attach: additional_params['MultiAttachEnabled'] = True + if name: + tags['Name'] = name + + if tags: + additional_params['TagSpecifications'] = boto3_tag_specifications(tags, types=['volume']) + create_vol_response = ec2_conn.create_volume( aws_retry=True, AvailabilityZone=zone, @@ -824,15 +835,17 @@ def main(): changed=False ) + final_tags = None + tags_changed = False + if volume: volume, changed = update_volume(module, ec2_conn, volume) + if name: + tags['Name'] = name + final_tags, tags_changed = ensure_tags(module, ec2_conn, volume['volume_id'], 'volume', tags, module.params.get('purge_tags')) else: volume, changed = create_volume(module, ec2_conn, zone=zone) - 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, attach_changed = detach_volume(module, ec2_conn, volume_dict=volume) elif inst is not None: