From 788066eae26048e3a7109163b8e45e6a377b8a48 Mon Sep 17 00:00:00 2001 From: Mark Chappell Date: Tue, 26 Oct 2021 11:55:46 +0200 Subject: [PATCH] ec2_ami - Tag the image on creation when creating an image from an instance (#551) ec2_ami - Tag the image on creation when creating an image from an instance SUMMARY Tagging an instance during creation avoids the need to make an additional "tag" call on an untagged resource. ISSUE TYPE Feature Pull Request COMPONENT NAME ec2_ami ADDITIONAL INFORMATION fixes: #550 Reviewed-by: Andy Thompson Reviewed-by: Mark Chappell Reviewed-by: Alina Buzachis Reviewed-by: None --- changelogs/551-ec2_ami-tag-on-create.yml | 2 ++ plugins/modules/ec2_ami.py | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 changelogs/551-ec2_ami-tag-on-create.yml diff --git a/changelogs/551-ec2_ami-tag-on-create.yml b/changelogs/551-ec2_ami-tag-on-create.yml new file mode 100644 index 00000000000..22403e449d7 --- /dev/null +++ b/changelogs/551-ec2_ami-tag-on-create.yml @@ -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). diff --git a/plugins/modules/ec2_ami.py b/plugins/modules/ec2_ami.py index 5b13cbbf8a8..56ed9cc940a 100644 --- a/plugins/modules/ec2_ami.py +++ b/plugins/modules/ec2_ami.py @@ -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 @@ -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: @@ -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'):