| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2017 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 |
module: elasticache_info |
| 9 |
short_description: Retrieve information for AWS ElastiCache clusters |
| 10 |
version_added: 1.0.0 |
| 11 |
description: |
| 12 |
- Retrieve information from AWS ElastiCache clusters. |
| 13 |
options: |
| 14 |
name: |
| 15 |
description: |
| 16 |
- The name of an ElastiCache cluster. |
| 17 |
type: str |
| 18 |
author: |
| 19 |
- Will Thames (@willthames) |
| 20 |
extends_documentation_fragment: |
| 21 |
- amazon.aws.common.modules |
| 22 |
- amazon.aws.region.modules |
| 23 |
- amazon.aws.boto3 |
| 24 |
""" |
| 25 |
|
| 26 |
EXAMPLES = r""" |
| 27 |
- name: obtain all ElastiCache information |
| 28 |
community.aws.elasticache_info: |
| 29 |
|
| 30 |
- name: obtain all information for a single ElastiCache cluster |
| 31 |
community.aws.elasticache_info: |
| 32 |
name: test_elasticache |
| 33 |
""" |
| 34 |
|
| 35 |
RETURN = r""" |
| 36 |
elasticache_clusters: |
| 37 |
description: List of ElastiCache clusters. |
| 38 |
returned: always |
| 39 |
type: list |
| 40 |
elements: dict |
| 41 |
contains: |
| 42 |
arn: |
| 43 |
description: ARN of the cache cluster. |
| 44 |
returned: always |
| 45 |
type: str |
| 46 |
sample: 'arn:aws:elasticache:us-east-1:123456789012:cluster:ansible-test' |
| 47 |
auto_minor_version_upgrade: |
| 48 |
description: Whether to automatically upgrade to minor versions. |
| 49 |
returned: always |
| 50 |
type: bool |
| 51 |
sample: true |
| 52 |
cache_cluster_create_time: |
| 53 |
description: Date and time cluster was created. |
| 54 |
returned: always |
| 55 |
type: str |
| 56 |
sample: '2017-09-15T05:43:46.038000+00:00' |
| 57 |
cache_cluster_id: |
| 58 |
description: ID of the cache cluster. |
| 59 |
returned: always |
| 60 |
type: str |
| 61 |
sample: abcd-1234-001 |
| 62 |
cache_cluster_status: |
| 63 |
description: Status of ElastiCache cluster. |
| 64 |
returned: always |
| 65 |
type: str |
| 66 |
sample: available |
| 67 |
cache_node_type: |
| 68 |
description: Instance type of ElastiCache nodes. |
| 69 |
returned: always |
| 70 |
type: str |
| 71 |
sample: cache.t2.micro |
| 72 |
cache_nodes: |
| 73 |
description: List of ElastiCache nodes in the cluster. |
| 74 |
returned: always |
| 75 |
type: list |
| 76 |
elements: dict |
| 77 |
contains: |
| 78 |
cache_node_create_time: |
| 79 |
description: Date and time node was created. |
| 80 |
returned: always |
| 81 |
type: str |
| 82 |
sample: '2017-09-15T05:43:46.038000+00:00' |
| 83 |
cache_node_id: |
| 84 |
description: ID of the cache node. |
| 85 |
returned: always |
| 86 |
type: str |
| 87 |
sample: '0001' |
| 88 |
cache_node_status: |
| 89 |
description: Status of the cache node. |
| 90 |
returned: always |
| 91 |
type: str |
| 92 |
sample: available |
| 93 |
customer_availability_zone: |
| 94 |
description: Availability Zone in which the cache node was created. |
| 95 |
returned: always |
| 96 |
type: str |
| 97 |
sample: ap-southeast-2b |
| 98 |
endpoint: |
| 99 |
description: Connection details for the cache node. |
| 100 |
returned: always |
| 101 |
type: dict |
| 102 |
contains: |
| 103 |
address: |
| 104 |
description: URL of the cache node endpoint. |
| 105 |
returned: always |
| 106 |
type: str |
| 107 |
sample: abcd-1234-001.bgiz2p.0001.apse2.cache.amazonaws.com |
| 108 |
port: |
| 109 |
description: Port of the cache node endpoint. |
| 110 |
returned: always |
| 111 |
type: int |
| 112 |
sample: 6379 |
| 113 |
parameter_group_status: |
| 114 |
description: Status of the Cache Parameter Group. |
| 115 |
returned: always |
| 116 |
type: str |
| 117 |
sample: in-sync |
| 118 |
cache_parameter_group: |
| 119 |
description: Contents of the Cache Parameter Group. |
| 120 |
returned: always |
| 121 |
type: dict |
| 122 |
contains: |
| 123 |
cache_node_ids_to_reboot: |
| 124 |
description: Cache nodes which need to be rebooted for parameter changes to be applied. |
| 125 |
returned: always |
| 126 |
type: list |
| 127 |
elements: str |
| 128 |
sample: [] |
| 129 |
cache_parameter_group_name: |
| 130 |
description: Name of the cache parameter group. |
| 131 |
returned: always |
| 132 |
type: str |
| 133 |
sample: default.redis3.2 |
| 134 |
parameter_apply_status: |
| 135 |
description: Status of parameter updates. |
| 136 |
returned: always |
| 137 |
type: str |
| 138 |
sample: in-sync |
| 139 |
cache_security_groups: |
| 140 |
description: Security Groups used by the cache. |
| 141 |
returned: always |
| 142 |
type: list |
| 143 |
elements: str |
| 144 |
sample: |
| 145 |
- 'sg-abcd1234' |
| 146 |
cache_subnet_group_name: |
| 147 |
description: ElastiCache Subnet Group used by the cache. |
| 148 |
returned: always |
| 149 |
type: str |
| 150 |
sample: abcd-subnet-group |
| 151 |
client_download_landing_page: |
| 152 |
description: URL of client download web page. |
| 153 |
returned: always |
| 154 |
type: str |
| 155 |
sample: 'https://console.aws.amazon.com/elasticache/home#client-download:' |
| 156 |
engine: |
| 157 |
description: Engine used by ElastiCache. |
| 158 |
returned: always |
| 159 |
type: str |
| 160 |
sample: redis |
| 161 |
engine_version: |
| 162 |
description: Version of ElastiCache engine. |
| 163 |
returned: always |
| 164 |
type: str |
| 165 |
sample: 3.2.4 |
| 166 |
notification_configuration: |
| 167 |
description: Configuration of notifications. |
| 168 |
returned: if notifications are enabled |
| 169 |
type: dict |
| 170 |
contains: |
| 171 |
topic_arn: |
| 172 |
description: ARN of notification destination topic. |
| 173 |
returned: if notifications are enabled |
| 174 |
type: str |
| 175 |
sample: arn:aws:sns:*:123456789012:my_topic |
| 176 |
topic_name: |
| 177 |
description: Name of notification destination topic. |
| 178 |
returned: if notifications are enabled |
| 179 |
type: str |
| 180 |
sample: MyTopic |
| 181 |
num_cache_nodes: |
| 182 |
description: Number of Cache Nodes. |
| 183 |
returned: always |
| 184 |
type: int |
| 185 |
sample: 1 |
| 186 |
pending_modified_values: |
| 187 |
description: Values that are pending modification. |
| 188 |
returned: always |
| 189 |
type: dict |
| 190 |
preferred_availability_zone: |
| 191 |
description: Preferred Availability Zone. |
| 192 |
returned: always |
| 193 |
type: str |
| 194 |
sample: ap-southeast-2b |
| 195 |
preferred_maintenance_window: |
| 196 |
description: Time slot for preferred maintenance window. |
| 197 |
returned: always |
| 198 |
type: str |
| 199 |
sample: sat:12:00-sat:13:00 |
| 200 |
replication_group: |
| 201 |
description: Informations about the associated replication group. |
| 202 |
version_added: 4.1.0 |
| 203 |
returned: if replication is enabled |
| 204 |
type: dict |
| 205 |
contains: |
| 206 |
arn: |
| 207 |
description: The ARN (Amazon Resource Name) of the replication group. |
| 208 |
returned: always |
| 209 |
type: str |
| 210 |
at_rest_encryption_enabled: |
| 211 |
description: A flag that enables encryption at-rest when set to true. |
| 212 |
returned: always |
| 213 |
type: bool |
| 214 |
auth_token_enabled: |
| 215 |
description: A flag that enables using an AuthToken (password) when issuing Redis commands. |
| 216 |
returned: always |
| 217 |
type: bool |
| 218 |
automatic_failover: |
| 219 |
description: Indicates the status of automatic failover for this Redis replication group. |
| 220 |
returned: always |
| 221 |
type: str |
| 222 |
sample: enabled |
| 223 |
cache_node_type: |
| 224 |
description: The name of the compute and memory capacity node type for each node in the replication group. |
| 225 |
returned: always |
| 226 |
type: str |
| 227 |
sample: cache.t3.medium |
| 228 |
cluster_enabled: |
| 229 |
description: A flag indicating whether or not this replication group is cluster enabled. |
| 230 |
returned: always |
| 231 |
type: bool |
| 232 |
description: |
| 233 |
description: The user supplied description of the replication group. |
| 234 |
returned: always |
| 235 |
type: str |
| 236 |
global_replication_group_info: |
| 237 |
description: The name of the Global datastore and role of this replication group in the Global datastore. |
| 238 |
returned: always |
| 239 |
type: dict |
| 240 |
contains: |
| 241 |
global_replication_group_id: |
| 242 |
description: The name of the Global datastore. |
| 243 |
returned: always |
| 244 |
type: str |
| 245 |
global_replication_group_member_role: |
| 246 |
description: The role of the replication group in a Global datastore. Can be primary or secondary. |
| 247 |
returned: always |
| 248 |
type: str |
| 249 |
kms_key_id: |
| 250 |
description: The ID of the KMS key used to encrypt the disk in the cluster. |
| 251 |
returned: always |
| 252 |
type: str |
| 253 |
member_clusters: |
| 254 |
description: The names of all the cache clusters that are part of this replication group. |
| 255 |
returned: always |
| 256 |
type: list |
| 257 |
elements: str |
| 258 |
multi_az: |
| 259 |
description: A flag indicating if you have Multi-AZ enabled to enhance fault tolerance. |
| 260 |
returned: always |
| 261 |
type: str |
| 262 |
sample: enabled |
| 263 |
node_groups: |
| 264 |
description: A list of node groups in this replication group. |
| 265 |
returned: always |
| 266 |
type: list |
| 267 |
elements: dict |
| 268 |
contains: |
| 269 |
node_group_id: |
| 270 |
description: The identifier for the node group (shard). |
| 271 |
returned: always |
| 272 |
type: str |
| 273 |
node_group_members: |
| 274 |
description: A list containing information about individual nodes within the node group (shard). |
| 275 |
returned: always |
| 276 |
type: list |
| 277 |
elements: dict |
| 278 |
contains: |
| 279 |
cache_cluster_id: |
| 280 |
description: The ID of the cluster to which the node belongs. |
| 281 |
returned: always |
| 282 |
type: str |
| 283 |
cache_node_id: |
| 284 |
description: The ID of the node within its cluster. |
| 285 |
returned: always |
| 286 |
type: str |
| 287 |
current_role: |
| 288 |
description: The role that is currently assigned to the node - primary or replica. |
| 289 |
returned: always |
| 290 |
type: str |
| 291 |
sample: primary |
| 292 |
preferred_availability_zone: |
| 293 |
description: The name of the Availability Zone in which the node is located. |
| 294 |
returned: always |
| 295 |
type: str |
| 296 |
read_endpoint: |
| 297 |
description: The information required for client programs to connect to a node for read operations. |
| 298 |
returned: always |
| 299 |
type: list |
| 300 |
elements: dict |
| 301 |
contains: |
| 302 |
address: |
| 303 |
description: The DNS hostname of the cache node. |
| 304 |
returned: always |
| 305 |
type: str |
| 306 |
port: |
| 307 |
description: The port number that the cache engine is listening on. |
| 308 |
returned: always |
| 309 |
type: int |
| 310 |
sample: 6379 |
| 311 |
primary_endpoint: |
| 312 |
description: The endpoint of the primary node in this node group (shard). |
| 313 |
returned: always |
| 314 |
type: list |
| 315 |
elements: dict |
| 316 |
contains: |
| 317 |
address: |
| 318 |
description: The DNS hostname of the cache node. |
| 319 |
returned: always |
| 320 |
type: str |
| 321 |
port: |
| 322 |
description: The port number that the cache engine is listening on. |
| 323 |
returned: always |
| 324 |
type: int |
| 325 |
sample: 6379 |
| 326 |
reader_endpoint: |
| 327 |
description: The endpoint of the cache node. |
| 328 |
returned: always |
| 329 |
type: dict |
| 330 |
contains: |
| 331 |
address: |
| 332 |
description: The DNS hostname of the cache node. |
| 333 |
returned: always |
| 334 |
type: str |
| 335 |
port: |
| 336 |
description: The port number that the cache engine is listening on. |
| 337 |
returned: always |
| 338 |
type: int |
| 339 |
sample: 6379 |
| 340 |
status: |
| 341 |
description: The current state of this replication group - C(creating), C(available), C(modifying), C(deleting). |
| 342 |
returned: always |
| 343 |
type: str |
| 344 |
sample: available |
| 345 |
pending_modified_values: |
| 346 |
description: A group of settings to be applied to the replication group, either immediately or during the next maintenance window. |
| 347 |
returned: always |
| 348 |
type: dict |
| 349 |
replication_group_id: |
| 350 |
description: Replication Group Id. |
| 351 |
returned: always |
| 352 |
type: str |
| 353 |
sample: replication-001 |
| 354 |
snapshot_retention_limit: |
| 355 |
description: The number of days for which ElastiCache retains automatic cluster snapshots before deleting them. |
| 356 |
returned: always |
| 357 |
type: int |
| 358 |
snapshot_window: |
| 359 |
description: The daily time range (in UTC) during which ElastiCache begins taking a daily snapshot of your node group (shard). |
| 360 |
returned: always |
| 361 |
type: str |
| 362 |
sample: 07:00-09:00 |
| 363 |
snapshotting_cluster_id: |
| 364 |
description: The cluster ID that is used as the daily snapshot source for the replication group. |
| 365 |
returned: always |
| 366 |
type: str |
| 367 |
status: |
| 368 |
description: The current state of this replication group - C(creating), C(available), C(modifying), C(deleting), C(create-failed), C(snapshotting) |
| 369 |
returned: always |
| 370 |
type: str |
| 371 |
transit_encryption_enabled: |
| 372 |
description: A flag that enables in-transit encryption when set to C(true). |
| 373 |
returned: always |
| 374 |
type: bool |
| 375 |
replication_group_id: |
| 376 |
description: Replication Group Id. |
| 377 |
returned: if replication is enabled |
| 378 |
type: str |
| 379 |
sample: replication-001 |
| 380 |
security_groups: |
| 381 |
description: List of Security Groups associated with ElastiCache. |
| 382 |
returned: always |
| 383 |
type: list |
| 384 |
elements: dict |
| 385 |
contains: |
| 386 |
security_group_id: |
| 387 |
description: Security Group ID |
| 388 |
returned: always |
| 389 |
type: str |
| 390 |
sample: sg-abcd1234 |
| 391 |
status: |
| 392 |
description: Status of Security Group |
| 393 |
returned: always |
| 394 |
type: str |
| 395 |
sample: active |
| 396 |
tags: |
| 397 |
description: Tags applied to the ElastiCache cluster |
| 398 |
returned: always |
| 399 |
type: dict |
| 400 |
sample: |
| 401 |
Application: web |
| 402 |
Environment: test |
| 403 |
""" |
| 404 |
|
| 405 |
try: |
| 406 |
import botocore |
| 407 |
except ImportError: |
| 408 |
pass # caught by AnsibleAWSModule |
| 409 |
|
| 410 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 411 |
|
| 412 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code |
| 413 |
from ansible_collections.amazon.aws.plugins.module_utils.iam import get_aws_account_info |
| 414 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 415 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 416 |
|
| 417 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 418 |
|
| 419 |
|
| 420 |
@AWSRetry.exponential_backoff() |
| 421 |
def describe_cache_clusters_with_backoff(client, cluster_id=None): |
| 422 |
paginator = client.get_paginator("describe_cache_clusters") |
| 423 |
params = dict(ShowCacheNodeInfo=True) |
| 424 |
if cluster_id: |
| 425 |
params["CacheClusterId"] = cluster_id |
| 426 |
try: |
| 427 |
response = paginator.paginate(**params).build_full_result() |
| 428 |
except is_boto3_error_code("CacheClusterNotFound"): |
| 429 |
return [] |
| 430 |
return response["CacheClusters"] |
| 431 |
|
| 432 |
|
| 433 |
@AWSRetry.exponential_backoff() |
| 434 |
def describe_replication_group_with_backoff(client, replication_group_id): |
| 435 |
try: |
| 436 |
response = client.describe_replication_groups(ReplicationGroupId=replication_group_id) |
| 437 |
except is_boto3_error_code("ReplicationGroupNotFoundFault"): |
| 438 |
return None |
| 439 |
|
| 440 |
return response["ReplicationGroups"][0] |
| 441 |
|
| 442 |
|
| 443 |
@AWSRetry.exponential_backoff() |
| 444 |
def get_elasticache_tags_with_backoff(client, cluster_id): |
| 445 |
return client.list_tags_for_resource(ResourceName=cluster_id)["TagList"] |
| 446 |
|
| 447 |
|
| 448 |
def get_elasticache_clusters(client, module): |
| 449 |
region = module.region |
| 450 |
try: |
| 451 |
clusters = describe_cache_clusters_with_backoff(client, cluster_id=module.params.get("name")) |
| 452 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 453 |
module.fail_json_aws(e, msg="Couldn't obtain cache cluster info") |
| 454 |
|
| 455 |
account_id, partition = get_aws_account_info(module) |
| 456 |
results = [] |
| 457 |
for cluster in clusters: |
| 458 |
cluster = camel_dict_to_snake_dict(cluster) |
| 459 |
arn = f"arn:{partition}:elasticache:{region}:{account_id}:cluster:{cluster['cache_cluster_id']}" |
| 460 |
try: |
| 461 |
tags = get_elasticache_tags_with_backoff(client, arn) |
| 462 |
except is_boto3_error_code("CacheClusterNotFound"): |
| 463 |
# e.g: Cluster was listed but is in deleting state |
| 464 |
continue |
| 465 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 466 |
module.fail_json_aws(e, msg=f"Couldn't get tags for cluster {cluster['cache_cluster_id']}") |
| 467 |
|
| 468 |
cluster["tags"] = boto3_tag_list_to_ansible_dict(tags) |
| 469 |
|
| 470 |
if cluster.get("replication_group_id", None): |
| 471 |
try: |
| 472 |
replication_group = describe_replication_group_with_backoff(client, cluster["replication_group_id"]) |
| 473 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 474 |
module.fail_json_aws(e, msg="Couldn't obtain replication group info") |
| 475 |
|
| 476 |
if replication_group is not None: |
| 477 |
replication_group = camel_dict_to_snake_dict(replication_group) |
| 478 |
cluster["replication_group"] = replication_group |
| 479 |
|
| 480 |
results.append(cluster) |
| 481 |
return results |
| 482 |
|
| 483 |
|
| 484 |
def main(): |
| 485 |
argument_spec = dict( |
| 486 |
name=dict(required=False), |
| 487 |
) |
| 488 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) |
| 489 |
|
| 490 |
client = module.client("elasticache") |
| 491 |
|
| 492 |
module.exit_json(elasticache_clusters=get_elasticache_clusters(client, module)) |
| 493 |
|
| 494 |
|
| 495 |
if __name__ == "__main__": |
| 496 |
main() |