Skip to content

Commit

Permalink
Tag EC2 volume on creation (ansible-collections#604)
Browse files Browse the repository at this point in the history
Tag EC2 volume on creation

SUMMARY

Tag EC2 volume on creation using TagSpecifications.
Fixes: ansible-collections#596

ISSUE TYPE


Feature Pull Request

COMPONENT NAME

ec2_vol

Reviewed-by: Jill R <None>
Reviewed-by: None <None>
  • Loading branch information
alinabuzachis authored Jan 12, 2022
1 parent 7bdad10 commit e6b0e71
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/603-ec2_vol-add-tags-on-creation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- ec2_vol - tag volume on creation (https://github.com/ansible-collections/amazon.aws/pull/603).
21 changes: 17 additions & 4 deletions plugins/modules/ec2_vol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit e6b0e71

Please sign in to comment.