| 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: redshift_info |
| 10 |
version_added: 1.0.0 |
| 11 |
author: |
| 12 |
- "Jens Carl (@j-carl)" |
| 13 |
short_description: Gather information about Redshift cluster(s) |
| 14 |
description: |
| 15 |
- Gather information about Redshift cluster(s). |
| 16 |
options: |
| 17 |
cluster_identifier: |
| 18 |
description: |
| 19 |
- The prefix of cluster identifier of the Redshift cluster you are searching for. |
| 20 |
- "This is a regular expression match with implicit '^'. Append '$' for a complete match." |
| 21 |
required: false |
| 22 |
aliases: ['name', 'identifier'] |
| 23 |
type: str |
| 24 |
tags: |
| 25 |
description: |
| 26 |
- "A dictionary/hash of tags in the format { tag1_name: 'tag1_value', tag2_name: 'tag2_value' } |
| 27 |
to match against the security group(s) you are searching for." |
| 28 |
required: false |
| 29 |
type: dict |
| 30 |
extends_documentation_fragment: |
| 31 |
- amazon.aws.region.modules |
| 32 |
- amazon.aws.common.modules |
| 33 |
- amazon.aws.boto3 |
| 34 |
""" |
| 35 |
|
| 36 |
EXAMPLES = r""" |
| 37 |
# Note: These examples do net set authentication details, see the AWS guide for details. |
| 38 |
|
| 39 |
- name: Find all clusters |
| 40 |
community.aws.redshift_info: |
| 41 |
register: redshift |
| 42 |
|
| 43 |
- name: Find cluster(s) with matching tags |
| 44 |
community.aws.redshift_info: |
| 45 |
tags: |
| 46 |
env: prd |
| 47 |
stack: monitoring |
| 48 |
register: redshift_tags |
| 49 |
|
| 50 |
- name: Find cluster(s) with matching name/prefix and tags |
| 51 |
community.aws.redshift_info: |
| 52 |
tags: |
| 53 |
env: dev |
| 54 |
stack: web |
| 55 |
name: user- |
| 56 |
register: redshift_web |
| 57 |
|
| 58 |
- name: Fail if no cluster(s) is/are found |
| 59 |
community.aws.redshift_info: |
| 60 |
tags: |
| 61 |
env: stg |
| 62 |
stack: db |
| 63 |
register: redshift_user |
| 64 |
failed_when: redshift_user.results | length == 0 |
| 65 |
""" |
| 66 |
|
| 67 |
RETURN = r""" |
| 68 |
# For more information see U(http://boto3.readthedocs.io/en/latest/reference/services/redshift.html#Redshift.Client.describe_clusters) |
| 69 |
--- |
| 70 |
cluster_identifier: |
| 71 |
description: Unique key to identify the cluster. |
| 72 |
returned: success |
| 73 |
type: str |
| 74 |
sample: "redshift-identifier" |
| 75 |
node_type: |
| 76 |
description: The node type for nodes in the cluster. |
| 77 |
returned: success |
| 78 |
type: str |
| 79 |
sample: "ds2.xlarge" |
| 80 |
cluster_status: |
| 81 |
description: Current state of the cluster. |
| 82 |
returned: success |
| 83 |
type: str |
| 84 |
sample: "available" |
| 85 |
modify_status: |
| 86 |
description: The status of a modify operation. |
| 87 |
returned: optional |
| 88 |
type: str |
| 89 |
sample: "" |
| 90 |
master_username: |
| 91 |
description: The master user name for the cluster. |
| 92 |
returned: success |
| 93 |
type: str |
| 94 |
sample: "admin" |
| 95 |
db_name: |
| 96 |
description: The name of the initial database that was created when the cluster was created. |
| 97 |
returned: success |
| 98 |
type: str |
| 99 |
sample: "dev" |
| 100 |
endpoint: |
| 101 |
description: The connection endpoint. |
| 102 |
returned: success |
| 103 |
type: str |
| 104 |
sample: { |
| 105 |
"address": "cluster-ds2.ocmugla0rf.us-east-1.redshift.amazonaws.com", |
| 106 |
"port": 5439 |
| 107 |
} |
| 108 |
cluster_create_time: |
| 109 |
description: The date and time that the cluster was created. |
| 110 |
returned: success |
| 111 |
type: str |
| 112 |
sample: "2016-05-10T08:33:16.629000+00:00" |
| 113 |
automated_snapshot_retention_period: |
| 114 |
description: The number of days that automatic cluster snapshots are retained. |
| 115 |
returned: success |
| 116 |
type: int |
| 117 |
sample: 1 |
| 118 |
cluster_security_groups: |
| 119 |
description: A list of cluster security groups that are associated with the cluster. |
| 120 |
returned: success |
| 121 |
type: list |
| 122 |
sample: [] |
| 123 |
vpc_security_groups: |
| 124 |
description: A list of VPC security groups the are associated with the cluster. |
| 125 |
returned: success |
| 126 |
type: list |
| 127 |
sample: [ |
| 128 |
{ |
| 129 |
"status": "active", |
| 130 |
"vpc_security_group_id": "sg-12cghhg" |
| 131 |
} |
| 132 |
] |
| 133 |
cluster_paramater_groups: |
| 134 |
description: The list of cluster parameters that are associated with this cluster. |
| 135 |
returned: success |
| 136 |
type: list |
| 137 |
sample: [ |
| 138 |
{ |
| 139 |
"cluster_parameter_status_list": [ |
| 140 |
{ |
| 141 |
"parameter_apply_status": "in-sync", |
| 142 |
"parameter_name": "statement_timeout" |
| 143 |
}, |
| 144 |
{ |
| 145 |
"parameter_apply_status": "in-sync", |
| 146 |
"parameter_name": "require_ssl" |
| 147 |
} |
| 148 |
], |
| 149 |
"parameter_apply_status": "in-sync", |
| 150 |
"parameter_group_name": "tuba" |
| 151 |
} |
| 152 |
] |
| 153 |
cluster_subnet_group_name: |
| 154 |
description: The name of the subnet group that is associated with the cluster. |
| 155 |
returned: success |
| 156 |
type: str |
| 157 |
sample: "redshift-subnet" |
| 158 |
vpc_id: |
| 159 |
description: The identifier of the VPC the cluster is in, if the cluster is in a VPC. |
| 160 |
returned: success |
| 161 |
type: str |
| 162 |
sample: "vpc-1234567" |
| 163 |
availability_zone: |
| 164 |
description: The name of the Availability Zone in which the cluster is located. |
| 165 |
returned: success |
| 166 |
type: str |
| 167 |
sample: "us-east-1b" |
| 168 |
preferred_maintenance_window: |
| 169 |
description: The weekly time range, in Universal Coordinated Time (UTC), during which system maintenance can occur. |
| 170 |
returned: success |
| 171 |
type: str |
| 172 |
sample: "tue:07:30-tue:08:00" |
| 173 |
pending_modified_values: |
| 174 |
description: A value that, if present, indicates that changes to the cluster are pending. |
| 175 |
returned: success |
| 176 |
type: dict |
| 177 |
sample: {} |
| 178 |
cluster_version: |
| 179 |
description: The version ID of the Amazon Redshift engine that is running on the cluster. |
| 180 |
returned: success |
| 181 |
type: str |
| 182 |
sample: "1.0" |
| 183 |
allow_version_upgrade: |
| 184 |
description: > |
| 185 |
A Boolean value that, if true, indicates that major version upgrades will be applied |
| 186 |
automatically to the cluster during the maintenance window. |
| 187 |
returned: success |
| 188 |
type: bool |
| 189 |
sample: true|false |
| 190 |
number_of_nodes: |
| 191 |
description: The number of compute nodes in the cluster. |
| 192 |
returned: success |
| 193 |
type: int |
| 194 |
sample: 12 |
| 195 |
publicly_accessible: |
| 196 |
description: A Boolean value that, if true , indicates that the cluster can be accessed from a public network. |
| 197 |
returned: success |
| 198 |
type: bool |
| 199 |
sample: true|false |
| 200 |
encrypted: |
| 201 |
description: Boolean value that, if true , indicates that data in the cluster is encrypted at rest. |
| 202 |
returned: success |
| 203 |
type: bool |
| 204 |
sample: true|false |
| 205 |
restore_status: |
| 206 |
description: A value that describes the status of a cluster restore action. |
| 207 |
returned: success |
| 208 |
type: dict |
| 209 |
sample: {} |
| 210 |
hsm_status: |
| 211 |
description: > |
| 212 |
A value that reports whether the Amazon Redshift cluster has finished applying any hardware |
| 213 |
security module (HSM) settings changes specified in a modify cluster command. |
| 214 |
returned: success |
| 215 |
type: dict |
| 216 |
sample: {} |
| 217 |
cluster_snapshot_copy_status: |
| 218 |
description: A value that returns the destination region and retention period that are configured for cross-region snapshot copy. |
| 219 |
returned: success |
| 220 |
type: dict |
| 221 |
sample: {} |
| 222 |
cluster_public_keys: |
| 223 |
description: The public key for the cluster. |
| 224 |
returned: success |
| 225 |
type: str |
| 226 |
sample: "ssh-rsa anjigfam Amazon-Redshift\n" |
| 227 |
cluster_nodes: |
| 228 |
description: The nodes in the cluster. |
| 229 |
returned: success |
| 230 |
type: list |
| 231 |
sample: [ |
| 232 |
{ |
| 233 |
"node_role": "LEADER", |
| 234 |
"private_ip_address": "10.0.0.1", |
| 235 |
"public_ip_address": "x.x.x.x" |
| 236 |
}, |
| 237 |
{ |
| 238 |
"node_role": "COMPUTE-1", |
| 239 |
"private_ip_address": "10.0.0.3", |
| 240 |
"public_ip_address": "x.x.x.x" |
| 241 |
} |
| 242 |
] |
| 243 |
elastic_ip_status: |
| 244 |
description: The status of the elastic IP (EIP) address. |
| 245 |
returned: success |
| 246 |
type: dict |
| 247 |
sample: {} |
| 248 |
cluster_revision_number: |
| 249 |
description: The specific revision number of the database in the cluster. |
| 250 |
returned: success |
| 251 |
type: str |
| 252 |
sample: "1231" |
| 253 |
tags: |
| 254 |
description: The list of tags for the cluster. |
| 255 |
returned: success |
| 256 |
type: list |
| 257 |
sample: [] |
| 258 |
kms_key_id: |
| 259 |
description: The AWS Key Management Service (AWS KMS) key ID of the encryption key used to encrypt data in the cluster. |
| 260 |
returned: success |
| 261 |
type: str |
| 262 |
sample: "" |
| 263 |
enhanced_vpc_routing: |
| 264 |
description: An option that specifies whether to create the cluster with enhanced VPC routing enabled. |
| 265 |
returned: success |
| 266 |
type: bool |
| 267 |
sample: true|false |
| 268 |
iam_roles: |
| 269 |
description: List of IAM roles attached to the cluster. |
| 270 |
returned: success |
| 271 |
type: list |
| 272 |
sample: [] |
| 273 |
""" |
| 274 |
|
| 275 |
import re |
| 276 |
|
| 277 |
try: |
| 278 |
from botocore.exceptions import BotoCoreError |
| 279 |
from botocore.exceptions import ClientError |
| 280 |
except ImportError: |
| 281 |
pass # caught by AnsibleAWSModule |
| 282 |
|
| 283 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 284 |
|
| 285 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 286 |
|
| 287 |
|
| 288 |
def match_tags(tags_to_match, cluster): |
| 289 |
for key, value in tags_to_match.items(): |
| 290 |
for tag in cluster["Tags"]: |
| 291 |
if key == tag["Key"] and value == tag["Value"]: |
| 292 |
return True |
| 293 |
|
| 294 |
return False |
| 295 |
|
| 296 |
|
| 297 |
def find_clusters(conn, module, identifier=None, tags=None): |
| 298 |
try: |
| 299 |
cluster_paginator = conn.get_paginator("describe_clusters") |
| 300 |
clusters = cluster_paginator.paginate().build_full_result() |
| 301 |
except (BotoCoreError, ClientError) as e: |
| 302 |
module.fail_json_aws(e, msg="Failed to fetch clusters.") |
| 303 |
|
| 304 |
matched_clusters = [] |
| 305 |
|
| 306 |
if identifier is not None: |
| 307 |
identifier_prog = re.compile("^" + identifier) |
| 308 |
|
| 309 |
for cluster in clusters["Clusters"]: |
| 310 |
matched_identifier = True |
| 311 |
if identifier: |
| 312 |
matched_identifier = identifier_prog.search(cluster["ClusterIdentifier"]) |
| 313 |
|
| 314 |
matched_tags = True |
| 315 |
if tags: |
| 316 |
matched_tags = match_tags(tags, cluster) |
| 317 |
|
| 318 |
if matched_identifier and matched_tags: |
| 319 |
matched_clusters.append(camel_dict_to_snake_dict(cluster)) |
| 320 |
|
| 321 |
return matched_clusters |
| 322 |
|
| 323 |
|
| 324 |
def main(): |
| 325 |
argument_spec = dict( |
| 326 |
cluster_identifier=dict(type="str", aliases=["identifier", "name"]), |
| 327 |
tags=dict(type="dict"), |
| 328 |
) |
| 329 |
module = AnsibleAWSModule( |
| 330 |
argument_spec=argument_spec, |
| 331 |
supports_check_mode=True, |
| 332 |
) |
| 333 |
|
| 334 |
cluster_identifier = module.params.get("cluster_identifier") |
| 335 |
cluster_tags = module.params.get("tags") |
| 336 |
|
| 337 |
redshift = module.client("redshift") |
| 338 |
|
| 339 |
results = find_clusters(redshift, module, identifier=cluster_identifier, tags=cluster_tags) |
| 340 |
module.exit_json(results=results) |
| 341 |
|
| 342 |
|
| 343 |
if __name__ == "__main__": |
| 344 |
main() |