| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2021, Daniil Kupchenko (@oukooveu) |
| 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: msk_cluster |
| 10 |
short_description: Manage Amazon MSK clusters |
| 11 |
version_added: "2.0.0" |
| 12 |
description: |
| 13 |
- Create, delete and modify Amazon MSK (Managed Streaming for Apache Kafka) clusters. |
| 14 |
- Prior to release 5.0.0 this module was called C(community.aws.aws_msk_cluster). |
| 15 |
The usage did not change. |
| 16 |
author: |
| 17 |
- Daniil Kupchenko (@oukooveu) |
| 18 |
options: |
| 19 |
state: |
| 20 |
description: Create (C(present)) or delete (C(absent)) cluster. |
| 21 |
choices: ['present', 'absent'] |
| 22 |
type: str |
| 23 |
default: 'present' |
| 24 |
name: |
| 25 |
description: The name of the cluster. |
| 26 |
required: true |
| 27 |
type: str |
| 28 |
version: |
| 29 |
description: |
| 30 |
- The version of Apache Kafka. |
| 31 |
- This version should exist in given configuration. |
| 32 |
- This parameter is required when I(state=present). |
| 33 |
type: str |
| 34 |
configuration_arn: |
| 35 |
description: |
| 36 |
- ARN of the configuration to use. |
| 37 |
- This parameter is required when I(state=present). |
| 38 |
type: str |
| 39 |
configuration_revision: |
| 40 |
description: |
| 41 |
- The revision of the configuration to use. |
| 42 |
- This parameter is required when I(state=present). |
| 43 |
type: int |
| 44 |
nodes: |
| 45 |
description: The number of broker nodes in the cluster. Should be greater or equal to two. |
| 46 |
type: int |
| 47 |
default: 3 |
| 48 |
instance_type: |
| 49 |
description: |
| 50 |
- The type of Amazon EC2 instances to use for Kafka brokers. |
| 51 |
choices: |
| 52 |
- kafka.t3.small |
| 53 |
- kafka.m5.large |
| 54 |
- kafka.m5.xlarge |
| 55 |
- kafka.m5.2xlarge |
| 56 |
- kafka.m5.4xlarge |
| 57 |
- kafka.m5.8xlarge |
| 58 |
- kafka.m5.12xlarge |
| 59 |
- kafka.m5.16xlarge |
| 60 |
- kafka.m5.24xlarge |
| 61 |
- kafka.m7g.large |
| 62 |
- kafka.m7g.xlarge |
| 63 |
- kafka.m7g.2xlarge |
| 64 |
- kafka.m7g.4xlarge |
| 65 |
- kafka.m7g.8xlarge |
| 66 |
- kafka.m7g.12xlarge |
| 67 |
- kafka.m7g.16xlarge |
| 68 |
default: kafka.t3.small |
| 69 |
type: str |
| 70 |
ebs_volume_size: |
| 71 |
description: The size in GiB of the EBS volume for the data drive on each broker node. |
| 72 |
type: int |
| 73 |
default: 100 |
| 74 |
subnets: |
| 75 |
description: |
| 76 |
- The list of subnets to connect to in the client virtual private cloud (VPC). |
| 77 |
AWS creates elastic network interfaces inside these subnets. Client applications use |
| 78 |
elastic network interfaces to produce and consume data. |
| 79 |
- Client subnets can't be in Availability Zone us-east-1e. |
| 80 |
- This parameter is required when I(state=present). |
| 81 |
type: list |
| 82 |
elements: str |
| 83 |
security_groups: |
| 84 |
description: |
| 85 |
- The AWS security groups to associate with the elastic network interfaces in order to specify |
| 86 |
who can connect to and communicate with the Amazon MSK cluster. |
| 87 |
If you don't specify a security group, Amazon MSK uses the default security group associated with the VPC. |
| 88 |
type: list |
| 89 |
elements: str |
| 90 |
encryption: |
| 91 |
description: |
| 92 |
- Includes all encryption-related information. |
| 93 |
- Effective only for new cluster and can not be updated. |
| 94 |
type: dict |
| 95 |
suboptions: |
| 96 |
kms_key_id: |
| 97 |
description: |
| 98 |
- The ARN of the AWS KMS key for encrypting data at rest. If you don't specify a KMS key, MSK creates one for you and uses it. |
| 99 |
default: Null |
| 100 |
type: str |
| 101 |
in_transit: |
| 102 |
description: The details for encryption in transit. |
| 103 |
type: dict |
| 104 |
suboptions: |
| 105 |
in_cluster: |
| 106 |
description: |
| 107 |
- When set to true, it indicates that data communication among the broker nodes of the cluster is encrypted. |
| 108 |
When set to false, the communication happens in plaintext. |
| 109 |
type: bool |
| 110 |
default: True |
| 111 |
client_broker: |
| 112 |
description: |
| 113 |
- Indicates the encryption setting for data in transit between clients and brokers. The following are the possible values. |
| 114 |
TLS means that client-broker communication is enabled with TLS only. |
| 115 |
TLS_PLAINTEXT means that client-broker communication is enabled for both TLS-encrypted, as well as plaintext data. |
| 116 |
PLAINTEXT means that client-broker communication is enabled in plaintext only. |
| 117 |
choices: |
| 118 |
- TLS |
| 119 |
- TLS_PLAINTEXT |
| 120 |
- PLAINTEXT |
| 121 |
type: str |
| 122 |
default: TLS |
| 123 |
authentication: |
| 124 |
description: |
| 125 |
- Includes all client authentication related information. |
| 126 |
- Effective only for new cluster and can not be updated. |
| 127 |
type: dict |
| 128 |
suboptions: |
| 129 |
tls_ca_arn: |
| 130 |
description: List of ACM Certificate Authority ARNs. |
| 131 |
type: list |
| 132 |
elements: str |
| 133 |
sasl_scram: |
| 134 |
description: SASL/SCRAM authentication is enabled or not. |
| 135 |
type: bool |
| 136 |
sasl_iam: |
| 137 |
version_added: 5.5.0 |
| 138 |
description: IAM authentication is enabled or not. |
| 139 |
type: bool |
| 140 |
unauthenticated: |
| 141 |
version_added: 5.5.0 |
| 142 |
description: Option to explicitly turn on or off authentication |
| 143 |
type: bool |
| 144 |
default: True |
| 145 |
enhanced_monitoring: |
| 146 |
description: Specifies the level of monitoring for the MSK cluster. |
| 147 |
choices: |
| 148 |
- DEFAULT |
| 149 |
- PER_BROKER |
| 150 |
- PER_TOPIC_PER_BROKER |
| 151 |
- PER_TOPIC_PER_PARTITION |
| 152 |
default: DEFAULT |
| 153 |
type: str |
| 154 |
open_monitoring: |
| 155 |
description: The settings for open monitoring. |
| 156 |
type: dict |
| 157 |
suboptions: |
| 158 |
jmx_exporter: |
| 159 |
description: Indicates whether you want to enable or disable the JMX Exporter. |
| 160 |
type: bool |
| 161 |
default: False |
| 162 |
node_exporter: |
| 163 |
description: Indicates whether you want to enable or disable the Node Exporter. |
| 164 |
type: bool |
| 165 |
default: False |
| 166 |
logging: |
| 167 |
description: Logging configuration. |
| 168 |
type: dict |
| 169 |
suboptions: |
| 170 |
cloudwatch: |
| 171 |
description: Details of the CloudWatch Logs destination for broker logs. |
| 172 |
type: dict |
| 173 |
suboptions: |
| 174 |
enabled: |
| 175 |
description: Specifies whether broker logs get sent to the specified CloudWatch Logs destination. |
| 176 |
type: bool |
| 177 |
default: False |
| 178 |
log_group: |
| 179 |
description: The CloudWatch log group that is the destination for broker logs. |
| 180 |
type: str |
| 181 |
required: False |
| 182 |
firehose: |
| 183 |
description: Details of the Kinesis Data Firehose delivery stream that is the destination for broker logs. |
| 184 |
type: dict |
| 185 |
suboptions: |
| 186 |
enabled: |
| 187 |
description: Specifies whether broker logs get send to the specified Kinesis Data Firehose delivery stream. |
| 188 |
type: bool |
| 189 |
default: False |
| 190 |
delivery_stream: |
| 191 |
description: The Kinesis Data Firehose delivery stream that is the destination for broker logs. |
| 192 |
type: str |
| 193 |
required: False |
| 194 |
s3: |
| 195 |
description: Details of the Amazon S3 destination for broker logs. |
| 196 |
type: dict |
| 197 |
suboptions: |
| 198 |
enabled: |
| 199 |
description: Specifies whether broker logs get sent to the specified Amazon S3 destination. |
| 200 |
type: bool |
| 201 |
default: False |
| 202 |
bucket: |
| 203 |
description: The name of the S3 bucket that is the destination for broker logs. |
| 204 |
type: str |
| 205 |
required: False |
| 206 |
prefix: |
| 207 |
description: The S3 prefix that is the destination for broker logs. |
| 208 |
type: str |
| 209 |
required: False |
| 210 |
wait: |
| 211 |
description: Whether to wait for the cluster to be available or deleted. |
| 212 |
type: bool |
| 213 |
default: false |
| 214 |
wait_timeout: |
| 215 |
description: How many seconds to wait. Cluster creation can take up to 20-30 minutes. |
| 216 |
type: int |
| 217 |
default: 3600 |
| 218 |
notes: |
| 219 |
- All operations are time consuming, for example create takes 20-30 minutes, |
| 220 |
update kafka version -- more than one hour, update configuration -- 10-15 minutes; |
| 221 |
- Cluster's brokers get evenly distributed over a number of availability zones |
| 222 |
that's equal to the number of subnets. |
| 223 |
extends_documentation_fragment: |
| 224 |
- amazon.aws.common.modules |
| 225 |
- amazon.aws.region.modules |
| 226 |
- amazon.aws.boto3 |
| 227 |
- amazon.aws.tags |
| 228 |
""" |
| 229 |
|
| 230 |
EXAMPLES = r""" |
| 231 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 232 |
|
| 233 |
- community.aws.msk_cluster: |
| 234 |
name: kafka-cluster |
| 235 |
state: present |
| 236 |
version: 2.6.1 |
| 237 |
nodes: 6 |
| 238 |
ebs_volume_size: "{{ aws_msk_options.ebs_volume_size }}" |
| 239 |
subnets: |
| 240 |
- subnet-e3b48ce7c25861eeb |
| 241 |
- subnet-2990c8b25b07ddd43 |
| 242 |
- subnet-d9fbeaf46c54bfab6 |
| 243 |
wait: true |
| 244 |
wait_timeout: 1800 |
| 245 |
configuration_arn: arn:aws:kafka:us-east-1:123456789012:configuration/kafka-cluster-configuration/aaaaaaaa-bbbb-4444-3333-ccccccccc-1 |
| 246 |
configuration_revision: 1 |
| 247 |
|
| 248 |
- community.aws.msk_cluster: |
| 249 |
name: kafka-cluster |
| 250 |
state: absent |
| 251 |
""" |
| 252 |
|
| 253 |
RETURN = r""" |
| 254 |
# These are examples of possible return values, and in general should use other names for return values. |
| 255 |
|
| 256 |
bootstrap_broker_string: |
| 257 |
description: A list of brokers that a client application can use to bootstrap. |
| 258 |
type: complex |
| 259 |
contains: |
| 260 |
plain: |
| 261 |
description: A string containing one or more hostname:port pairs. |
| 262 |
type: str |
| 263 |
tls: |
| 264 |
description: A string containing one or more DNS names (or IP) and TLS port pairs. |
| 265 |
type: str |
| 266 |
returned: I(state=present) and cluster state is I(ACTIVE) |
| 267 |
cluster_info: |
| 268 |
description: Description of the MSK cluster. |
| 269 |
type: dict |
| 270 |
returned: I(state=present) |
| 271 |
response: |
| 272 |
description: The response from actual API call. |
| 273 |
type: dict |
| 274 |
returned: always |
| 275 |
sample: {} |
| 276 |
""" |
| 277 |
|
| 278 |
import time |
| 279 |
|
| 280 |
try: |
| 281 |
import botocore |
| 282 |
except ImportError: |
| 283 |
pass # handled by AnsibleAWSModule |
| 284 |
|
| 285 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 286 |
|
| 287 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 288 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import compare_aws_tags |
| 289 |
|
| 290 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 291 |
|
| 292 |
|
| 293 |
@AWSRetry.jittered_backoff(retries=5, delay=5) |
| 294 |
def list_clusters_with_backoff(client, cluster_name): |
| 295 |
paginator = client.get_paginator("list_clusters") |
| 296 |
return paginator.paginate(ClusterNameFilter=cluster_name).build_full_result() |
| 297 |
|
| 298 |
|
| 299 |
@AWSRetry.jittered_backoff(retries=5, delay=5) |
| 300 |
def list_nodes_with_backoff(client, cluster_arn): |
| 301 |
paginator = client.get_paginator("list_nodes") |
| 302 |
return paginator.paginate(ClusterArn=cluster_arn).build_full_result() |
| 303 |
|
| 304 |
|
| 305 |
def find_cluster_by_name(client, module, cluster_name): |
| 306 |
try: |
| 307 |
cluster_list = list_clusters_with_backoff(client, cluster_name).get("ClusterInfoList", []) |
| 308 |
except ( |
| 309 |
botocore.exceptions.BotoCoreError, |
| 310 |
botocore.exceptions.ClientError, |
| 311 |
) as e: |
| 312 |
module.fail_json_aws(e, "Failed to find kafka cluster by name") |
| 313 |
if cluster_list: |
| 314 |
if len(cluster_list) != 1: |
| 315 |
module.fail_json(msg=f"Found more than one cluster with name '{cluster_name}'") |
| 316 |
return cluster_list[0] |
| 317 |
return {} |
| 318 |
|
| 319 |
|
| 320 |
def get_cluster_state(client, module, arn): |
| 321 |
try: |
| 322 |
response = client.describe_cluster(ClusterArn=arn, aws_retry=True) |
| 323 |
except client.exceptions.NotFoundException: |
| 324 |
return "DELETED" |
| 325 |
except ( |
| 326 |
botocore.exceptions.BotoCoreError, |
| 327 |
botocore.exceptions.ClientError, |
| 328 |
) as e: |
| 329 |
module.fail_json_aws(e, "Failed to get kafka cluster state") |
| 330 |
return response["ClusterInfo"]["State"] |
| 331 |
|
| 332 |
|
| 333 |
def get_cluster_version(client, module, arn): |
| 334 |
try: |
| 335 |
response = client.describe_cluster(ClusterArn=arn, aws_retry=True) |
| 336 |
except ( |
| 337 |
botocore.exceptions.BotoCoreError, |
| 338 |
botocore.exceptions.ClientError, |
| 339 |
) as e: |
| 340 |
module.fail_json_aws(e, "Failed to get kafka cluster version") |
| 341 |
return response["ClusterInfo"]["CurrentVersion"] |
| 342 |
|
| 343 |
|
| 344 |
def wait_for_cluster_state(client, module, arn, state="ACTIVE"): |
| 345 |
# As of 2021-06 boto3 doesn't offer any built in waiters |
| 346 |
start = time.time() |
| 347 |
timeout = int(module.params.get("wait_timeout")) |
| 348 |
check_interval = 60 |
| 349 |
while True: |
| 350 |
current_state = get_cluster_state(client, module, arn) |
| 351 |
if current_state == state: |
| 352 |
return |
| 353 |
if time.time() - start > timeout: |
| 354 |
module.fail_json(msg=f"Timeout waiting for cluster {current_state} (desired state is '{state}')") |
| 355 |
time.sleep(check_interval) |
| 356 |
|
| 357 |
|
| 358 |
def prepare_create_options(module): |
| 359 |
""" |
| 360 |
Return data structure for cluster create operation |
| 361 |
""" |
| 362 |
|
| 363 |
c_params = { |
| 364 |
"ClusterName": module.params["name"], |
| 365 |
"KafkaVersion": module.params["version"], |
| 366 |
"ConfigurationInfo": { |
| 367 |
"Arn": module.params["configuration_arn"], |
| 368 |
"Revision": module.params["configuration_revision"], |
| 369 |
}, |
| 370 |
"NumberOfBrokerNodes": module.params["nodes"], |
| 371 |
"BrokerNodeGroupInfo": { |
| 372 |
"ClientSubnets": module.params["subnets"], |
| 373 |
"InstanceType": module.params["instance_type"], |
| 374 |
}, |
| 375 |
} |
| 376 |
|
| 377 |
if module.params["security_groups"] and len(module.params["security_groups"]) != 0: |
| 378 |
c_params["BrokerNodeGroupInfo"]["SecurityGroups"] = module.params.get("security_groups") |
| 379 |
|
| 380 |
if module.params["ebs_volume_size"]: |
| 381 |
c_params["BrokerNodeGroupInfo"]["StorageInfo"] = { |
| 382 |
"EbsStorageInfo": {"VolumeSize": module.params.get("ebs_volume_size")} |
| 383 |
} |
| 384 |
|
| 385 |
if module.params["encryption"]: |
| 386 |
c_params["EncryptionInfo"] = {} |
| 387 |
if module.params["encryption"].get("kms_key_id"): |
| 388 |
c_params["EncryptionInfo"]["EncryptionAtRest"] = { |
| 389 |
"DataVolumeKMSKeyId": module.params["encryption"]["kms_key_id"] |
| 390 |
} |
| 391 |
c_params["EncryptionInfo"]["EncryptionInTransit"] = { |
| 392 |
"ClientBroker": module.params["encryption"]["in_transit"].get("client_broker", "TLS"), |
| 393 |
"InCluster": module.params["encryption"]["in_transit"].get("in_cluster", True), |
| 394 |
} |
| 395 |
|
| 396 |
if module.params["authentication"]: |
| 397 |
c_params["ClientAuthentication"] = {} |
| 398 |
if module.params["authentication"].get("sasl_scram") or module.params["authentication"].get("sasl_iam"): |
| 399 |
sasl = {} |
| 400 |
if module.params["authentication"].get("sasl_scram"): |
| 401 |
sasl["Scram"] = {"Enabled": True} |
| 402 |
if module.params["authentication"].get("sasl_iam"): |
| 403 |
sasl["Iam"] = {"Enabled": True} |
| 404 |
c_params["ClientAuthentication"]["Sasl"] = sasl |
| 405 |
if module.params["authentication"].get("tls_ca_arn"): |
| 406 |
c_params["ClientAuthentication"]["Tls"] = { |
| 407 |
"CertificateAuthorityArnList": module.params["authentication"]["tls_ca_arn"], |
| 408 |
"Enabled": True, |
| 409 |
} |
| 410 |
if module.params["authentication"].get("unauthenticated"): |
| 411 |
c_params["ClientAuthentication"] = { |
| 412 |
"Unauthenticated": {"Enabled": True}, |
| 413 |
} |
| 414 |
|
| 415 |
c_params.update(prepare_enhanced_monitoring_options(module)) |
| 416 |
c_params.update(prepare_open_monitoring_options(module)) |
| 417 |
c_params.update(prepare_logging_options(module)) |
| 418 |
|
| 419 |
return c_params |
| 420 |
|
| 421 |
|
| 422 |
def prepare_enhanced_monitoring_options(module): |
| 423 |
m_params = {} |
| 424 |
m_params["EnhancedMonitoring"] = module.params["enhanced_monitoring"] or "DEFAULT" |
| 425 |
return m_params |
| 426 |
|
| 427 |
|
| 428 |
def prepare_open_monitoring_options(module): |
| 429 |
m_params = {} |
| 430 |
open_monitoring = module.params["open_monitoring"] or {} |
| 431 |
m_params["OpenMonitoring"] = { |
| 432 |
"Prometheus": { |
| 433 |
"JmxExporter": {"EnabledInBroker": open_monitoring.get("jmx_exporter", False)}, |
| 434 |
"NodeExporter": {"EnabledInBroker": open_monitoring.get("node_exporter", False)}, |
| 435 |
} |
| 436 |
} |
| 437 |
return m_params |
| 438 |
|
| 439 |
|
| 440 |
def prepare_logging_options(module): |
| 441 |
l_params = {} |
| 442 |
logging = module.params["logging"] or {} |
| 443 |
if logging.get("cloudwatch"): |
| 444 |
l_params["CloudWatchLogs"] = { |
| 445 |
"Enabled": module.params["logging"]["cloudwatch"].get("enabled"), |
| 446 |
"LogGroup": module.params["logging"]["cloudwatch"].get("log_group"), |
| 447 |
} |
| 448 |
else: |
| 449 |
l_params["CloudWatchLogs"] = {"Enabled": False} |
| 450 |
if logging.get("firehose"): |
| 451 |
l_params["Firehose"] = { |
| 452 |
"Enabled": module.params["logging"]["firehose"].get("enabled"), |
| 453 |
"DeliveryStream": module.params["logging"]["firehose"].get("delivery_stream"), |
| 454 |
} |
| 455 |
else: |
| 456 |
l_params["Firehose"] = {"Enabled": False} |
| 457 |
if logging.get("s3"): |
| 458 |
l_params["S3"] = { |
| 459 |
"Enabled": module.params["logging"]["s3"].get("enabled"), |
| 460 |
"Bucket": module.params["logging"]["s3"].get("bucket"), |
| 461 |
"Prefix": module.params["logging"]["s3"].get("prefix"), |
| 462 |
} |
| 463 |
else: |
| 464 |
l_params["S3"] = {"Enabled": False} |
| 465 |
return {"LoggingInfo": {"BrokerLogs": l_params}} |
| 466 |
|
| 467 |
|
| 468 |
def create_or_update_cluster(client, module): |
| 469 |
""" |
| 470 |
Create new or update existing cluster |
| 471 |
""" |
| 472 |
|
| 473 |
changed = False |
| 474 |
response = {} |
| 475 |
|
| 476 |
cluster = find_cluster_by_name(client, module, module.params["name"]) |
| 477 |
|
| 478 |
if not cluster: |
| 479 |
changed = True |
| 480 |
|
| 481 |
if module.check_mode: |
| 482 |
return True, {} |
| 483 |
|
| 484 |
create_params = prepare_create_options(module) |
| 485 |
|
| 486 |
try: |
| 487 |
response = client.create_cluster(aws_retry=True, **create_params) |
| 488 |
except ( |
| 489 |
botocore.exceptions.BotoCoreError, |
| 490 |
botocore.exceptions.ClientError, |
| 491 |
) as e: |
| 492 |
module.fail_json_aws(e, "Failed to create kafka cluster") |
| 493 |
|
| 494 |
if module.params.get("wait"): |
| 495 |
wait_for_cluster_state(client, module, arn=response["ClusterArn"], state="ACTIVE") |
| 496 |
|
| 497 |
else: |
| 498 |
response["ClusterArn"] = cluster["ClusterArn"] |
| 499 |
response["changes"] = {} |
| 500 |
|
| 501 |
# prepare available update methods definitions with current/target values and options |
| 502 |
msk_cluster_changes = { |
| 503 |
"broker_count": { |
| 504 |
"current_value": cluster["NumberOfBrokerNodes"], |
| 505 |
"target_value": module.params.get("nodes"), |
| 506 |
"update_params": {"TargetNumberOfBrokerNodes": module.params.get("nodes")}, |
| 507 |
}, |
| 508 |
"broker_storage": { |
| 509 |
"current_value": cluster["BrokerNodeGroupInfo"]["StorageInfo"]["EbsStorageInfo"]["VolumeSize"], |
| 510 |
"target_value": module.params.get("ebs_volume_size"), |
| 511 |
"update_params": { |
| 512 |
"TargetBrokerEBSVolumeInfo": [ |
| 513 |
{"KafkaBrokerNodeId": "All", "VolumeSizeGB": module.params.get("ebs_volume_size")} |
| 514 |
] |
| 515 |
}, |
| 516 |
}, |
| 517 |
"broker_type": { |
| 518 |
"current_value": cluster["BrokerNodeGroupInfo"]["InstanceType"], |
| 519 |
"target_value": module.params.get("instance_type"), |
| 520 |
"update_params": {"TargetInstanceType": module.params.get("instance_type")}, |
| 521 |
}, |
| 522 |
"cluster_configuration": { |
| 523 |
"current_value": { |
| 524 |
"arn": cluster["CurrentBrokerSoftwareInfo"]["ConfigurationArn"], |
| 525 |
"revision": cluster["CurrentBrokerSoftwareInfo"]["ConfigurationRevision"], |
| 526 |
}, |
| 527 |
"target_value": { |
| 528 |
"arn": module.params.get("configuration_arn"), |
| 529 |
"revision": module.params.get("configuration_revision"), |
| 530 |
}, |
| 531 |
"update_params": { |
| 532 |
"ConfigurationInfo": { |
| 533 |
"Arn": module.params.get("configuration_arn"), |
| 534 |
"Revision": module.params.get("configuration_revision"), |
| 535 |
} |
| 536 |
}, |
| 537 |
}, |
| 538 |
"cluster_kafka_version": { |
| 539 |
"current_value": cluster["CurrentBrokerSoftwareInfo"]["KafkaVersion"], |
| 540 |
"target_value": module.params.get("version"), |
| 541 |
"update_params": {"TargetKafkaVersion": module.params.get("version")}, |
| 542 |
}, |
| 543 |
"enhanced_monitoring": { |
| 544 |
"current_value": cluster["EnhancedMonitoring"], |
| 545 |
"target_value": module.params.get("enhanced_monitoring"), |
| 546 |
"update_method": "update_monitoring", |
| 547 |
"update_params": prepare_enhanced_monitoring_options(module), |
| 548 |
}, |
| 549 |
"open_monitoring": { |
| 550 |
"current_value": {"OpenMonitoring": cluster["OpenMonitoring"]}, |
| 551 |
"target_value": prepare_open_monitoring_options(module), |
| 552 |
"update_method": "update_monitoring", |
| 553 |
"update_params": prepare_open_monitoring_options(module), |
| 554 |
}, |
| 555 |
"logging": { |
| 556 |
"current_value": {"LoggingInfo": cluster["LoggingInfo"]}, |
| 557 |
"target_value": prepare_logging_options(module), |
| 558 |
"update_method": "update_monitoring", |
| 559 |
"update_params": prepare_logging_options(module), |
| 560 |
}, |
| 561 |
} |
| 562 |
|
| 563 |
for method, options in msk_cluster_changes.items(): |
| 564 |
if "botocore_version" in options: |
| 565 |
if not module.botocore_at_least(options["botocore_version"]): |
| 566 |
continue |
| 567 |
|
| 568 |
try: |
| 569 |
update_method = getattr(client, options.get("update_method", "update_" + method)) |
| 570 |
except AttributeError as e: |
| 571 |
module.fail_json_aws(e, f"There is no update method 'update_{method}'") |
| 572 |
|
| 573 |
if options["current_value"] != options["target_value"]: |
| 574 |
changed = True |
| 575 |
if module.check_mode: |
| 576 |
return True, {} |
| 577 |
|
| 578 |
# need to get cluster version and check for the state because |
| 579 |
# there can be several updates requested but only one in time can be performed |
| 580 |
version = get_cluster_version(client, module, cluster["ClusterArn"]) |
| 581 |
state = get_cluster_state(client, module, cluster["ClusterArn"]) |
| 582 |
if state != "ACTIVE": |
| 583 |
if module.params["wait"]: |
| 584 |
wait_for_cluster_state(client, module, arn=cluster["ClusterArn"], state="ACTIVE") |
| 585 |
else: |
| 586 |
module.fail_json( |
| 587 |
msg=f"Cluster can be updated only in active state, current state is '{state}'. check cluster state or use wait option" |
| 588 |
) |
| 589 |
try: |
| 590 |
response["changes"][method] = update_method( |
| 591 |
ClusterArn=cluster["ClusterArn"], CurrentVersion=version, **options["update_params"] |
| 592 |
) |
| 593 |
except ( |
| 594 |
botocore.exceptions.BotoCoreError, |
| 595 |
botocore.exceptions.ClientError, |
| 596 |
) as e: |
| 597 |
module.fail_json_aws(e, f"Failed to update cluster via 'update_{method}'") |
| 598 |
|
| 599 |
if module.params["wait"]: |
| 600 |
wait_for_cluster_state(client, module, arn=cluster["ClusterArn"], state="ACTIVE") |
| 601 |
|
| 602 |
changed |= update_cluster_tags(client, module, response["ClusterArn"]) |
| 603 |
|
| 604 |
return changed, response |
| 605 |
|
| 606 |
|
| 607 |
def update_cluster_tags(client, module, arn): |
| 608 |
new_tags = module.params.get("tags") |
| 609 |
if new_tags is None: |
| 610 |
return False |
| 611 |
purge_tags = module.params.get("purge_tags") |
| 612 |
|
| 613 |
try: |
| 614 |
existing_tags = client.list_tags_for_resource(ResourceArn=arn, aws_retry=True)["Tags"] |
| 615 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 616 |
module.fail_json_aws(e, msg=f"Unable to retrieve tags for cluster '{arn}'") |
| 617 |
|
| 618 |
tags_to_add, tags_to_remove = compare_aws_tags(existing_tags, new_tags, purge_tags=purge_tags) |
| 619 |
|
| 620 |
if not module.check_mode: |
| 621 |
try: |
| 622 |
if tags_to_remove: |
| 623 |
client.untag_resource(ResourceArn=arn, TagKeys=tags_to_remove, aws_retry=True) |
| 624 |
if tags_to_add: |
| 625 |
client.tag_resource(ResourceArn=arn, Tags=tags_to_add, aws_retry=True) |
| 626 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 627 |
module.fail_json_aws(e, msg=f"Unable to set tags for cluster '{arn}'") |
| 628 |
|
| 629 |
changed = bool(tags_to_add) or bool(tags_to_remove) |
| 630 |
return changed |
| 631 |
|
| 632 |
|
| 633 |
def delete_cluster(client, module): |
| 634 |
cluster = find_cluster_by_name(client, module, module.params["name"]) |
| 635 |
|
| 636 |
if module.check_mode: |
| 637 |
if cluster: |
| 638 |
return True, cluster |
| 639 |
else: |
| 640 |
return False, {} |
| 641 |
|
| 642 |
if not cluster: |
| 643 |
return False, {} |
| 644 |
|
| 645 |
try: |
| 646 |
response = client.delete_cluster( |
| 647 |
ClusterArn=cluster["ClusterArn"], |
| 648 |
CurrentVersion=cluster["CurrentVersion"], |
| 649 |
) |
| 650 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 651 |
module.fail_json_aws(e, "Failed to delete kafka cluster") |
| 652 |
|
| 653 |
if module.params["wait"]: |
| 654 |
wait_for_cluster_state(client, module, arn=cluster["ClusterArn"], state="DELETED") |
| 655 |
|
| 656 |
response["bootstrap_broker_string"] = {} |
| 657 |
|
| 658 |
return True, response |
| 659 |
|
| 660 |
|
| 661 |
def main(): |
| 662 |
module_args = dict( |
| 663 |
name=dict(type="str", required=True), |
| 664 |
state=dict(type="str", choices=["present", "absent"], default="present"), |
| 665 |
version=dict(type="str"), |
| 666 |
configuration_arn=dict(type="str"), |
| 667 |
configuration_revision=dict(type="int"), |
| 668 |
nodes=dict(type="int", default=3), |
| 669 |
instance_type=dict( |
| 670 |
choices=[ |
| 671 |
"kafka.t3.small", |
| 672 |
"kafka.m5.large", |
| 673 |
"kafka.m5.xlarge", |
| 674 |
"kafka.m5.2xlarge", |
| 675 |
"kafka.m5.4xlarge", |
| 676 |
"kafka.m5.8xlarge", |
| 677 |
"kafka.m5.12xlarge", |
| 678 |
"kafka.m5.16xlarge", |
| 679 |
"kafka.m5.24xlarge", |
| 680 |
"kafka.m7g.large", |
| 681 |
"kafka.m7g.xlarge", |
| 682 |
"kafka.m7g.2xlarge", |
| 683 |
"kafka.m7g.4xlarge", |
| 684 |
"kafka.m7g.8xlarge", |
| 685 |
"kafka.m7g.12xlarge", |
| 686 |
"kafka.m7g.16xlarge", |
| 687 |
], |
| 688 |
default="kafka.t3.small", |
| 689 |
), |
| 690 |
ebs_volume_size=dict(type="int", default=100), |
| 691 |
subnets=dict(type="list", elements="str"), |
| 692 |
security_groups=dict(type="list", elements="str", required=False), |
| 693 |
encryption=dict( |
| 694 |
type="dict", |
| 695 |
options=dict( |
| 696 |
kms_key_id=dict(type="str", required=False), |
| 697 |
in_transit=dict( |
| 698 |
type="dict", |
| 699 |
options=dict( |
| 700 |
in_cluster=dict(type="bool", default=True), |
| 701 |
client_broker=dict(choices=["TLS", "TLS_PLAINTEXT", "PLAINTEXT"], default="TLS"), |
| 702 |
), |
| 703 |
), |
| 704 |
), |
| 705 |
), |
| 706 |
authentication=dict( |
| 707 |
type="dict", |
| 708 |
options=dict( |
| 709 |
tls_ca_arn=dict(type="list", elements="str", required=False), |
| 710 |
sasl_scram=dict(type="bool", required=False), |
| 711 |
sasl_iam=dict(type="bool", required=False), |
| 712 |
unauthenticated=dict(type="bool", default=True, required=False), |
| 713 |
), |
| 714 |
), |
| 715 |
enhanced_monitoring=dict( |
| 716 |
choices=[ |
| 717 |
"DEFAULT", |
| 718 |
"PER_BROKER", |
| 719 |
"PER_TOPIC_PER_BROKER", |
| 720 |
"PER_TOPIC_PER_PARTITION", |
| 721 |
], |
| 722 |
default="DEFAULT", |
| 723 |
required=False, |
| 724 |
), |
| 725 |
open_monitoring=dict( |
| 726 |
type="dict", |
| 727 |
options=dict( |
| 728 |
jmx_exporter=dict(type="bool", default=False), |
| 729 |
node_exporter=dict(type="bool", default=False), |
| 730 |
), |
| 731 |
), |
| 732 |
logging=dict( |
| 733 |
type="dict", |
| 734 |
options=dict( |
| 735 |
cloudwatch=dict( |
| 736 |
type="dict", |
| 737 |
options=dict( |
| 738 |
enabled=dict(type="bool", default=False), |
| 739 |
log_group=dict(type="str", required=False), |
| 740 |
), |
| 741 |
), |
| 742 |
firehose=dict( |
| 743 |
type="dict", |
| 744 |
options=dict( |
| 745 |
enabled=dict(type="bool", default=False), |
| 746 |
delivery_stream=dict(type="str", required=False), |
| 747 |
), |
| 748 |
), |
| 749 |
s3=dict( |
| 750 |
type="dict", |
| 751 |
options=dict( |
| 752 |
enabled=dict(type="bool", default=False), |
| 753 |
bucket=dict(type="str", required=False), |
| 754 |
prefix=dict(type="str", required=False), |
| 755 |
), |
| 756 |
), |
| 757 |
), |
| 758 |
), |
| 759 |
wait=dict(type="bool", default=False), |
| 760 |
wait_timeout=dict(type="int", default=3600), |
| 761 |
tags=dict(type="dict", aliases=["resource_tags"]), |
| 762 |
purge_tags=dict(type="bool", default=True), |
| 763 |
) |
| 764 |
|
| 765 |
module = AnsibleAWSModule( |
| 766 |
argument_spec=module_args, |
| 767 |
required_if=[["state", "present", ["version", "configuration_arn", "configuration_revision", "subnets"]]], |
| 768 |
supports_check_mode=True, |
| 769 |
) |
| 770 |
|
| 771 |
client = module.client("kafka", retry_decorator=AWSRetry.jittered_backoff()) |
| 772 |
|
| 773 |
if module.params["state"] == "present": |
| 774 |
if len(module.params["subnets"]) < 2: |
| 775 |
module.fail_json(msg="At least two client subnets should be provided") |
| 776 |
if int(module.params["nodes"]) % int(len(module.params["subnets"])) != 0: |
| 777 |
module.fail_json( |
| 778 |
msg="The number of broker nodes must be a multiple of availability zones in the subnets parameter" |
| 779 |
) |
| 780 |
if len(module.params["name"]) > 64: |
| 781 |
module.fail_json(module.fail_json(msg=f'Cluster name "{module.params["name"]}" exceeds 64 character limit')) |
| 782 |
changed, response = create_or_update_cluster(client, module) |
| 783 |
elif module.params["state"] == "absent": |
| 784 |
changed, response = delete_cluster(client, module) |
| 785 |
|
| 786 |
cluster_info = {} |
| 787 |
bootstrap_broker_string = {} |
| 788 |
if response.get("ClusterArn") and module.params["state"] == "present": |
| 789 |
try: |
| 790 |
cluster_info = client.describe_cluster(ClusterArn=response["ClusterArn"], aws_retry=True)["ClusterInfo"] |
| 791 |
if cluster_info.get("State") == "ACTIVE": |
| 792 |
brokers = client.get_bootstrap_brokers(ClusterArn=response["ClusterArn"], aws_retry=True) |
| 793 |
if brokers.get("BootstrapBrokerString"): |
| 794 |
bootstrap_broker_string["plain"] = brokers["BootstrapBrokerString"] |
| 795 |
if brokers.get("BootstrapBrokerStringTls"): |
| 796 |
bootstrap_broker_string["tls"] = brokers["BootstrapBrokerStringTls"] |
| 797 |
except ( |
| 798 |
botocore.exceptions.BotoCoreError, |
| 799 |
botocore.exceptions.ClientError, |
| 800 |
) as e: |
| 801 |
module.fail_json_aws( |
| 802 |
e, |
| 803 |
f"Can not obtain information about cluster {response['ClusterArn']}", |
| 804 |
) |
| 805 |
|
| 806 |
module.exit_json( |
| 807 |
changed=changed, |
| 808 |
bootstrap_broker_string=bootstrap_broker_string, |
| 809 |
cluster_info=camel_dict_to_snake_dict(cluster_info), |
| 810 |
response=camel_dict_to_snake_dict(response), |
| 811 |
) |
| 812 |
|
| 813 |
|
| 814 |
if __name__ == "__main__": |
| 815 |
main() |