Debian Codegraph @

ansible_collections/community/aws/plugins/modules/ec2_snapshot_copy.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2017, Ansible Project
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 DOCUMENTATION = r"""
8 ---
9 module: ec2_snapshot_copy
10 version_added: 1.0.0
11 short_description: Copies an EC2 snapshot and returns the new Snapshot ID
12 description:
13 - Copies an EC2 Snapshot from a source region to a destination region.
14 options:
15 source_region:
16 description:
17 - The source region the Snapshot should be copied from.
18 required: true
19 type: str
20 source_snapshot_id:
21 description:
22 - The ID of the Snapshot in source region that should be copied.
23 required: true
24 type: str
25 description:
26 description:
27 - An optional human-readable string describing purpose of the new Snapshot.
28 type: str
29 default: ''
30 encrypted:
31 description:
32 - Whether or not the destination Snapshot should be encrypted.
33 type: bool
34 default: false
35 kms_key_id:
36 description:
37 - KMS key id used to encrypt snapshot. If not specified, AWS defaults to C(alias/aws/ebs).
38 type: str
39 wait:
40 description:
41 - Wait for the copied Snapshot to be in the C(Available) state before returning.
42 type: bool
43 default: false
44 wait_timeout:
45 description:
46 - How long before wait gives up, in seconds.
47 default: 600
48 type: int
49 tags:
50 description:
51 - A dictionary representing the tags to be applied to the newly created resource.
52 type: dict
53 aliases: ['resource_tags']
54 author:
55 - Deepak Kothandan (@Deepakkothandan) <[email protected]>
56 extends_documentation_fragment:
57 - amazon.aws.common.modules
58 - amazon.aws.region.modules
59 - amazon.aws.boto3
60 """
61
62 EXAMPLES = r"""
63 - name: Basic Snapshot Copy
64 community.aws.ec2_snapshot_copy:
65 source_region: eu-central-1
66 region: eu-west-1
67 source_snapshot_id: snap-xxxxxxx
68
69 - name: Copy Snapshot and wait until available
70 community.aws.ec2_snapshot_copy:
71 source_region: eu-central-1
72 region: eu-west-1
73 source_snapshot_id: snap-xxxxxxx
74 wait: true
75 wait_timeout: 1200 # Default timeout is 600
76 register: snapshot_id
77
78 - name: Tagged Snapshot copy
79 community.aws.ec2_snapshot_copy:
80 source_region: eu-central-1
81 region: eu-west-1
82 source_snapshot_id: snap-xxxxxxx
83 tags:
84 Name: Snapshot-Name
85
86 - name: Encrypted Snapshot copy
87 community.aws.ec2_snapshot_copy:
88 source_region: eu-central-1
89 region: eu-west-1
90 source_snapshot_id: snap-xxxxxxx
91 encrypted: true
92
93 - name: Encrypted Snapshot copy with specified key
94 community.aws.ec2_snapshot_copy:
95 source_region: eu-central-1
96 region: eu-west-1
97 source_snapshot_id: snap-xxxxxxx
98 encrypted: true
99 kms_key_id: arn:aws:kms:eu-central-1:XXXXXXXXXXXX:key/746de6ea-50a4-4bcb-8fbc-e3b29f2d367b
100 """
101
102 RETURN = r"""
103 snapshot_id:
104 description: snapshot id of the newly created snapshot
105 returned: when snapshot copy is successful
106 type: str
107 sample: "snap-e9095e8c"
108 """
109
110 try:
111 import botocore
112 except ImportError:
113 pass # Handled by AnsibleAWSModule
114
115 from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_specifications
116
117 from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule
118
119
120 def copy_snapshot(module, ec2):
121 """
122 Copies an EC2 Snapshot to another region
123
124 module : AnsibleAWSModule object
125 ec2: ec2 connection object
126 """
127
128 params = {
129 "SourceRegion": module.params.get("source_region"),
130 "SourceSnapshotId": module.params.get("source_snapshot_id"),
131 "Description": module.params.get("description"),
132 }
133
134 if module.params.get("encrypted"):
135 params["Encrypted"] = True
136
137 if module.params.get("kms_key_id"):
138 params["KmsKeyId"] = module.params.get("kms_key_id")
139
140 if module.params.get("tags"):
141 params["TagSpecifications"] = boto3_tag_specifications(module.params.get("tags"), types=["snapshot"])
142
143 try:
144 snapshot_id = ec2.copy_snapshot(**params)["SnapshotId"]
145 if module.params.get("wait"):
146 delay = 15
147 # Add one to max_attempts as wait() increment
148 # its counter before assessing it for time.sleep()
149 max_attempts = (module.params.get("wait_timeout") // delay) + 1
150 ec2.get_waiter("snapshot_completed").wait(
151 SnapshotIds=[snapshot_id], WaiterConfig=dict(Delay=delay, MaxAttempts=max_attempts)
152 )
153
154 except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
155 module.fail_json_aws(e, msg="An error occurred waiting for the snapshot to become available.")
156
157 module.exit_json(changed=True, snapshot_id=snapshot_id)
158
159
160 def main():
161 argument_spec = dict(
162 source_region=dict(required=True),
163 source_snapshot_id=dict(required=True),
164 description=dict(default=""),
165 encrypted=dict(type="bool", default=False, required=False),
166 kms_key_id=dict(type="str", required=False),
167 wait=dict(type="bool", default=False),
168 wait_timeout=dict(type="int", default=600),
169 tags=dict(type="dict", aliases=["resource_tags"]),
170 )
171
172 module = AnsibleAWSModule(argument_spec=argument_spec)
173
174 try:
175 client = module.client("ec2")
176 except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
177 module.fail_json_aws(e, msg="Failed to connect to AWS")
178
179 copy_snapshot(module, client)
180
181
182 if __name__ == "__main__":
183 main()