| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: Contributors to the Ansible project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
module: backup_selection |
| 10 |
short_description: Create, delete and modify AWS Backup selection |
| 11 |
version_added: 6.0.0 |
| 12 |
description: |
| 13 |
- Manages AWS Backup selections. |
| 14 |
- For more information see the AWS documentation for backup selections |
| 15 |
U(https://docs.aws.amazon.com/aws-backup/latest/devguide/assigning-resources.html). |
| 16 |
options: |
| 17 |
backup_plan_name: |
| 18 |
description: |
| 19 |
- Uniquely identifies the backup plan to be associated with the selection of resources. |
| 20 |
required: true |
| 21 |
type: str |
| 22 |
aliases: |
| 23 |
- plan_name |
| 24 |
backup_selection_name: |
| 25 |
description: |
| 26 |
- The display name of a resource selection document. Must contain 1 to 50 alphanumeric or '-_.' characters. |
| 27 |
required: true |
| 28 |
type: str |
| 29 |
aliases: |
| 30 |
- selection_name |
| 31 |
iam_role_arn: |
| 32 |
description: |
| 33 |
- The ARN of the IAM role that Backup uses to authenticate when backing up the target resource. |
| 34 |
type: str |
| 35 |
resources: |
| 36 |
description: |
| 37 |
- A list of Amazon Resource Names (ARNs) to assign to a backup plan. The maximum number of ARNs is 500 without wildcards, |
| 38 |
or 30 ARNs with wildcards. If you need to assign many resources to a backup plan, consider a different resource selection |
| 39 |
strategy, such as assigning all resources of a resource type or refining your resource selection using tags. |
| 40 |
type: list |
| 41 |
elements: str |
| 42 |
list_of_tags: |
| 43 |
description: |
| 44 |
- A list of conditions that you define to assign resources to your backup plans using tags. |
| 45 |
- Condition operators are case sensitive. |
| 46 |
- When you specify more than one condition in O(list_of_tags), you assign all resources that match AT LEAST ONE condition (using OR logic). |
| 47 |
type: list |
| 48 |
elements: dict |
| 49 |
suboptions: |
| 50 |
condition_type: |
| 51 |
description: |
| 52 |
- An operation applied to a key-value pair used to assign resources to your backup plan. |
| 53 |
- Condition only supports V(STRINGEQUALS). |
| 54 |
type: str |
| 55 |
condition_key: |
| 56 |
description: |
| 57 |
- The key in a key-value pair. |
| 58 |
type: str |
| 59 |
condition_value: |
| 60 |
description: |
| 61 |
- The value in a key-value pair. |
| 62 |
type: str |
| 63 |
not_resources: |
| 64 |
description: |
| 65 |
- A list of Amazon Resource Names (ARNs) to exclude from a backup plan. The maximum number of ARNs is 500 without wildcards, |
| 66 |
or 30 ARNs with wildcards. If you need to exclude many resources from a backup plan, consider a different resource |
| 67 |
selection strategy, such as assigning only one or a few resource types or refining your resource selection using tags. |
| 68 |
type: list |
| 69 |
elements: str |
| 70 |
conditions: |
| 71 |
description: |
| 72 |
- A list of conditions (expressed as a dict) that you define to assign resources to your backup plans using tags. |
| 73 |
- When you specify more than one condition in I(conditions), you only assign the resources that match ALL conditions (using AND logic). |
| 74 |
- O(conditions) supports V(string_equals), V(string_like), V(string_not_equals), and V(string_not_like). O(list_of_tags) only supports V(string_equals). |
| 75 |
type: dict |
| 76 |
suboptions: |
| 77 |
string_equals: |
| 78 |
description: |
| 79 |
- Filters the values of your tagged resources for only those resources that you tagged with the same value. |
| 80 |
type: list |
| 81 |
default: [] |
| 82 |
elements: dict |
| 83 |
suboptions: |
| 84 |
condition_key: |
| 85 |
description: |
| 86 |
- The key in a key-value pair. |
| 87 |
- O(conditions.string_equals.condition_key) in the O(conditions) option must use the AWS resource tag prefix, e.g. 'aws:ResourceTag/key-name' |
| 88 |
type: str |
| 89 |
condition_value: |
| 90 |
description: The value in a key-value pair. |
| 91 |
type: str |
| 92 |
string_like: |
| 93 |
description: |
| 94 |
- Filters the values of your tagged resources for matching tag values with the use of a wildcard character (*) anywhere in the string. |
| 95 |
For example, "prod*" or "*rod*" matches the tag value "production". |
| 96 |
type: list |
| 97 |
default: [] |
| 98 |
elements: dict |
| 99 |
suboptions: |
| 100 |
condition_key: |
| 101 |
description: |
| 102 |
- The key in a key-value pair. |
| 103 |
- O(conditions.string_like.condition_key) in the O(conditions) option must use the AWS resource tag prefix, e.g. 'aws:ResourceTag/key-name' |
| 104 |
type: str |
| 105 |
condition_value: |
| 106 |
description: The value in a key-value pair. |
| 107 |
type: str |
| 108 |
string_not_equals: |
| 109 |
description: |
| 110 |
- Filters the values of your tagged resources for only those resources that you tagged that do not have the same value. |
| 111 |
type: list |
| 112 |
default: [] |
| 113 |
elements: dict |
| 114 |
suboptions: |
| 115 |
condition_key: |
| 116 |
description: |
| 117 |
- The key in a key-value pair. |
| 118 |
- O(conditions.string_not_equals.condition_key) in the O(conditions) option must use the AWS resource tag prefix, e.g. 'aws:ResourceTag/key-name' |
| 119 |
type: str |
| 120 |
condition_value: |
| 121 |
description: The value in a key-value pair. |
| 122 |
type: str |
| 123 |
string_not_like: |
| 124 |
description: |
| 125 |
- Filters the values of your tagged resources for non-matching tag values with the use of a wildcard character (*) anywhere in the string. |
| 126 |
type: list |
| 127 |
default: [] |
| 128 |
elements: dict |
| 129 |
suboptions: |
| 130 |
condition_key: |
| 131 |
description: |
| 132 |
- The key in a key-value pair. |
| 133 |
- O(conditions.string_not_like.condition_key) in the O(conditions) option must use the AWS resource tag prefix, e.g. 'aws:ResourceTag/key-name' |
| 134 |
type: str |
| 135 |
condition_value: |
| 136 |
description: The value in a key-value pair. |
| 137 |
type: str |
| 138 |
state: |
| 139 |
description: |
| 140 |
- Create, delete a backup selection. |
| 141 |
default: present |
| 142 |
choices: ['present', 'absent'] |
| 143 |
type: str |
| 144 |
author: |
| 145 |
- Kristof Imre Szabo (@krisek) |
| 146 |
- Alina Buzachis (@alinabuzachis) |
| 147 |
extends_documentation_fragment: |
| 148 |
- amazon.aws.common.modules |
| 149 |
- amazon.aws.region.modules |
| 150 |
- amazon.aws.boto3 |
| 151 |
""" |
| 152 |
|
| 153 |
|
| 154 |
EXAMPLES = r""" |
| 155 |
- name: Create backup selection |
| 156 |
amazon.aws.backup_selection: |
| 157 |
selection_name: elastic |
| 158 |
backup_plan_name: test-backup-plan |
| 159 |
iam_role_arn: arn:aws:iam::111122223333:role/system-backup |
| 160 |
resources: |
| 161 |
- arn:aws:elasticfilesystem:*:*:file-system/* |
| 162 |
|
| 163 |
- name: Create backup selection, assign resources using tags |
| 164 |
amazon.aws.backup_selection: |
| 165 |
selection_name: elastic |
| 166 |
backup_plan_name: test-backup-plan |
| 167 |
iam_role_arn: arn:aws:iam::111122223333:role/system-backup |
| 168 |
list_of_tags: |
| 169 |
- condition_type: STRINGEQUALS |
| 170 |
condition_key: Owner |
| 171 |
condition_value: Dev01 |
| 172 |
""" |
| 173 |
|
| 174 |
|
| 175 |
RETURN = r""" |
| 176 |
backup_selection: |
| 177 |
description: Backup selection details. |
| 178 |
returned: always |
| 179 |
type: complex |
| 180 |
contains: |
| 181 |
backup_plan_id: |
| 182 |
description: ID of the backup plan. |
| 183 |
returned: always |
| 184 |
type: str |
| 185 |
sample: "1111f877-1ecf-4d79-9718-a861cd09df3b" |
| 186 |
creation_date: |
| 187 |
description: Creation date of the backup plan. |
| 188 |
returned: on create/update |
| 189 |
type: str |
| 190 |
sample: '2023-01-24T10:08:03.193000+01:00' |
| 191 |
iam_role_arn: |
| 192 |
description: The ARN of the IAM role that Backup uses to authenticate when backing up the target resource. |
| 193 |
returned: always |
| 194 |
type: str |
| 195 |
sample: "arn:aws:iam::111122223333:role/system-backup" |
| 196 |
selection_id: |
| 197 |
description: Backup selection id. |
| 198 |
returned: always |
| 199 |
type: str |
| 200 |
sample: "1111c217-5d71-4a55-8728-5fc4e63d437b" |
| 201 |
selection_name: |
| 202 |
description: The display name of a resource selection document. |
| 203 |
returned: always |
| 204 |
type: str |
| 205 |
sample: elastic |
| 206 |
conditions: |
| 207 |
description: A list of conditions that defines how resources are assigned to backup plans using tags. |
| 208 |
returned: always |
| 209 |
type: dict |
| 210 |
sample: { |
| 211 |
"string_equals": [], |
| 212 |
"string_like": [], |
| 213 |
"string_not_equals": [], |
| 214 |
"string_not_like": [] |
| 215 |
} |
| 216 |
contains: |
| 217 |
string_equals: |
| 218 |
description: Filters the values of tagged resources for only those resources that are tagged with the same value. |
| 219 |
returned: always |
| 220 |
type: list |
| 221 |
sample: [] |
| 222 |
string_like: |
| 223 |
description: Filters the values of tagged resources for matching tag values with the use of a wildcard character (*) anywhere in the string. |
| 224 |
returned: always |
| 225 |
type: list |
| 226 |
sample: [] |
| 227 |
string_not_equals: |
| 228 |
description: Filters the values of tagged resources for only those resources that are not tagged with the same value. |
| 229 |
returned: always |
| 230 |
type: list |
| 231 |
sample: [] |
| 232 |
string_not_like: |
| 233 |
description: Filters the values of tagged resources for non-matching tag values with the use of a wildcard character (*) anywhere in the string. |
| 234 |
returned: always |
| 235 |
type: list |
| 236 |
sample: [] |
| 237 |
list_of_tags: |
| 238 |
description: Conditions defined to assign resources to the backup plans using tags. |
| 239 |
returned: always |
| 240 |
type: list |
| 241 |
elements: dict |
| 242 |
sample: [ |
| 243 |
{ |
| 244 |
"condition_key": "Environment", |
| 245 |
"condition_type": "STRINGEQUALS", |
| 246 |
"condition_value": "Dev" |
| 247 |
} |
| 248 |
] |
| 249 |
contains: |
| 250 |
condition_key: |
| 251 |
description: The key in a key-value pair. |
| 252 |
returned: always |
| 253 |
type: str |
| 254 |
sample: "Environment" |
| 255 |
condition_type: |
| 256 |
description: An operation applied to a key-value pair used to assign resources to backup plan. |
| 257 |
returned: always |
| 258 |
type: str |
| 259 |
sample: "STRINGEQUALS" |
| 260 |
condition_value: |
| 261 |
description: The value in a key-value pair. |
| 262 |
returned: always |
| 263 |
type: str |
| 264 |
sample: "Dev" |
| 265 |
not_resources: |
| 266 |
description: List of Amazon Resource Names (ARNs) that are excluded from the backup plan. |
| 267 |
returned: always |
| 268 |
type: list |
| 269 |
sample: [] |
| 270 |
resources: |
| 271 |
description: List of Amazon Resource Names (ARNs) that are assigned to the backup plan. |
| 272 |
returned: always |
| 273 |
type: list |
| 274 |
sample: [] |
| 275 |
""" |
| 276 |
|
| 277 |
import json |
| 278 |
|
| 279 |
try: |
| 280 |
import botocore |
| 281 |
except ImportError: |
| 282 |
pass # Handled by AnsibleAWSModule |
| 283 |
|
| 284 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 285 |
from ansible.module_utils.common.dict_transformations import snake_dict_to_camel_dict |
| 286 |
|
| 287 |
from ansible_collections.amazon.aws.plugins.module_utils.backup import get_plan_details |
| 288 |
from ansible_collections.amazon.aws.plugins.module_utils.backup import get_selection_details |
| 289 |
from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule |
| 290 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry |
| 291 |
|
| 292 |
|
| 293 |
def check_for_update(current_selection, backup_selection_data, iam_role_arn): |
| 294 |
update_needed = False |
| 295 |
if current_selection[0].get("IamRoleArn", None) != iam_role_arn: |
| 296 |
update_needed = True |
| 297 |
|
| 298 |
fields_to_check = ["Resources", "ListOfTags", "NotResources", "Conditions"] |
| 299 |
for field_name in fields_to_check: |
| 300 |
field_value_from_aws = json.dumps(current_selection[0].get(field_name, []), sort_keys=True) |
| 301 |
new_field_value = json.dumps(backup_selection_data.get(field_name, []), sort_keys=True) |
| 302 |
if new_field_value != field_value_from_aws: |
| 303 |
if field_name != "Conditions": |
| 304 |
update_needed = True |
| 305 |
elif not ( # Check that Conditions values are not both empty |
| 306 |
field_value_from_aws |
| 307 |
== '{"StringEquals": [], "StringLike": [], "StringNotEquals": [], "StringNotLike": []}' # Default AWS Conditions return value |
| 308 |
and new_field_value == "[]" |
| 309 |
): |
| 310 |
update_needed = True |
| 311 |
return update_needed |
| 312 |
|
| 313 |
|
| 314 |
def main(): |
| 315 |
argument_spec = dict( |
| 316 |
backup_selection_name=dict(type="str", required=True, aliases=["selection_name"]), |
| 317 |
backup_plan_name=dict(type="str", required=True, aliases=["plan_name"]), |
| 318 |
iam_role_arn=dict(type="str"), |
| 319 |
resources=dict(type="list", elements="str"), |
| 320 |
conditions=dict( |
| 321 |
type="dict", |
| 322 |
options=dict( |
| 323 |
string_equals=dict( |
| 324 |
type="list", |
| 325 |
default=[], |
| 326 |
elements="dict", |
| 327 |
options=dict( |
| 328 |
condition_key=dict(type="str", no_log=False), |
| 329 |
condition_value=dict(type="str"), |
| 330 |
), |
| 331 |
), |
| 332 |
string_like=dict( |
| 333 |
type="list", |
| 334 |
default=[], |
| 335 |
elements="dict", |
| 336 |
options=dict( |
| 337 |
condition_key=dict(type="str", no_log=False), |
| 338 |
condition_value=dict(type="str"), |
| 339 |
), |
| 340 |
), |
| 341 |
string_not_equals=dict( |
| 342 |
type="list", |
| 343 |
default=[], |
| 344 |
elements="dict", |
| 345 |
options=dict( |
| 346 |
condition_key=dict(type="str", no_log=False), |
| 347 |
condition_value=dict(type="str"), |
| 348 |
), |
| 349 |
), |
| 350 |
string_not_like=dict( |
| 351 |
type="list", |
| 352 |
default=[], |
| 353 |
elements="dict", |
| 354 |
options=dict( |
| 355 |
condition_key=dict(type="str", no_log=False), |
| 356 |
condition_value=dict(type="str"), |
| 357 |
), |
| 358 |
), |
| 359 |
), |
| 360 |
), |
| 361 |
not_resources=dict(type="list", elements="str"), |
| 362 |
list_of_tags=dict( |
| 363 |
type="list", |
| 364 |
elements="dict", |
| 365 |
options=dict( |
| 366 |
condition_type=dict(type="str"), |
| 367 |
condition_key=dict(type="str", no_log=False), |
| 368 |
condition_value=dict(type="str"), |
| 369 |
), |
| 370 |
), |
| 371 |
state=dict(default="present", choices=["present", "absent"]), |
| 372 |
) |
| 373 |
required_if = [ |
| 374 |
("state", "present", ["backup_selection_name", "backup_plan_name", "iam_role_arn"]), |
| 375 |
("state", "absent", ["backup_selection_name", "backup_plan_name"]), |
| 376 |
] |
| 377 |
module = AnsibleAWSModule(argument_spec=argument_spec, required_if=required_if, supports_check_mode=True) |
| 378 |
state = module.params.get("state") |
| 379 |
backup_selection_name = module.params.get("selection_name") |
| 380 |
backup_plan_name = module.params.get("backup_plan_name") |
| 381 |
iam_role_arn = module.params.get("iam_role_arn") |
| 382 |
resources = module.params.get("resources") |
| 383 |
list_of_tags = module.params.get("list_of_tags") |
| 384 |
not_resources = module.params.get("not_resources") |
| 385 |
conditions = module.params.get("conditions") |
| 386 |
|
| 387 |
try: |
| 388 |
client = module.client("backup", retry_decorator=AWSRetry.jittered_backoff()) |
| 389 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 390 |
module.fail_json_aws(e, msg="Failed to connect to AWS") |
| 391 |
|
| 392 |
results = {"changed": False, "exists": False, "backup_selection": {}} |
| 393 |
|
| 394 |
current_selection = get_selection_details(module, client, backup_plan_name, backup_selection_name) |
| 395 |
results["current_selection"] = current_selection |
| 396 |
|
| 397 |
if state == "present": |
| 398 |
# build data specified by user |
| 399 |
update_needed = False |
| 400 |
backup_selection_data = {"SelectionName": backup_selection_name, "IamRoleArn": iam_role_arn} |
| 401 |
if resources: |
| 402 |
backup_selection_data["Resources"] = resources |
| 403 |
if list_of_tags: |
| 404 |
backup_selection_data["ListOfTags"] = snake_dict_to_camel_dict(list_of_tags, capitalize_first=True) |
| 405 |
if not_resources: |
| 406 |
backup_selection_data["NotResources"] = not_resources |
| 407 |
if conditions: |
| 408 |
backup_selection_data["Conditions"] = snake_dict_to_camel_dict(conditions, capitalize_first=True) |
| 409 |
|
| 410 |
if current_selection: |
| 411 |
results["exists"] = True |
| 412 |
update_needed = check_for_update(current_selection, backup_selection_data, iam_role_arn) |
| 413 |
if update_needed: |
| 414 |
if module.check_mode: |
| 415 |
results["changed"] = True |
| 416 |
module.exit_json(**results, msg="Would have created selection if not in check mode") |
| 417 |
|
| 418 |
try: |
| 419 |
client.delete_backup_selection( |
| 420 |
aws_retry=True, |
| 421 |
SelectionId=current_selection[0]["SelectionId"], |
| 422 |
BackupPlanId=current_selection[0]["BackupPlanId"], |
| 423 |
) |
| 424 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 425 |
module.fail_json_aws(e, msg="Failed to delete selection") |
| 426 |
elif not update_needed: |
| 427 |
results["exists"] = True |
| 428 |
# state is present but backup vault doesnt exist |
| 429 |
if not current_selection or update_needed: |
| 430 |
results["changed"] = True |
| 431 |
results["exists"] = True |
| 432 |
plan = get_plan_details(module, client, backup_plan_name) |
| 433 |
|
| 434 |
if module.check_mode: |
| 435 |
module.exit_json(**results, msg="Would have created selection if not in check mode") |
| 436 |
try: |
| 437 |
client.create_backup_selection( |
| 438 |
BackupSelection=backup_selection_data, BackupPlanId=plan[0]["backup_plan_id"] |
| 439 |
) |
| 440 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 441 |
module.fail_json_aws(e, msg="Failed to create selection") |
| 442 |
|
| 443 |
new_selection = get_selection_details(module, client, backup_plan_name, backup_selection_name) |
| 444 |
results["backup_selection"] = camel_dict_to_snake_dict(*new_selection) |
| 445 |
|
| 446 |
elif state == "absent": |
| 447 |
if current_selection: |
| 448 |
results["changed"] = True |
| 449 |
if module.check_mode: |
| 450 |
module.exit_json(**results, msg="Would have deleted backup selection if not in check mode") |
| 451 |
try: |
| 452 |
client.delete_backup_selection( |
| 453 |
aws_retry=True, |
| 454 |
SelectionId=current_selection[0]["SelectionId"], |
| 455 |
BackupPlanId=current_selection[0]["BackupPlanId"], |
| 456 |
) |
| 457 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 458 |
module.fail_json_aws(e, msg="Failed to delete selection") |
| 459 |
|
| 460 |
module.exit_json(**results) |
| 461 |
|
| 462 |
|
| 463 |
if __name__ == "__main__": |
| 464 |
main() |