| 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 |
DOCUMENTATION = r""" |
| 8 |
--- |
| 9 |
module: cloudformation_info |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Obtain information about an AWS CloudFormation stack |
| 12 |
description: |
| 13 |
- Gets information about an AWS CloudFormation stack. |
| 14 |
author: |
| 15 |
- Justin Menga (@jmenga) |
| 16 |
- Kevin Coming (@waffie1) |
| 17 |
options: |
| 18 |
stack_name: |
| 19 |
description: |
| 20 |
- The name or id of the CloudFormation stack. Gathers information on all stacks by default. |
| 21 |
type: str |
| 22 |
all_facts: |
| 23 |
description: |
| 24 |
- Get all stack information for the stack. |
| 25 |
type: bool |
| 26 |
default: false |
| 27 |
stack_events: |
| 28 |
description: |
| 29 |
- Get stack events for the stack. |
| 30 |
type: bool |
| 31 |
default: false |
| 32 |
stack_template: |
| 33 |
description: |
| 34 |
- Get stack template body for the stack. |
| 35 |
type: bool |
| 36 |
default: false |
| 37 |
stack_resources: |
| 38 |
description: |
| 39 |
- Get stack resources for the stack. |
| 40 |
type: bool |
| 41 |
default: false |
| 42 |
stack_policy: |
| 43 |
description: |
| 44 |
- Get stack policy for the stack. |
| 45 |
type: bool |
| 46 |
default: false |
| 47 |
stack_change_sets: |
| 48 |
description: |
| 49 |
- Get stack change sets for the stack |
| 50 |
type: bool |
| 51 |
default: false |
| 52 |
extends_documentation_fragment: |
| 53 |
- amazon.aws.common.modules |
| 54 |
- amazon.aws.region.modules |
| 55 |
- amazon.aws.boto3 |
| 56 |
""" |
| 57 |
|
| 58 |
EXAMPLES = r""" |
| 59 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 60 |
|
| 61 |
- name: Get information on all stacks |
| 62 |
amazon.aws.cloudformation_info: |
| 63 |
register: all_stacks_output |
| 64 |
|
| 65 |
- name: Get summary information about a stack |
| 66 |
amazon.aws.cloudformation_info: |
| 67 |
stack_name: my-cloudformation-stack |
| 68 |
register: output |
| 69 |
|
| 70 |
- debug: |
| 71 |
msg: "{{ output['cloudformation']['my-cloudformation-stack'] }}" |
| 72 |
|
| 73 |
# Get stack outputs, when you have the stack name available as a fact |
| 74 |
- set_fact: |
| 75 |
stack_name: my-awesome-stack |
| 76 |
|
| 77 |
- amazon.aws.cloudformation_info: |
| 78 |
stack_name: "{{ stack_name }}" |
| 79 |
register: my_stack |
| 80 |
|
| 81 |
- debug: |
| 82 |
msg: "{{ my_stack.cloudformation[stack_name].stack_outputs }}" |
| 83 |
|
| 84 |
# Get all stack information about a stack |
| 85 |
- amazon.aws.cloudformation_info: |
| 86 |
stack_name: my-cloudformation-stack |
| 87 |
all_facts: true |
| 88 |
|
| 89 |
# Get stack resource and stack policy information about a stack |
| 90 |
- amazon.aws.cloudformation_info: |
| 91 |
stack_name: my-cloudformation-stack |
| 92 |
stack_resources: true |
| 93 |
stack_policy: true |
| 94 |
|
| 95 |
# Fail if the stack doesn't exist |
| 96 |
- name: try to get info about a stack but fail if it doesn't exist |
| 97 |
amazon.aws.cloudformation_info: |
| 98 |
stack_name: nonexistent-stack |
| 99 |
all_facts: true |
| 100 |
failed_when: cloudformation['nonexistent-stack'] is undefined |
| 101 |
""" |
| 102 |
|
| 103 |
RETURN = r""" |
| 104 |
cloudformation: |
| 105 |
description: |
| 106 |
- Dictionary of dictionaries containing info of stack(s). |
| 107 |
- Keys are stack_name(s). |
| 108 |
returned: always |
| 109 |
type: dict |
| 110 |
contains: |
| 111 |
stack_description: |
| 112 |
description: Summary facts about the stack. |
| 113 |
returned: if the stack exists |
| 114 |
type: dict |
| 115 |
contains: |
| 116 |
capabilities: |
| 117 |
description: The capabilities allowed in the stack. |
| 118 |
returned: always |
| 119 |
type: list |
| 120 |
elements: str |
| 121 |
creation_time: |
| 122 |
description: The time at which the stack was created. |
| 123 |
returned: if stack exists |
| 124 |
type: str |
| 125 |
deletion_time: |
| 126 |
description: The time at which the stack was deleted. |
| 127 |
returned: if stack was deleted |
| 128 |
type: str |
| 129 |
description: |
| 130 |
description: The user-defined description associated with the stack. |
| 131 |
returned: always |
| 132 |
type: str |
| 133 |
disable_rollback: |
| 134 |
description: Whether or not rollback on stack creation failures is enabled. |
| 135 |
returned: always |
| 136 |
type: bool |
| 137 |
drift_information: |
| 138 |
description: Information about whether a stack's actual configuration differs, or has drifted, from it's expected configuration, |
| 139 |
as defined in the stack template and any values specified as template parameters. |
| 140 |
returned: always |
| 141 |
type: dict |
| 142 |
contains: |
| 143 |
stack_drift_status: |
| 144 |
description: Status of the stack's actual configuration compared to its expected template configuration. |
| 145 |
returned: always |
| 146 |
type: str |
| 147 |
last_check_timestamp: |
| 148 |
description: Most recent time when a drift detection operation was initiated on the stack, |
| 149 |
or any of its individual resources that support drift detection. |
| 150 |
returned: if a drift was detected |
| 151 |
type: str |
| 152 |
enable_termination_protection: |
| 153 |
description: Whether termination protection is enabled for the stack. |
| 154 |
returned: always |
| 155 |
type: bool |
| 156 |
notification_arns: |
| 157 |
description: Amazon SNS topic ARNs to which stack related events are published. |
| 158 |
returned: always |
| 159 |
type: list |
| 160 |
elements: str |
| 161 |
outputs: |
| 162 |
description: A list of output dicts. |
| 163 |
returned: always |
| 164 |
type: list |
| 165 |
elements: dict |
| 166 |
contains: |
| 167 |
output_key: |
| 168 |
description: The key associated with the output. |
| 169 |
returned: always |
| 170 |
type: str |
| 171 |
output_value: |
| 172 |
description: The value associated with the output. |
| 173 |
returned: always |
| 174 |
type: str |
| 175 |
parameters: |
| 176 |
description: A list of parameter dicts. |
| 177 |
returned: always |
| 178 |
type: list |
| 179 |
elements: dict |
| 180 |
contains: |
| 181 |
parameter_key: |
| 182 |
description: The key associated with the parameter. |
| 183 |
returned: always |
| 184 |
type: str |
| 185 |
parameter_value: |
| 186 |
description: The value associated with the parameter. |
| 187 |
returned: always |
| 188 |
type: str |
| 189 |
rollback_configuration: |
| 190 |
description: The rollback triggers for CloudFormation to monitor during stack creation and updating operations. |
| 191 |
returned: always |
| 192 |
type: dict |
| 193 |
contains: |
| 194 |
rollback_triggers: |
| 195 |
description: The triggers to monitor during stack creation or update actions. |
| 196 |
returned: when rollback triggers exist |
| 197 |
type: list |
| 198 |
elements: dict |
| 199 |
contains: |
| 200 |
arn: |
| 201 |
description: The ARN of the rollback trigger. |
| 202 |
returned: always |
| 203 |
type: str |
| 204 |
type: |
| 205 |
description: The resource type of the rollback trigger. |
| 206 |
returned: always |
| 207 |
type: str |
| 208 |
stack_id: |
| 209 |
description: The unique ID of the stack. |
| 210 |
returned: always |
| 211 |
type: str |
| 212 |
stack_name: |
| 213 |
description: The name of the stack. |
| 214 |
returned: always |
| 215 |
type: str |
| 216 |
stack_status: |
| 217 |
description: The status of the stack. |
| 218 |
returned: always |
| 219 |
type: str |
| 220 |
tags: |
| 221 |
description: A list of tags associated with the stack. |
| 222 |
returned: always |
| 223 |
type: list |
| 224 |
elements: dict |
| 225 |
contains: |
| 226 |
key: |
| 227 |
description: Key of tag. |
| 228 |
returned: always |
| 229 |
type: str |
| 230 |
value: |
| 231 |
description: Value of tag. |
| 232 |
returned: always |
| 233 |
type: str |
| 234 |
stack_outputs: |
| 235 |
description: Dictionary of stack outputs keyed by the value of each output 'OutputKey' parameter and corresponding value of each |
| 236 |
output 'OutputValue' parameter. |
| 237 |
returned: if the stack exists |
| 238 |
type: dict |
| 239 |
sample: { ApplicationDatabaseName: dazvlpr01xj55a.ap-southeast-2.rds.amazonaws.com } |
| 240 |
stack_parameters: |
| 241 |
description: Dictionary of stack parameters keyed by the value of each parameter 'ParameterKey' parameter and corresponding value of |
| 242 |
each parameter 'ParameterValue' parameter. |
| 243 |
returned: if the stack exists |
| 244 |
type: dict |
| 245 |
sample: |
| 246 |
{ |
| 247 |
DatabaseEngine: mysql, |
| 248 |
DatabasePassword: "***" |
| 249 |
} |
| 250 |
stack_events: |
| 251 |
description: All stack events for the stack. |
| 252 |
returned: only if O(all_facts) or O(stack_events) is V(true) and the stack exists |
| 253 |
type: list |
| 254 |
stack_policy: |
| 255 |
description: Describes the stack policy for the stack. |
| 256 |
returned: only if O(all_facts) or O(stack_policy) is V(true) and the stack exists |
| 257 |
type: dict |
| 258 |
stack_template: |
| 259 |
description: Describes the stack template for the stack. |
| 260 |
returned: only if O(all_facts) or O(stack_policy) is V(true) and the stack exists |
| 261 |
type: dict |
| 262 |
stack_resource_list: |
| 263 |
description: Describes stack resources for the stack. |
| 264 |
returned: only if O(all_facts) or O(stack_policy) is V(true) and the stack exists |
| 265 |
type: list |
| 266 |
stack_resources: |
| 267 |
description: Dictionary of stack resources keyed by the value of each resource 'LogicalResourceId' parameter and corresponding value of each |
| 268 |
resource 'PhysicalResourceId' parameter. |
| 269 |
returned: only if O(all_facts) or O(stack_policy) is V(true) and the stack exists |
| 270 |
type: dict |
| 271 |
sample: { |
| 272 |
"AutoScalingGroup": "dev-someapp-AutoscalingGroup-1SKEXXBCAN0S7", |
| 273 |
"AutoScalingSecurityGroup": "sg-abcd1234", |
| 274 |
"ApplicationDatabase": "dazvlpr01xj55a" |
| 275 |
} |
| 276 |
stack_change_sets: |
| 277 |
description: A list of stack change sets. Each item in the list represents the details of a specific changeset. |
| 278 |
returned: only if O(all_facts) or O(stack_policy) is V(true) and the stack exists |
| 279 |
type: list |
| 280 |
stack_tags: |
| 281 |
description: Dictionary of key value pairs of tags. |
| 282 |
returned: only if O(all_facts) or O(stack_policy) is V(true) and the stack exists |
| 283 |
type: dict |
| 284 |
sample: { |
| 285 |
'TagOne': 'ValueOne', |
| 286 |
'TagTwo': 'ValueTwo' |
| 287 |
} |
| 288 |
""" |
| 289 |
|
| 290 |
import json |
| 291 |
|
| 292 |
try: |
| 293 |
import botocore |
| 294 |
except ImportError: |
| 295 |
pass # Handled by AnsibleAWSModule |
| 296 |
|
| 297 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 298 |
|
| 299 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_message |
| 300 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 301 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 302 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 303 |
|
| 304 |
|
| 305 |
class CloudFormationServiceManager: |
| 306 |
"""Handles CloudFormation Services""" |
| 307 |
|
| 308 |
def __init__(self, module): |
| 309 |
self.module = module |
| 310 |
self.client = module.client("cloudformation") |
| 311 |
|
| 312 |
@AWSRetry.exponential_backoff(retries=5, delay=5) |
| 313 |
def describe_stacks_with_backoff(self, **kwargs): |
| 314 |
paginator = self.client.get_paginator("describe_stacks") |
| 315 |
return paginator.paginate(**kwargs).build_full_result()["Stacks"] |
| 316 |
|
| 317 |
def describe_stacks(self, stack_name=None): |
| 318 |
try: |
| 319 |
kwargs = {"StackName": stack_name} if stack_name else {} |
| 320 |
response = self.describe_stacks_with_backoff(**kwargs) |
| 321 |
if response is not None: |
| 322 |
return response |
| 323 |
self.module.fail_json(msg="Error describing stack(s) - an empty response was returned") |
| 324 |
except is_boto3_error_message("does not exist"): |
| 325 |
return {} |
| 326 |
except ( |
| 327 |
botocore.exceptions.BotoCoreError, |
| 328 |
botocore.exceptions.ClientError, |
| 329 |
) as e: # pylint: disable=duplicate-except |
| 330 |
self.module.fail_json_aws(e, msg="Error describing stack " + stack_name) |
| 331 |
|
| 332 |
@AWSRetry.exponential_backoff(retries=5, delay=5) |
| 333 |
def list_stack_resources_with_backoff(self, stack_name): |
| 334 |
paginator = self.client.get_paginator("list_stack_resources") |
| 335 |
return paginator.paginate(StackName=stack_name).build_full_result()["StackResourceSummaries"] |
| 336 |
|
| 337 |
def list_stack_resources(self, stack_name): |
| 338 |
try: |
| 339 |
return self.list_stack_resources_with_backoff(stack_name) |
| 340 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 341 |
self.module.fail_json_aws(e, msg="Error listing stack resources for stack " + stack_name) |
| 342 |
|
| 343 |
@AWSRetry.exponential_backoff(retries=5, delay=5) |
| 344 |
def describe_stack_events_with_backoff(self, stack_name): |
| 345 |
paginator = self.client.get_paginator("describe_stack_events") |
| 346 |
return paginator.paginate(StackName=stack_name).build_full_result()["StackEvents"] |
| 347 |
|
| 348 |
def describe_stack_events(self, stack_name): |
| 349 |
try: |
| 350 |
return self.describe_stack_events_with_backoff(stack_name) |
| 351 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 352 |
self.module.fail_json_aws(e, msg="Error listing stack events for stack " + stack_name) |
| 353 |
|
| 354 |
@AWSRetry.exponential_backoff(retries=5, delay=5) |
| 355 |
def list_stack_change_sets_with_backoff(self, stack_name): |
| 356 |
paginator = self.client.get_paginator("list_change_sets") |
| 357 |
return paginator.paginate(StackName=stack_name).build_full_result()["Summaries"] |
| 358 |
|
| 359 |
@AWSRetry.exponential_backoff(retries=5, delay=5) |
| 360 |
def describe_stack_change_set_with_backoff(self, **kwargs): |
| 361 |
paginator = self.client.get_paginator("describe_change_set") |
| 362 |
return paginator.paginate(**kwargs).build_full_result() |
| 363 |
|
| 364 |
def describe_stack_change_sets(self, stack_name): |
| 365 |
changes = [] |
| 366 |
try: |
| 367 |
change_sets = self.list_stack_change_sets_with_backoff(stack_name) |
| 368 |
for item in change_sets: |
| 369 |
changes.append( |
| 370 |
self.describe_stack_change_set_with_backoff( |
| 371 |
StackName=stack_name, ChangeSetName=item["ChangeSetName"] |
| 372 |
) |
| 373 |
) |
| 374 |
return changes |
| 375 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 376 |
self.module.fail_json_aws(e, msg="Error describing stack change sets for stack " + stack_name) |
| 377 |
|
| 378 |
@AWSRetry.exponential_backoff(retries=5, delay=5) |
| 379 |
def get_stack_policy_with_backoff(self, stack_name): |
| 380 |
return self.client.get_stack_policy(StackName=stack_name) |
| 381 |
|
| 382 |
def get_stack_policy(self, stack_name): |
| 383 |
try: |
| 384 |
response = self.get_stack_policy_with_backoff(stack_name) |
| 385 |
stack_policy = response.get("StackPolicyBody") |
| 386 |
if stack_policy: |
| 387 |
return json.loads(stack_policy) |
| 388 |
return dict() |
| 389 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 390 |
self.module.fail_json_aws(e, msg="Error getting stack policy for stack " + stack_name) |
| 391 |
|
| 392 |
@AWSRetry.exponential_backoff(retries=5, delay=5) |
| 393 |
def get_template_with_backoff(self, stack_name): |
| 394 |
return self.client.get_template(StackName=stack_name) |
| 395 |
|
| 396 |
def get_template(self, stack_name): |
| 397 |
try: |
| 398 |
response = self.get_template_with_backoff(stack_name) |
| 399 |
return response.get("TemplateBody") |
| 400 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 401 |
self.module.fail_json_aws(e, msg="Error getting stack template for stack " + stack_name) |
| 402 |
|
| 403 |
|
| 404 |
def to_dict(items, key, value): |
| 405 |
"""Transforms a list of items to a Key/Value dictionary""" |
| 406 |
if items: |
| 407 |
return dict(zip([i.get(key) for i in items], [i.get(value) for i in items])) |
| 408 |
else: |
| 409 |
return dict() |
| 410 |
|
| 411 |
|
| 412 |
def main(): |
| 413 |
argument_spec = dict( |
| 414 |
stack_name=dict(), |
| 415 |
all_facts=dict(required=False, default=False, type="bool"), |
| 416 |
stack_policy=dict(required=False, default=False, type="bool"), |
| 417 |
stack_events=dict(required=False, default=False, type="bool"), |
| 418 |
stack_resources=dict(required=False, default=False, type="bool"), |
| 419 |
stack_template=dict(required=False, default=False, type="bool"), |
| 420 |
stack_change_sets=dict(required=False, default=False, type="bool"), |
| 421 |
) |
| 422 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) |
| 423 |
|
| 424 |
service_mgr = CloudFormationServiceManager(module) |
| 425 |
|
| 426 |
result = {"cloudformation": {}} |
| 427 |
|
| 428 |
for stack_description in service_mgr.describe_stacks(module.params.get("stack_name")): |
| 429 |
facts = {"stack_description": stack_description} |
| 430 |
stack_name = stack_description.get("StackName") |
| 431 |
|
| 432 |
# Create stack output and stack parameter dictionaries |
| 433 |
if facts["stack_description"]: |
| 434 |
facts["stack_outputs"] = to_dict(facts["stack_description"].get("Outputs"), "OutputKey", "OutputValue") |
| 435 |
facts["stack_parameters"] = to_dict( |
| 436 |
facts["stack_description"].get("Parameters"), "ParameterKey", "ParameterValue" |
| 437 |
) |
| 438 |
facts["stack_tags"] = boto3_tag_list_to_ansible_dict(facts["stack_description"].get("Tags")) |
| 439 |
|
| 440 |
# Create optional stack outputs |
| 441 |
all_facts = module.params.get("all_facts") |
| 442 |
if all_facts or module.params.get("stack_resources"): |
| 443 |
facts["stack_resource_list"] = service_mgr.list_stack_resources(stack_name) |
| 444 |
facts["stack_resources"] = to_dict( |
| 445 |
facts.get("stack_resource_list"), "LogicalResourceId", "PhysicalResourceId" |
| 446 |
) |
| 447 |
if all_facts or module.params.get("stack_template"): |
| 448 |
facts["stack_template"] = service_mgr.get_template(stack_name) |
| 449 |
if all_facts or module.params.get("stack_policy"): |
| 450 |
facts["stack_policy"] = service_mgr.get_stack_policy(stack_name) |
| 451 |
if all_facts or module.params.get("stack_events"): |
| 452 |
facts["stack_events"] = service_mgr.describe_stack_events(stack_name) |
| 453 |
if all_facts or module.params.get("stack_change_sets"): |
| 454 |
facts["stack_change_sets"] = service_mgr.describe_stack_change_sets(stack_name) |
| 455 |
|
| 456 |
result["cloudformation"][stack_name] = camel_dict_to_snake_dict( |
| 457 |
facts, |
| 458 |
ignore_list=( |
| 459 |
"stack_outputs", |
| 460 |
"stack_parameters", |
| 461 |
"stack_policy", |
| 462 |
"stack_resources", |
| 463 |
"stack_tags", |
| 464 |
"stack_template", |
| 465 |
), |
| 466 |
) |
| 467 |
module.exit_json(changed=False, **result) |
| 468 |
|
| 469 |
|
| 470 |
if __name__ == "__main__": |
| 471 |
main() |