| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: 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 |
--- |
| 10 |
module: backup_vault |
| 11 |
version_added: 6.0.0 |
| 12 |
short_description: Manage AWS Backup Vaults |
| 13 |
description: |
| 14 |
- Creates, deletes, or lists Backup Vault configuration. |
| 15 |
author: |
| 16 |
- Gomathi Selvi Srinivasan (@GomathiselviS) |
| 17 |
options: |
| 18 |
state: |
| 19 |
description: |
| 20 |
- Add or remove Backup Vault configuration. |
| 21 |
type: str |
| 22 |
choices: ['present', 'absent'] |
| 23 |
default: present |
| 24 |
backup_vault_name: |
| 25 |
description: |
| 26 |
- Name for the Backup Vault. |
| 27 |
- Names are unique to the account used to create them and the Amazon Web Services Region where they are created. |
| 28 |
- They consist of letters, numbers, and hyphens. |
| 29 |
type: str |
| 30 |
required: true |
| 31 |
encryption_key_arn: |
| 32 |
description: |
| 33 |
- The server-side encryption key that is used to protect the backups. |
| 34 |
type: str |
| 35 |
creator_request_id: |
| 36 |
description: |
| 37 |
- A unique string that identifies the request and allows failed requests to be retried without the risk of running the operation twice. |
| 38 |
- If used, this parameter must contain 1 to 50 alphanumeric or "-_." characters. |
| 39 |
type: str |
| 40 |
|
| 41 |
extends_documentation_fragment: |
| 42 |
- amazon.aws.common.modules |
| 43 |
- amazon.aws.region.modules |
| 44 |
- amazon.aws.boto3 |
| 45 |
- amazon.aws.tags |
| 46 |
""" |
| 47 |
|
| 48 |
EXAMPLES = r""" |
| 49 |
- name: create backup vault |
| 50 |
amazon.aws.backup_vault: |
| 51 |
state: present |
| 52 |
backup_vault_name: default-vault |
| 53 |
encryption_key_arn: arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab |
| 54 |
tags: |
| 55 |
environment: dev |
| 56 |
Name: default |
| 57 |
""" |
| 58 |
|
| 59 |
RETURN = r""" |
| 60 |
exists: |
| 61 |
description: Whether the Backup Vault exists. |
| 62 |
returned: always |
| 63 |
type: bool |
| 64 |
sample: true |
| 65 |
vault: |
| 66 |
description: Backup Vault details. |
| 67 |
returned: always |
| 68 |
type: complex |
| 69 |
sample: hash/dictionary of values |
| 70 |
contains: |
| 71 |
backup_vault_name: |
| 72 |
description: The name of a logical container where backups are stored. |
| 73 |
returned: always |
| 74 |
type: str |
| 75 |
sample: default-name |
| 76 |
backup_vault_arn: |
| 77 |
description: An Amazon Resource Name (ARN) that uniquely identifies a backup vault. |
| 78 |
returned: always |
| 79 |
type: str |
| 80 |
sample: arn:aws:backup:us-east-1:123456789012:vault:aBackupVault |
| 81 |
creation_date: |
| 82 |
description: The date and time a backup vault is created, in Unix format and Coordinated Universal Time (UTC). |
| 83 |
returned: always |
| 84 |
type: str |
| 85 |
sample: "2024-05-21T13:21:10.062000-07:00" |
| 86 |
encryption_key_arn: |
| 87 |
description: The server-side encryption key that is used to protect your backups. |
| 88 |
returned: always |
| 89 |
type: str |
| 90 |
sample: "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab" |
| 91 |
locked: |
| 92 |
description: |
| 93 |
- A Boolean that indicates whether Backup Vault Lock is currently protecting the backup vault. |
| 94 |
- True means that Vault Lock causes delete or update operations on the recovery points stored in the vault to fail. |
| 95 |
returned: always |
| 96 |
type: bool |
| 97 |
sample: false |
| 98 |
number_of_recovery_points: |
| 99 |
description: The number of recovery points that are stored in a backup vault. |
| 100 |
returned: always |
| 101 |
type: int |
| 102 |
sample: 0 |
| 103 |
tags: |
| 104 |
description: Tags of the backup vault. |
| 105 |
returned: on create/update |
| 106 |
type: str |
| 107 |
sample: { |
| 108 |
"TagKey1": "TagValue1", |
| 109 |
"TagKey2": "TagValue2" |
| 110 |
} |
| 111 |
""" |
| 112 |
|
| 113 |
|
| 114 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 115 |
|
| 116 |
from ansible_collections.amazon.aws.plugins.module_utils.backup import get_backup_resource_tags |
| 117 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code |
| 118 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 119 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import compare_aws_tags |
| 120 |
|
| 121 |
try: |
| 122 |
from botocore.exceptions import BotoCoreError |
| 123 |
from botocore.exceptions import ClientError |
| 124 |
except ImportError: |
| 125 |
pass # Handled by AnsibleAWSModule |
| 126 |
|
| 127 |
|
| 128 |
def create_backup_vault(module, client, params): |
| 129 |
""" |
| 130 |
Creates a Backup Vault |
| 131 |
|
| 132 |
module : AnsibleAWSModule object |
| 133 |
client : boto3 client connection object |
| 134 |
params : The parameters to create a backup vault |
| 135 |
""" |
| 136 |
resp = {} |
| 137 |
params = {k: v for k, v in params.items() if v is not None} |
| 138 |
try: |
| 139 |
resp = client.create_backup_vault(**params) |
| 140 |
except ( |
| 141 |
BotoCoreError, |
| 142 |
ClientError, |
| 143 |
) as err: |
| 144 |
module.fail_json_aws(err, msg="Failed to create Backup Vault") |
| 145 |
return resp |
| 146 |
|
| 147 |
|
| 148 |
def tag_vault(module, client, tags, vault_arn, curr_tags=None, purge_tags=True): |
| 149 |
""" |
| 150 |
Creates, updates, removes tags on a Backup Vault resource |
| 151 |
|
| 152 |
module : AnsibleAWSModule object |
| 153 |
client : boto3 client connection object |
| 154 |
tags : Dict of tags converted from ansible_dict to boto3 list of dicts |
| 155 |
vault_arn : The ARN of the Backup Vault to operate on |
| 156 |
curr_tags : Dict of the current tags on resource, if any |
| 157 |
purge_tags : true/false to determine if current tags will be retained or not |
| 158 |
""" |
| 159 |
|
| 160 |
if tags is None: |
| 161 |
return False |
| 162 |
|
| 163 |
curr_tags = curr_tags or {} |
| 164 |
tags_to_add, tags_to_remove = compare_aws_tags(curr_tags, tags, purge_tags=purge_tags) |
| 165 |
|
| 166 |
if not tags_to_add and not tags_to_remove: |
| 167 |
return False |
| 168 |
|
| 169 |
if module.check_mode: |
| 170 |
return True |
| 171 |
|
| 172 |
if tags_to_remove: |
| 173 |
try: |
| 174 |
client.untag_resource(ResourceArn=vault_arn, TagKeyList=tags_to_remove) |
| 175 |
except (BotoCoreError, ClientError) as err: |
| 176 |
module.fail_json_aws(err, msg="Failed to remove tags from the vault") |
| 177 |
|
| 178 |
if tags_to_add: |
| 179 |
try: |
| 180 |
client.tag_resource(ResourceArn=vault_arn, Tags=tags_to_add) |
| 181 |
except (BotoCoreError, ClientError) as err: |
| 182 |
module.fail_json_aws(err, msg="Failed to add tags to Vault") |
| 183 |
|
| 184 |
return True |
| 185 |
|
| 186 |
|
| 187 |
def get_vault_facts(module, client, vault_name): |
| 188 |
""" |
| 189 |
Describes existing vault in an account |
| 190 |
|
| 191 |
module : AnsibleAWSModule object |
| 192 |
client : boto3 client connection object |
| 193 |
vault_name : Name of the backup vault |
| 194 |
""" |
| 195 |
resp = None |
| 196 |
# get Backup Vault info |
| 197 |
try: |
| 198 |
resp = client.describe_backup_vault(BackupVaultName=vault_name) |
| 199 |
except is_boto3_error_code("AccessDeniedException"): |
| 200 |
module.warn("Access Denied trying to describe backup vault") |
| 201 |
except (BotoCoreError, ClientError) as err: |
| 202 |
module.fail_json_aws(err, msg="Unable to get vault facts") |
| 203 |
|
| 204 |
# Now check to see if our vault exists and get status and tags |
| 205 |
if resp: |
| 206 |
if resp.get("BackupVaultArn"): |
| 207 |
resource = resp.get("BackupVaultArn") |
| 208 |
resp["tags"] = get_backup_resource_tags(module, client, resource) |
| 209 |
|
| 210 |
return resp |
| 211 |
|
| 212 |
else: |
| 213 |
# vault doesn't exist return None |
| 214 |
return None |
| 215 |
|
| 216 |
|
| 217 |
def delete_backup_vault(module, client, vault_name): |
| 218 |
""" |
| 219 |
Delete a Backup Vault |
| 220 |
|
| 221 |
module : AnsibleAWSModule object |
| 222 |
client : boto3 client connection object |
| 223 |
vault_name : Backup Vault Name |
| 224 |
""" |
| 225 |
try: |
| 226 |
client.delete_backup_vault(BackupVaultName=vault_name) |
| 227 |
except (BotoCoreError, ClientError) as err: |
| 228 |
module.fail_json_aws(err, msg="Failed to delete the Backup Vault") |
| 229 |
|
| 230 |
|
| 231 |
def main(): |
| 232 |
argument_spec = dict( |
| 233 |
state=dict(default="present", choices=["present", "absent"]), |
| 234 |
backup_vault_name=dict(required=True, type="str"), |
| 235 |
encryption_key_arn=dict(type="str", no_log=False), |
| 236 |
creator_request_id=dict(type="str"), |
| 237 |
tags=dict(type="dict", aliases=["resource_tags"]), |
| 238 |
purge_tags=dict(default=True, type="bool"), |
| 239 |
) |
| 240 |
|
| 241 |
required_if = [ |
| 242 |
("state", "present", ["backup_vault_name"]), |
| 243 |
("state", "enabled", ["backup_vault_name"]), |
| 244 |
] |
| 245 |
|
| 246 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True, required_if=required_if) |
| 247 |
|
| 248 |
# collect parameters |
| 249 |
if module.params["state"] in ("present", "enabled"): |
| 250 |
state = "present" |
| 251 |
elif module.params["state"] in ("absent", "disabled"): |
| 252 |
state = "absent" |
| 253 |
tags = module.params["tags"] |
| 254 |
purge_tags = module.params["purge_tags"] |
| 255 |
ct_params = dict( |
| 256 |
BackupVaultName=module.params["backup_vault_name"], |
| 257 |
BackupVaultTags=module.params["tags"], |
| 258 |
EncryptionKeyArn=module.params["encryption_key_arn"], |
| 259 |
CreatorRequestId=module.params["creator_request_id"], |
| 260 |
) |
| 261 |
|
| 262 |
client = module.client("backup") |
| 263 |
results = dict(changed=False, exists=False) |
| 264 |
|
| 265 |
# Get existing backup vault facts |
| 266 |
try: |
| 267 |
vault = get_vault_facts(module, client, ct_params["BackupVaultName"]) |
| 268 |
except (BotoCoreError, ClientError) as err: |
| 269 |
module.debug(f"Unable to get vault facts {err}") |
| 270 |
|
| 271 |
# If the vault exists set the result exists variable |
| 272 |
if vault is not None: |
| 273 |
results["exists"] = True |
| 274 |
|
| 275 |
if state == "absent" and results["exists"]: |
| 276 |
# If Trail exists go ahead and delete |
| 277 |
results["changed"] = True |
| 278 |
results["exists"] = False |
| 279 |
results["backupvault"] = dict() |
| 280 |
if not module.check_mode: |
| 281 |
delete_backup_vault(module, client, vault["BackupVaultName"]) |
| 282 |
|
| 283 |
elif state == "present" and not results["exists"]: |
| 284 |
# Backup Vault doesn't exist just go create it |
| 285 |
results["changed"] = True |
| 286 |
results["exists"] = True |
| 287 |
if not module.check_mode: |
| 288 |
if tags: |
| 289 |
ct_params["BackupVaultTags"] = tags |
| 290 |
# If we aren't in check_mode then actually create it |
| 291 |
create_backup_vault(module, client, ct_params) |
| 292 |
|
| 293 |
# Get facts for newly created Backup Vault |
| 294 |
vault = get_vault_facts(module, client, ct_params["BackupVaultName"]) |
| 295 |
|
| 296 |
# If we are in check mode create a fake return structure for the newly created vault |
| 297 |
if module.check_mode: |
| 298 |
vault = dict() |
| 299 |
vault.update(ct_params) |
| 300 |
vault["EncryptionKeyArn"] = "" |
| 301 |
vault["tags"] = tags |
| 302 |
|
| 303 |
elif state == "present" and results["exists"]: |
| 304 |
# Check if we need to update tags on resource |
| 305 |
tags_changed = tag_vault( |
| 306 |
module, |
| 307 |
client, |
| 308 |
tags=tags, |
| 309 |
vault_arn=vault["BackupVaultArn"], |
| 310 |
curr_tags=vault["tags"], |
| 311 |
purge_tags=purge_tags, |
| 312 |
) |
| 313 |
if tags_changed: |
| 314 |
updated_tags = dict() |
| 315 |
if not purge_tags: |
| 316 |
updated_tags = vault["tags"] |
| 317 |
updated_tags.update(tags) |
| 318 |
results["changed"] = True |
| 319 |
vault["tags"] = updated_tags |
| 320 |
|
| 321 |
# Populate backup vault facts in output |
| 322 |
|
| 323 |
if vault: |
| 324 |
results["vault"] = camel_dict_to_snake_dict(vault, ignore_list=["tags"]) |
| 325 |
module.exit_json(**results) |
| 326 |
|
| 327 |
|
| 328 |
if __name__ == "__main__": |
| 329 |
main() |