Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ec2_ami - Tag the image on creation when creating an image from an instance #551

Merged
merged 1 commit into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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).
7 changes: 5 additions & 2 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 and module.botocore_at_least('1.19.30'):
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,7 +527,7 @@ 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)
if image_info and image_info.get('BlockDeviceMappings'):
Expand Down