From 5f0b044eb5c49d49c2a13de3478a30ccfc6ae12c Mon Sep 17 00:00:00 2001 From: Mark Chappell Date: Thu, 2 Jun 2022 09:54:26 +0200 Subject: [PATCH] Tagging - cleanup docs for ec2_snapshot_copy --- .../1201-tagging-ec2_snapshot_copy.yml | 3 +++ plugins/modules/ec2_snapshot_copy.py | 27 ++++++++++--------- 2 files changed, 17 insertions(+), 13 deletions(-) create mode 100644 changelogs/fragments/1201-tagging-ec2_snapshot_copy.yml diff --git a/changelogs/fragments/1201-tagging-ec2_snapshot_copy.yml b/changelogs/fragments/1201-tagging-ec2_snapshot_copy.yml new file mode 100644 index 00000000000..976b69d9957 --- /dev/null +++ b/changelogs/fragments/1201-tagging-ec2_snapshot_copy.yml @@ -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). diff --git a/plugins/modules/ec2_snapshot_copy.py b/plugins/modules/ec2_snapshot_copy.py index 5ad307dd693..2d0d40546e7 100644 --- a/plugins/modules/ec2_snapshot_copy.py +++ b/plugins/modules/ec2_snapshot_copy.py @@ -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: @@ -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: @@ -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) + aliases: ['resource_tags'] +author: + - Deepak Kothandan (@Deepakkothandan) extends_documentation_fragment: -- amazon.aws.aws -- amazon.aws.ec2 + - amazon.aws.aws + - amazon.aws.ec2 ''' EXAMPLES = ''' @@ -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): @@ -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'): @@ -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.') @@ -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)