| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2014 Ansible Project |
| 5 |
# Copyright (c) 2017, 2018, 2019 Will Thames |
| 6 |
# Copyright (c) 2017, 2018 Michael De La Rue |
| 7 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
--- |
| 11 |
module: rds_instance_snapshot |
| 12 |
version_added: 5.0.0 |
| 13 |
short_description: Manage Amazon RDS instance snapshots |
| 14 |
description: |
| 15 |
- Creates or deletes RDS snapshots. |
| 16 |
- This module was originally added to C(community.aws) in release 1.0.0. |
| 17 |
options: |
| 18 |
state: |
| 19 |
description: |
| 20 |
- Specify the desired state of the snapshot. |
| 21 |
default: present |
| 22 |
choices: [ 'present', 'absent'] |
| 23 |
type: str |
| 24 |
db_snapshot_identifier: |
| 25 |
description: |
| 26 |
- The snapshot to manage. |
| 27 |
required: true |
| 28 |
aliases: |
| 29 |
- id |
| 30 |
- snapshot_id |
| 31 |
type: str |
| 32 |
db_instance_identifier: |
| 33 |
description: |
| 34 |
- Database instance identifier. Required when creating a snapshot. |
| 35 |
aliases: |
| 36 |
- instance_id |
| 37 |
type: str |
| 38 |
source_db_snapshot_identifier: |
| 39 |
description: |
| 40 |
- The identifier of the source DB snapshot. |
| 41 |
- Required when copying a snapshot. |
| 42 |
- If the source snapshot is in the same AWS region as the copy, specify the snapshot's identifier. |
| 43 |
- If the source snapshot is in a different AWS region as the copy, specify the snapshot's ARN. |
| 44 |
aliases: |
| 45 |
- source_id |
| 46 |
- source_snapshot_id |
| 47 |
type: str |
| 48 |
version_added: 3.3.0 |
| 49 |
version_added_collection: community.aws |
| 50 |
source_region: |
| 51 |
description: |
| 52 |
- The region that contains the snapshot to be copied. |
| 53 |
type: str |
| 54 |
version_added: 3.3.0 |
| 55 |
version_added_collection: community.aws |
| 56 |
copy_tags: |
| 57 |
description: |
| 58 |
- Whether to copy all tags from O(source_db_snapshot_identifier) to O(db_instance_identifier). |
| 59 |
type: bool |
| 60 |
default: false |
| 61 |
version_added: 3.3.0 |
| 62 |
version_added_collection: community.aws |
| 63 |
wait: |
| 64 |
description: |
| 65 |
- Whether or not to wait for snapshot creation or deletion. |
| 66 |
type: bool |
| 67 |
default: false |
| 68 |
wait_timeout: |
| 69 |
description: |
| 70 |
- how long before wait gives up, in seconds. |
| 71 |
default: 300 |
| 72 |
type: int |
| 73 |
author: |
| 74 |
- "Will Thames (@willthames)" |
| 75 |
- "Michael De La Rue (@mikedlr)" |
| 76 |
- "Alina Buzachis (@alinabuzachis)" |
| 77 |
- "Joseph Torcasso (@jatorcasso)" |
| 78 |
extends_documentation_fragment: |
| 79 |
- amazon.aws.common.modules |
| 80 |
- amazon.aws.region.modules |
| 81 |
- amazon.aws.tags |
| 82 |
- amazon.aws.boto3 |
| 83 |
""" |
| 84 |
|
| 85 |
EXAMPLES = r""" |
| 86 |
- name: Create snapshot |
| 87 |
amazon.aws.rds_instance_snapshot: |
| 88 |
db_instance_identifier: new-database |
| 89 |
db_snapshot_identifier: new-database-snapshot |
| 90 |
register: snapshot |
| 91 |
|
| 92 |
- name: Copy snapshot from a different region and copy its tags |
| 93 |
amazon.aws.rds_instance_snapshot: |
| 94 |
id: new-database-snapshot-copy |
| 95 |
region: us-east-1 |
| 96 |
source_id: "{{ snapshot.db_snapshot_arn }}" |
| 97 |
source_region: us-east-2 |
| 98 |
copy_tags: true |
| 99 |
|
| 100 |
- name: Delete snapshot |
| 101 |
amazon.aws.rds_instance_snapshot: |
| 102 |
db_snapshot_identifier: new-database-snapshot |
| 103 |
state: absent |
| 104 |
""" |
| 105 |
|
| 106 |
RETURN = r""" |
| 107 |
allocated_storage: |
| 108 |
description: How much storage is allocated in GB. |
| 109 |
returned: always |
| 110 |
type: int |
| 111 |
sample: 20 |
| 112 |
availability_zone: |
| 113 |
description: Availability zone of the database from which the snapshot was created. |
| 114 |
returned: always |
| 115 |
type: str |
| 116 |
sample: us-west-2a |
| 117 |
db_instance_identifier: |
| 118 |
description: Database from which the snapshot was created. |
| 119 |
returned: always |
| 120 |
type: str |
| 121 |
sample: ansible-test-16638696 |
| 122 |
db_snapshot_arn: |
| 123 |
description: Amazon Resource Name for the snapshot. |
| 124 |
returned: always |
| 125 |
type: str |
| 126 |
sample: arn:aws:rds:us-west-2:123456789012:snapshot:ansible-test-16638696-test-snapshot |
| 127 |
db_snapshot_identifier: |
| 128 |
description: Name of the snapshot. |
| 129 |
returned: always |
| 130 |
type: str |
| 131 |
sample: ansible-test-16638696-test-snapshot |
| 132 |
dbi_resource_id: |
| 133 |
description: The identifier for the source DB instance, which can't be changed and which is unique to an AWS Region. |
| 134 |
returned: always |
| 135 |
type: str |
| 136 |
sample: db-MM4P2U35RQRAMWD3QDOXWPZP4U |
| 137 |
encrypted: |
| 138 |
description: Whether the snapshot is encrypted. |
| 139 |
returned: always |
| 140 |
type: bool |
| 141 |
sample: false |
| 142 |
engine: |
| 143 |
description: Engine of the database from which the snapshot was created. |
| 144 |
returned: always |
| 145 |
type: str |
| 146 |
sample: mariadb |
| 147 |
engine_version: |
| 148 |
description: Version of the database from which the snapshot was created. |
| 149 |
returned: always |
| 150 |
type: str |
| 151 |
sample: 10.2.21 |
| 152 |
iam_database_authentication_enabled: |
| 153 |
description: Whether IAM database authentication is enabled. |
| 154 |
returned: always |
| 155 |
type: bool |
| 156 |
sample: false |
| 157 |
instance_create_time: |
| 158 |
description: Creation time of the instance from which the snapshot was created. |
| 159 |
returned: always |
| 160 |
type: str |
| 161 |
sample: '2019-06-15T10:15:56.221000+00:00' |
| 162 |
license_model: |
| 163 |
description: License model of the database. |
| 164 |
returned: always |
| 165 |
type: str |
| 166 |
sample: general-public-license |
| 167 |
master_username: |
| 168 |
description: Master username of the database. |
| 169 |
returned: always |
| 170 |
type: str |
| 171 |
sample: test |
| 172 |
option_group_name: |
| 173 |
description: Option group of the database. |
| 174 |
returned: always |
| 175 |
type: str |
| 176 |
sample: default:mariadb-10-2 |
| 177 |
percent_progress: |
| 178 |
description: How much progress has been made taking the snapshot. Will be 100 for an available snapshot. |
| 179 |
returned: always |
| 180 |
type: int |
| 181 |
sample: 100 |
| 182 |
port: |
| 183 |
description: Port on which the database is listening. |
| 184 |
returned: always |
| 185 |
type: int |
| 186 |
sample: 3306 |
| 187 |
processor_features: |
| 188 |
description: List of processor features of the database. |
| 189 |
returned: always |
| 190 |
type: list |
| 191 |
sample: [] |
| 192 |
source_db_snapshot_identifier: |
| 193 |
description: The DB snapshot ARN that the DB snapshot was copied from. |
| 194 |
returned: when snapshot is a copy |
| 195 |
type: str |
| 196 |
sample: arn:aws:rds:us-west-2:123456789012:snapshot:ansible-test-16638696-test-snapshot-source |
| 197 |
version_added: 3.3.0 |
| 198 |
version_added_collection: community.aws |
| 199 |
snapshot_create_time: |
| 200 |
description: Creation time of the snapshot. |
| 201 |
returned: always |
| 202 |
type: str |
| 203 |
sample: '2019-06-15T10:46:23.776000+00:00' |
| 204 |
snapshot_type: |
| 205 |
description: How the snapshot was created (always manual for this module!). |
| 206 |
returned: always |
| 207 |
type: str |
| 208 |
sample: manual |
| 209 |
status: |
| 210 |
description: Status of the snapshot. |
| 211 |
returned: always |
| 212 |
type: str |
| 213 |
sample: available |
| 214 |
storage_type: |
| 215 |
description: Storage type of the database. |
| 216 |
returned: always |
| 217 |
type: str |
| 218 |
sample: gp2 |
| 219 |
tags: |
| 220 |
description: Tags applied to the snapshot. |
| 221 |
returned: always |
| 222 |
type: complex |
| 223 |
contains: {} |
| 224 |
vpc_id: |
| 225 |
description: ID of the VPC in which the DB lives. |
| 226 |
returned: always |
| 227 |
type: str |
| 228 |
sample: vpc-09ff232e222710ae0 |
| 229 |
""" |
| 230 |
|
| 231 |
from typing import Any |
| 232 |
from typing import Dict |
| 233 |
|
| 234 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 235 |
|
| 236 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 237 |
from ansible_collections.amazon.aws.plugins.module_utils.rds import AnsibleRDSError |
| 238 |
from ansible_collections.amazon.aws.plugins.module_utils.rds import arg_spec_to_rds_params |
| 239 |
from ansible_collections.amazon.aws.plugins.module_utils.rds import call_method |
| 240 |
from ansible_collections.amazon.aws.plugins.module_utils.rds import ensure_tags |
| 241 |
from ansible_collections.amazon.aws.plugins.module_utils.rds import format_rds_client_method_parameters |
| 242 |
from ansible_collections.amazon.aws.plugins.module_utils.rds import get_snapshot |
| 243 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 244 |
|
| 245 |
|
| 246 |
def ensure_snapshot_absent(client, module: AnsibleAWSModule) -> None: |
| 247 |
snapshot_id = module.params.get("db_snapshot_identifier") |
| 248 |
params = {"DBSnapshotIdentifier": snapshot_id} |
| 249 |
changed = False |
| 250 |
|
| 251 |
try: |
| 252 |
snapshot = get_snapshot(client, snapshot_id, "instance") |
| 253 |
except AnsibleRDSError as e: |
| 254 |
module.fail_json_aws(e, msg=f"Failed to get snapshot: {snapshot_id}") |
| 255 |
if not snapshot: |
| 256 |
module.exit_json(changed=changed) |
| 257 |
elif snapshot and snapshot["Status"] != "deleting": |
| 258 |
snapshot, changed = call_method(client, module, "delete_db_snapshot", params) |
| 259 |
|
| 260 |
module.exit_json(changed=changed) |
| 261 |
|
| 262 |
|
| 263 |
def ensure_snapshot_present(client, module: AnsibleAWSModule, params: Dict[str, Any]) -> None: |
| 264 |
source_id = module.params.get("source_db_snapshot_identifier") |
| 265 |
snapshot_id = module.params.get("db_snapshot_identifier") |
| 266 |
changed = False |
| 267 |
|
| 268 |
try: |
| 269 |
snapshot = get_snapshot(client, snapshot_id, "instance") |
| 270 |
except AnsibleRDSError as e: |
| 271 |
module.fail_json_aws(e, msg=f"Failed to get snapshot: {snapshot_id}") |
| 272 |
|
| 273 |
# Copy snapshot |
| 274 |
if source_id: |
| 275 |
changed |= copy_snapshot(client, module, params) |
| 276 |
|
| 277 |
# Create snapshot |
| 278 |
elif not snapshot: |
| 279 |
changed |= create_snapshot(client, module, params) |
| 280 |
|
| 281 |
# Snapshot exists and we're not creating a copy - modify exising snapshot |
| 282 |
else: |
| 283 |
changed |= modify_snapshot(client, module) |
| 284 |
|
| 285 |
try: |
| 286 |
snapshot = get_snapshot(client, snapshot_id, "instance") |
| 287 |
except AnsibleRDSError as e: |
| 288 |
module.fail_json_aws(e, msg=f"Failed to get snapshot: {snapshot_id}") |
| 289 |
module.exit_json(changed=changed, **camel_dict_to_snake_dict(snapshot, ignore_list=["Tags"])) |
| 290 |
|
| 291 |
|
| 292 |
def create_snapshot(client, module: AnsibleAWSModule, params: Dict[str, Any]) -> bool: |
| 293 |
method_params = format_rds_client_method_parameters(client, module, params, "create_db_snapshot", format_tags=True) |
| 294 |
_snapshot, changed = call_method(client, module, "create_db_snapshot", method_params) |
| 295 |
|
| 296 |
return changed |
| 297 |
|
| 298 |
|
| 299 |
def copy_snapshot(client, module: AnsibleAWSModule, params: Dict[str, Any]) -> bool: |
| 300 |
changed = False |
| 301 |
snapshot_id = module.params.get("db_snapshot_identifier") |
| 302 |
try: |
| 303 |
snapshot = get_snapshot(client, snapshot_id, "instance") |
| 304 |
except AnsibleRDSError as e: |
| 305 |
module.fail_json_aws(e, msg=f"Failed to get snapshot: {snapshot_id}") |
| 306 |
|
| 307 |
if not snapshot: |
| 308 |
params["TargetDBSnapshotIdentifier"] = module.params["db_snapshot_identifier"] |
| 309 |
method_params = format_rds_client_method_parameters( |
| 310 |
client, module, params, "copy_db_snapshot", format_tags=True |
| 311 |
) |
| 312 |
_result, changed = call_method(client, module, "copy_db_snapshot", method_params) |
| 313 |
|
| 314 |
return changed |
| 315 |
|
| 316 |
|
| 317 |
def modify_snapshot(client, module: AnsibleAWSModule) -> bool: |
| 318 |
# TODO - add other modifications aside from purely tags |
| 319 |
changed = False |
| 320 |
snapshot_id = module.params.get("db_snapshot_identifier") |
| 321 |
try: |
| 322 |
snapshot = get_snapshot(client, snapshot_id, "instance") |
| 323 |
except AnsibleRDSError as e: |
| 324 |
module.fail_json_aws(e, msg=f"Failed to get snapshot: {snapshot_id}") |
| 325 |
|
| 326 |
if module.params.get("tags"): |
| 327 |
changed |= ensure_tags( |
| 328 |
client, |
| 329 |
module, |
| 330 |
snapshot["DBSnapshotArn"], |
| 331 |
snapshot["Tags"], |
| 332 |
module.params["tags"], |
| 333 |
module.params["purge_tags"], |
| 334 |
) |
| 335 |
|
| 336 |
return changed |
| 337 |
|
| 338 |
|
| 339 |
def main(): |
| 340 |
argument_spec = dict( |
| 341 |
state=dict(choices=["present", "absent"], default="present"), |
| 342 |
db_snapshot_identifier=dict(aliases=["id", "snapshot_id"], required=True), |
| 343 |
db_instance_identifier=dict(aliases=["instance_id"]), |
| 344 |
source_db_snapshot_identifier=dict(aliases=["source_id", "source_snapshot_id"]), |
| 345 |
wait=dict(type="bool", default=False), |
| 346 |
wait_timeout=dict(type="int", default=300), |
| 347 |
tags=dict(type="dict", aliases=["resource_tags"]), |
| 348 |
purge_tags=dict(type="bool", default=True), |
| 349 |
copy_tags=dict(type="bool", default=False), |
| 350 |
source_region=dict(type="str"), |
| 351 |
) |
| 352 |
|
| 353 |
module = AnsibleAWSModule( |
| 354 |
argument_spec=argument_spec, |
| 355 |
supports_check_mode=True, |
| 356 |
) |
| 357 |
client = module.client("rds", retry_decorator=AWSRetry.jittered_backoff()) |
| 358 |
|
| 359 |
state = module.params.get("state") |
| 360 |
if state == "absent": |
| 361 |
ensure_snapshot_absent(client, module) |
| 362 |
|
| 363 |
elif state == "present": |
| 364 |
params = arg_spec_to_rds_params(dict((k, module.params[k]) for k in module.params if k in argument_spec)) |
| 365 |
ensure_snapshot_present(client, module, params) |
| 366 |
|
| 367 |
|
| 368 |
if __name__ == "__main__": |
| 369 |
main() |