| 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_plan_info |
| 11 |
version_added: 6.0.0 |
| 12 |
short_description: Describe AWS Backup Plans |
| 13 |
description: |
| 14 |
- Lists info about Backup Plan configuration. |
| 15 |
author: |
| 16 |
- Gomathi Selvi Srinivasan (@GomathiselviS) |
| 17 |
- Kristof Imre Szabo (@krisek) |
| 18 |
- Alina Buzachis (@alinabuzachis) |
| 19 |
options: |
| 20 |
backup_plan_names: |
| 21 |
type: list |
| 22 |
elements: str |
| 23 |
description: |
| 24 |
- Specifies a list of plan names. |
| 25 |
extends_documentation_fragment: |
| 26 |
- amazon.aws.common.modules |
| 27 |
- amazon.aws.region.modules |
| 28 |
- amazon.aws.boto3 |
| 29 |
""" |
| 30 |
|
| 31 |
EXAMPLES = r""" |
| 32 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 33 |
- name: Gather information about all backup plans |
| 34 |
amazon.aws.backup_plan_info: |
| 35 |
|
| 36 |
- name: Gather information about a particular backup plan |
| 37 |
amazon.aws.backup_plan_info: |
| 38 |
backup plan_names: |
| 39 |
- elastic |
| 40 |
""" |
| 41 |
|
| 42 |
RETURN = r""" |
| 43 |
backup_plans: |
| 44 |
description: List of backup plan objects. List elements are dict with the information about backup plan. |
| 45 |
type: list |
| 46 |
elements: dict |
| 47 |
returned: always |
| 48 |
contains: |
| 49 |
backup_plan_arn: |
| 50 |
description: An Amazon Resource Name (ARN) that uniquely identifies a backup plan. |
| 51 |
returned: always |
| 52 |
type: str |
| 53 |
sample: arn:aws:backup:eu-central-1:111122223333:backup-plan:1111f877-1ecf-4d79-9718-a861cd09df3b |
| 54 |
backup_plan_id: |
| 55 |
description: Id of the backup plan. |
| 56 |
returned: always |
| 57 |
type: str |
| 58 |
sample: "1111f877-1ecf-4d79-9718-a861cd09df3b" |
| 59 |
backup_plan_name: |
| 60 |
description: The display name of a backup plan. |
| 61 |
returned: always |
| 62 |
type: str |
| 63 |
sample: elastic |
| 64 |
creation_date: |
| 65 |
description: Creation date of the backup plan. |
| 66 |
returned: always |
| 67 |
type: str |
| 68 |
sample: '2023-01-24T10:08:03.193000+01:00' |
| 69 |
last_execution_date: |
| 70 |
description: Last execution date of the backup plan. |
| 71 |
returned: always |
| 72 |
type: str |
| 73 |
sample: '2023-03-24T06:30:08.250000+01:00' |
| 74 |
tags: |
| 75 |
description: Tags of the backup plan |
| 76 |
returned: always |
| 77 |
type: str |
| 78 |
sample: { |
| 79 |
"TagKey1": "TagValue1", |
| 80 |
"TagKey2": "TagValue2" |
| 81 |
} |
| 82 |
version_id: |
| 83 |
description: Version id of the backup plan |
| 84 |
returned: always |
| 85 |
type: str |
| 86 |
sample: 'A2AiAAAmAAAtAxAxAA00AAa0AAAxAxAtA0AmAAA4NDY1ZTZl' |
| 87 |
backup_plan: |
| 88 |
returned: always |
| 89 |
description: Detailed information about the backup plan. |
| 90 |
type: list |
| 91 |
elements: dict |
| 92 |
contains: |
| 93 |
backup_plan_name: |
| 94 |
description: Name of the backup plan. |
| 95 |
returned: always |
| 96 |
type: str |
| 97 |
sample: elastic |
| 98 |
advanced_backup_settings: |
| 99 |
description: Advanced backup settings of the backup plan. |
| 100 |
returned: when configured |
| 101 |
type: list |
| 102 |
elements: dict |
| 103 |
sample: [ |
| 104 |
{ |
| 105 |
"backup_options": { |
| 106 |
"windows_vss": "enabled" |
| 107 |
}, |
| 108 |
"resource_type": "EC2" |
| 109 |
} |
| 110 |
] |
| 111 |
contains: |
| 112 |
resource_type: |
| 113 |
description: Resource type of the advanced setting. |
| 114 |
type: str |
| 115 |
backup_options: |
| 116 |
description: Options of the advanced setting. |
| 117 |
type: dict |
| 118 |
rules: |
| 119 |
description: |
| 120 |
- An array of BackupRule objects, each of which specifies a scheduled task that is used to back up a selection of resources. |
| 121 |
returned: always |
| 122 |
type: list |
| 123 |
elements: dict |
| 124 |
contains: |
| 125 |
rule_name: |
| 126 |
description: A display name for a backup rule. |
| 127 |
returned: always |
| 128 |
type: str |
| 129 |
sample: "daily" |
| 130 |
target_backup_vault_name: |
| 131 |
description: The name of a logical container where backups are stored. |
| 132 |
returned: always |
| 133 |
type: str |
| 134 |
sample: "09da67966fd5-backup-vault" |
| 135 |
schedule_expression: |
| 136 |
description: A cron expression in UTC specifying when Backup initiates a backup job. |
| 137 |
returned: always |
| 138 |
type: str |
| 139 |
sample: "cron(0 5 ? * * *)" |
| 140 |
start_window_minutes: |
| 141 |
description: |
| 142 |
- A value in minutes after a backup is scheduled before a job will be canceled if it |
| 143 |
doesn't start successfully. |
| 144 |
type: int |
| 145 |
returned: always |
| 146 |
sample: 480 |
| 147 |
completion_window_minutes: |
| 148 |
description: |
| 149 |
- A value in minutes after a backup job is successfully started before it must be |
| 150 |
completed or it will be canceled by Backup. |
| 151 |
type: int |
| 152 |
returned: always |
| 153 |
sample: 10080 |
| 154 |
lifecycle: |
| 155 |
description: |
| 156 |
- Defines when a protected resource is transitioned to cold storage and when it expires. |
| 157 |
type: dict |
| 158 |
returned: when configured. |
| 159 |
sample: { |
| 160 |
"delete_after_days": 100, |
| 161 |
"move_to_cold_storage_after_days": 10 |
| 162 |
} |
| 163 |
recovery_point_tags: |
| 164 |
description: |
| 165 |
- An array of key-value pair strings that are assigned to resources that are associated with |
| 166 |
this rule when restored from backup. |
| 167 |
type: dict |
| 168 |
returned: when configured. |
| 169 |
sample: { |
| 170 |
"Tagkey1": "TagValue1", |
| 171 |
"Tagkey2": "TagValue2" |
| 172 |
} |
| 173 |
rule_id: |
| 174 |
description: |
| 175 |
- Uniquely identifies a rule that is used to schedule the backup of a selection of resources. |
| 176 |
type: str |
| 177 |
returned: always |
| 178 |
sample: "973621ef-d863-41ef-b5c3-9e943a64ad0c" |
| 179 |
copy_actions: |
| 180 |
description: An array of CopyAction objects, which contains the details of the copy operation. |
| 181 |
returned: when configured. |
| 182 |
type: list |
| 183 |
elements: dict |
| 184 |
sample: [ |
| 185 |
{ |
| 186 |
"destination_backup_vault_arn": "arn:aws:backup:us-west-2:123456789012:backup-vault:my-test-vault", |
| 187 |
"lifecycle": { |
| 188 |
"delete_after_days": 100, |
| 189 |
"move_to_cold_storage_after_days": 10 |
| 190 |
} |
| 191 |
} |
| 192 |
] |
| 193 |
enable_continous_backup: |
| 194 |
description: Specifies whether Backup creates continuous backups. |
| 195 |
type: bool |
| 196 |
returned: always |
| 197 |
sample: false |
| 198 |
""" |
| 199 |
|
| 200 |
try: |
| 201 |
import botocore |
| 202 |
except ImportError: |
| 203 |
pass # Handled by AnsibleAWSModule |
| 204 |
|
| 205 |
|
| 206 |
from ansible_collections.amazon.aws.plugins.module_utils.backup import get_plan_details |
| 207 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 208 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 209 |
|
| 210 |
|
| 211 |
def get_all_backup_plans_info(client): |
| 212 |
paginator = client.get_paginator("list_backup_plans") |
| 213 |
return paginator.paginate().build_full_result() |
| 214 |
|
| 215 |
|
| 216 |
def get_backup_plan_detail(client, module): |
| 217 |
backup_plan_list = [] |
| 218 |
backup_plan_names = module.params.get("backup_plan_names") |
| 219 |
|
| 220 |
if backup_plan_names is None: |
| 221 |
backup_plan_names = [] |
| 222 |
backup_plan_list_info = get_all_backup_plans_info(client)["BackupPlansList"] |
| 223 |
for backup_plan in backup_plan_list_info: |
| 224 |
backup_plan_names.append(backup_plan["BackupPlanName"]) |
| 225 |
|
| 226 |
for name in backup_plan_names: |
| 227 |
backup_plan_list.extend(get_plan_details(module, client, name)) |
| 228 |
|
| 229 |
module.exit_json(**{"backup_plans": backup_plan_list}) |
| 230 |
|
| 231 |
|
| 232 |
def main(): |
| 233 |
argument_spec = dict( |
| 234 |
backup_plan_names=dict(type="list", elements="str"), |
| 235 |
) |
| 236 |
|
| 237 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) |
| 238 |
|
| 239 |
try: |
| 240 |
connection = module.client("backup", retry_decorator=AWSRetry.jittered_backoff()) |
| 241 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 242 |
module.fail_json_aws(e, msg="Failed to connect to AWS") |
| 243 |
|
| 244 |
get_backup_plan_detail(connection, module) |
| 245 |
|
| 246 |
|
| 247 |
if __name__ == "__main__": |
| 248 |
main() |