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 - ensure tags are propagated to the snapshot(s) #437

Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- ec2_ami - ensure tags are propagated to the snapshot(s) when creating an AMI (https://github.com/ansible-collections/amazon.aws/pull/437).
7 changes: 6 additions & 1 deletion plugins/modules/ec2_ami.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,13 @@ def create_image(module, connection):
waiter.wait(ImageIds=[image_id], WaiterConfig=dict(Delay=delay, MaxAttempts=max_attempts))

if tags:
resources_to_tag = [image_id]
image_info = get_image_by_id(module, connection, image_id)
if image_info and image_info.get('BlockDeviceMappings'):
for mapping in image_info.get('BlockDeviceMappings'):
resources_to_tag.append(mapping.get('Ebs').get('SnapshotId'))
try:
connection.create_tags(aws_retry=True, Resources=[image_id], Tags=ansible_dict_to_boto3_tag_list(tags))
connection.create_tags(aws_retry=True, Resources=resources_to_tag, Tags=ansible_dict_to_boto3_tag_list(tags))
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e:
module.fail_json_aws(e, msg="Error tagging image")

Expand Down
12 changes: 12 additions & 0 deletions tests/integration/targets/ec2_ami/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@
- "result.changed"
- "result.image_id.startswith('ami-')"
- "'Name' in result.tags and result.tags.Name == ec2_ami_name + '_ami'"

- name: get related snapshot info and ensure the tags have been propagated
ec2_snapshot_info:
snapshot_ids:
- "{{ result.block_device_mapping['/dev/xvda'].snapshot_id }}"
register: snapshot_result

- name: ensure the tags have been propagated to the snapshot
assert:
that:
- "'tags' in snapshot_result.snapshots[0]"
- "'Name' in snapshot_result.snapshots[0].tags and snapshot_result.snapshots[0].tags.Name == ec2_ami_name + '_ami'"

# ============================================================

Expand Down