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

Tagging - ec2_snapshot_copy #1201

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
3 changes: 3 additions & 0 deletions changelogs/fragments/1201-tagging-ec2_snapshot_copy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
minor_changes:
- ec2_snapshot_copy - ``resource_tags`` has been added as an alias for the ``tags`` parameter (https://github.com/ansible-collections/community.aws/pull/1201).
- ec2_snapshot_copy - updated to pass tags as part of the copy API call rather than tagging the snapshot after creation (https://github.com/ansible-collections/community.aws/pull/1201).
27 changes: 14 additions & 13 deletions plugins/modules/ec2_snapshot_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
---
module: ec2_snapshot_copy
version_added: 1.0.0
short_description: Copies an EC2 snapshot and returns the new Snapshot ID.
short_description: Copies an EC2 snapshot and returns the new Snapshot ID
description:
- Copies an EC2 Snapshot from a source region to a destination region.
- Copies an EC2 Snapshot from a source region to a destination region.
options:
source_region:
description:
Expand All @@ -40,7 +40,7 @@
type: str
wait:
description:
- Wait for the copied Snapshot to be in 'Available' state before returning.
- Wait for the copied Snapshot to be in the C(Available) state before returning.
type: bool
default: 'no'
wait_timeout:
Expand All @@ -50,12 +50,14 @@
type: int
tags:
description:
- A hash/dictionary of tags to add to the new Snapshot; '{"key":"value"}' and '{"key":"value","key":"value"}'
- A dictionary representing the tags to be applied to the newly created resource.
type: dict
author: Deepak Kothandan (@Deepakkothandan) <deepak.kdy@gmail.com>
aliases: ['resource_tags']
author:
- Deepak Kothandan (@Deepakkothandan) <deepak.kdy@gmail.com>
extends_documentation_fragment:
- amazon.aws.aws
- amazon.aws.ec2
- amazon.aws.aws
- amazon.aws.ec2
'''

EXAMPLES = '''
Expand Down Expand Up @@ -112,6 +114,7 @@
pass # Handled by AnsibleAWSModule

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_specifications


def copy_snapshot(module, ec2):
Expand All @@ -134,6 +137,9 @@ def copy_snapshot(module, ec2):
if module.params.get('kms_key_id'):
params['KmsKeyId'] = module.params.get('kms_key_id')

if module.params.get('tags'):
params['TagSpecifications'] = boto3_tag_specifications(module.params.get('tags'))

try:
snapshot_id = ec2.copy_snapshot(**params)['SnapshotId']
if module.params.get('wait'):
Expand All @@ -145,11 +151,6 @@ def copy_snapshot(module, ec2):
SnapshotIds=[snapshot_id],
WaiterConfig=dict(Delay=delay, MaxAttempts=max_attempts)
)
if module.params.get('tags'):
ec2.create_tags(
Resources=[snapshot_id],
Tags=[{'Key': k, 'Value': v} for k, v in module.params.get('tags').items()]
)

except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg='An error occurred waiting for the snapshot to become available.')
Expand All @@ -166,7 +167,7 @@ def main():
kms_key_id=dict(type='str', required=False),
wait=dict(type='bool', default=False),
wait_timeout=dict(type='int', default=600),
tags=dict(type='dict'),
tags=dict(type='dict', aliases=['resource_tags']),
)

module = AnsibleAWSModule(argument_spec=argument_spec)
Expand Down