Skip to content

Commit

Permalink
ec2_ami - Tag the image on creation when creating an image from an in…
Browse files Browse the repository at this point in the history
…stance
  • Loading branch information
tremble committed Oct 24, 2021
1 parent 5152b6e commit bbd4782
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 2 additions & 0 deletions changelogs/551-ec2_ami-tag-on-create.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- ec2_ami - when creating an AMI from an instance pass the tagging options at creation time (https://github.com/ansible-collections/amazon.aws/pull/551).
11 changes: 7 additions & 4 deletions plugins/modules/ec2_ami.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,10 @@
from ..module_utils.core import AnsibleAWSModule
from ..module_utils.core import is_boto3_error_code
from ..module_utils.ec2 import AWSRetry
from ..module_utils.ec2 import boto3_tag_list_to_ansible_dict
from ..module_utils.ec2 import ensure_ec2_tags
from ..module_utils.ec2 import add_ec2_tags
from ..module_utils.tagging import boto3_tag_list_to_ansible_dict
from ..module_utils.tagging import boto3_tag_specifications
from ..module_utils.waiters import get_waiter


Expand Down Expand Up @@ -494,6 +495,8 @@ def create_image(module, connection):
if instance_id:
params['InstanceId'] = instance_id
params['NoReboot'] = no_reboot
if tags:
params['TagSpecifications'] = boto3_tag_specifications(tags, types=['image', 'snapshot'])
image_id = connection.create_image(aws_retry=True, **params).get('ImageId')
else:
if architecture:
Expand Down Expand Up @@ -524,15 +527,15 @@ def create_image(module, connection):
waiter = get_waiter(connection, 'image_available')
waiter.wait(ImageIds=[image_id], WaiterConfig=dict(Delay=delay, MaxAttempts=max_attempts))

if tags:
if tags and 'TagSpecifications' not in params:
image_info = get_image_by_id(module, connection, image_id)
add_ec2_tags(connection, module, image_id, tags)
ec2_tags(connection, module, image_id, tags=tags)
if image_info and image_info.get('BlockDeviceMappings'):
for mapping in image_info.get('BlockDeviceMappings'):
# We can only tag Ebs volumes
if 'Ebs' not in mapping:
continue
add_ec2_tags(connection, module, mapping.get('Ebs').get('SnapshotId'), tags)
add_ec2_tags(connection, module, mapping.get('Ebs').get('SnapshotId'), tags=tags)

if launch_permissions:
try:
Expand Down

0 comments on commit bbd4782

Please sign in to comment.