| 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: opensearch |
| 10 |
short_description: Creates OpenSearch or ElasticSearch domain |
| 11 |
description: |
| 12 |
- Creates or modify a Amazon OpenSearch Service domain. |
| 13 |
version_added: 4.0.0 |
| 14 |
author: |
| 15 |
- "Sebastien Rosset (@sebastien-rosset)" |
| 16 |
options: |
| 17 |
state: |
| 18 |
description: |
| 19 |
- Creates or modifies an existing OpenSearch domain. |
| 20 |
- Deletes an OpenSearch domain. |
| 21 |
required: false |
| 22 |
type: str |
| 23 |
choices: ['present', 'absent'] |
| 24 |
default: present |
| 25 |
domain_name: |
| 26 |
description: |
| 27 |
- The name of the Amazon OpenSearch/ElasticSearch Service domain. |
| 28 |
- Domain names are unique across the domains owned by an account within an AWS region. |
| 29 |
required: true |
| 30 |
type: str |
| 31 |
engine_version: |
| 32 |
description: |
| 33 |
-> |
| 34 |
The engine version to use. For example, 'ElasticSearch_7.10' or 'OpenSearch_1.1'. |
| 35 |
-> |
| 36 |
If the currently running version is not equal to I(engine_version), |
| 37 |
a cluster upgrade is triggered. |
| 38 |
-> |
| 39 |
It may not be possible to upgrade directly from the currently running version |
| 40 |
to I(engine_version). In that case, the upgrade is performed incrementally by |
| 41 |
upgrading to the highest compatible version, then repeat the operation until |
| 42 |
the cluster is running at the target version. |
| 43 |
-> |
| 44 |
The upgrade operation fails if there is no path from current version to I(engine_version). |
| 45 |
-> |
| 46 |
See OpenSearch documentation for upgrade compatibility. |
| 47 |
required: false |
| 48 |
type: str |
| 49 |
allow_intermediate_upgrades: |
| 50 |
description: |
| 51 |
- > |
| 52 |
If true, allow OpenSearch domain to be upgraded through one or more intermediate versions. |
| 53 |
- > |
| 54 |
If false, do not allow OpenSearch domain to be upgraded through intermediate versions. |
| 55 |
The upgrade operation fails if it's not possible to ugrade to I(engine_version) directly. |
| 56 |
required: false |
| 57 |
type: bool |
| 58 |
default: true |
| 59 |
cluster_config: |
| 60 |
description: |
| 61 |
- Parameters for the cluster configuration of an OpenSearch Service domain. |
| 62 |
type: dict |
| 63 |
suboptions: |
| 64 |
instance_type: |
| 65 |
description: |
| 66 |
- Type of the instances to use for the domain. |
| 67 |
required: false |
| 68 |
type: str |
| 69 |
instance_count: |
| 70 |
description: |
| 71 |
- Number of instances for the domain. |
| 72 |
required: false |
| 73 |
type: int |
| 74 |
zone_awareness: |
| 75 |
description: |
| 76 |
- A boolean value to indicate whether zone awareness is enabled. |
| 77 |
required: false |
| 78 |
type: bool |
| 79 |
availability_zone_count: |
| 80 |
description: |
| 81 |
- > |
| 82 |
An integer value to indicate the number of availability zones for a domain when zone awareness is enabled. |
| 83 |
This should be equal to number of subnets if VPC endpoints is enabled. |
| 84 |
required: false |
| 85 |
type: int |
| 86 |
dedicated_master: |
| 87 |
description: |
| 88 |
- A boolean value to indicate whether a dedicated master node is enabled. |
| 89 |
required: false |
| 90 |
type: bool |
| 91 |
dedicated_master_instance_type: |
| 92 |
description: |
| 93 |
- The instance type for a dedicated master node. |
| 94 |
required: false |
| 95 |
type: str |
| 96 |
dedicated_master_instance_count: |
| 97 |
description: |
| 98 |
- Total number of dedicated master nodes, active and on standby, for the domain. |
| 99 |
required: false |
| 100 |
type: int |
| 101 |
warm_enabled: |
| 102 |
description: |
| 103 |
- True to enable UltraWarm storage. |
| 104 |
required: false |
| 105 |
type: bool |
| 106 |
warm_type: |
| 107 |
description: |
| 108 |
- The instance type for the OpenSearch domain's warm nodes. |
| 109 |
required: false |
| 110 |
type: str |
| 111 |
warm_count: |
| 112 |
description: |
| 113 |
- The number of UltraWarm nodes in the domain. |
| 114 |
required: false |
| 115 |
type: int |
| 116 |
cold_storage_options: |
| 117 |
description: |
| 118 |
- Specifies the ColdStorageOptions config for a Domain. |
| 119 |
type: dict |
| 120 |
suboptions: |
| 121 |
enabled: |
| 122 |
description: |
| 123 |
- True to enable cold storage. Supported on Elasticsearch 7.9 or above. |
| 124 |
required: false |
| 125 |
type: bool |
| 126 |
ebs_options: |
| 127 |
description: |
| 128 |
- Parameters to configure EBS-based storage for an OpenSearch Service domain. |
| 129 |
type: dict |
| 130 |
suboptions: |
| 131 |
ebs_enabled: |
| 132 |
description: |
| 133 |
- Specifies whether EBS-based storage is enabled. |
| 134 |
required: false |
| 135 |
type: bool |
| 136 |
volume_type: |
| 137 |
description: |
| 138 |
- Specifies the volume type for EBS-based storage. "standard"|"gp2"|"io1" |
| 139 |
required: false |
| 140 |
type: str |
| 141 |
volume_size: |
| 142 |
description: |
| 143 |
- Integer to specify the size of an EBS volume. |
| 144 |
required: false |
| 145 |
type: int |
| 146 |
iops: |
| 147 |
description: |
| 148 |
- The IOPD for a Provisioned IOPS EBS volume (SSD). |
| 149 |
required: false |
| 150 |
type: int |
| 151 |
vpc_options: |
| 152 |
description: |
| 153 |
- Options to specify the subnets and security groups for a VPC endpoint. |
| 154 |
type: dict |
| 155 |
suboptions: |
| 156 |
subnets: |
| 157 |
description: |
| 158 |
- Specifies the subnet ids for VPC endpoint. |
| 159 |
required: false |
| 160 |
type: list |
| 161 |
elements: str |
| 162 |
security_groups: |
| 163 |
description: |
| 164 |
- Specifies the security group ids for VPC endpoint. |
| 165 |
required: false |
| 166 |
type: list |
| 167 |
elements: str |
| 168 |
snapshot_options: |
| 169 |
description: |
| 170 |
- Option to set time, in UTC format, of the daily automated snapshot. |
| 171 |
type: dict |
| 172 |
suboptions: |
| 173 |
automated_snapshot_start_hour: |
| 174 |
description: |
| 175 |
- > |
| 176 |
Integer value from 0 to 23 specifying when the service takes a daily automated snapshot |
| 177 |
of the specified Elasticsearch domain. |
| 178 |
required: false |
| 179 |
type: int |
| 180 |
access_policies: |
| 181 |
description: |
| 182 |
- IAM access policy as a JSON-formatted string. |
| 183 |
required: false |
| 184 |
type: dict |
| 185 |
encryption_at_rest_options: |
| 186 |
description: |
| 187 |
- Parameters to enable encryption at rest. |
| 188 |
type: dict |
| 189 |
suboptions: |
| 190 |
enabled: |
| 191 |
description: |
| 192 |
- Should data be encrypted while at rest. |
| 193 |
required: false |
| 194 |
type: bool |
| 195 |
kms_key_id: |
| 196 |
description: |
| 197 |
- If encryption at rest enabled, this identifies the encryption key to use. |
| 198 |
- The value should be a KMS key ARN. It can also be the KMS key id. |
| 199 |
required: false |
| 200 |
type: str |
| 201 |
node_to_node_encryption_options: |
| 202 |
description: |
| 203 |
- Node-to-node encryption options. |
| 204 |
type: dict |
| 205 |
suboptions: |
| 206 |
enabled: |
| 207 |
description: |
| 208 |
- True to enable node-to-node encryption. |
| 209 |
required: false |
| 210 |
type: bool |
| 211 |
cognito_options: |
| 212 |
description: |
| 213 |
- Parameters to configure OpenSearch Service to use Amazon Cognito authentication for OpenSearch Dashboards. |
| 214 |
type: dict |
| 215 |
suboptions: |
| 216 |
enabled: |
| 217 |
description: |
| 218 |
- The option to enable Cognito for OpenSearch Dashboards authentication. |
| 219 |
required: false |
| 220 |
type: bool |
| 221 |
user_pool_id: |
| 222 |
description: |
| 223 |
- The Cognito user pool ID for OpenSearch Dashboards authentication. |
| 224 |
required: false |
| 225 |
type: str |
| 226 |
identity_pool_id: |
| 227 |
description: |
| 228 |
- The Cognito identity pool ID for OpenSearch Dashboards authentication. |
| 229 |
required: false |
| 230 |
type: str |
| 231 |
role_arn: |
| 232 |
description: |
| 233 |
- The role ARN that provides OpenSearch permissions for accessing Cognito resources. |
| 234 |
required: false |
| 235 |
type: str |
| 236 |
domain_endpoint_options: |
| 237 |
description: |
| 238 |
- Options to specify configuration that will be applied to the domain endpoint. |
| 239 |
type: dict |
| 240 |
suboptions: |
| 241 |
enforce_https: |
| 242 |
description: |
| 243 |
- Whether only HTTPS endpoint should be enabled for the domain. |
| 244 |
type: bool |
| 245 |
tls_security_policy: |
| 246 |
description: |
| 247 |
- Specify the TLS security policy to apply to the HTTPS endpoint of the domain. |
| 248 |
type: str |
| 249 |
custom_endpoint_enabled: |
| 250 |
description: |
| 251 |
- Whether to enable a custom endpoint for the domain. |
| 252 |
type: bool |
| 253 |
custom_endpoint: |
| 254 |
description: |
| 255 |
- The fully qualified domain for your custom endpoint. |
| 256 |
type: str |
| 257 |
custom_endpoint_certificate_arn: |
| 258 |
description: |
| 259 |
- The ACM certificate ARN for your custom endpoint. |
| 260 |
type: str |
| 261 |
advanced_security_options: |
| 262 |
description: |
| 263 |
- Specifies advanced security options. |
| 264 |
type: dict |
| 265 |
suboptions: |
| 266 |
enabled: |
| 267 |
description: |
| 268 |
- True if advanced security is enabled. |
| 269 |
- You must enable node-to-node encryption to use advanced security options. |
| 270 |
type: bool |
| 271 |
internal_user_database_enabled: |
| 272 |
description: |
| 273 |
- True if the internal user database is enabled. |
| 274 |
type: bool |
| 275 |
master_user_options: |
| 276 |
description: |
| 277 |
- Credentials for the master user, username and password, ARN, or both. |
| 278 |
type: dict |
| 279 |
suboptions: |
| 280 |
master_user_arn: |
| 281 |
description: |
| 282 |
- ARN for the master user (if IAM is enabled). |
| 283 |
type: str |
| 284 |
master_user_name: |
| 285 |
description: |
| 286 |
- The username of the master user, which is stored in the Amazon OpenSearch Service domain internal database. |
| 287 |
type: str |
| 288 |
master_user_password: |
| 289 |
description: |
| 290 |
- The password of the master user, which is stored in the Amazon OpenSearch Service domain internal database. |
| 291 |
type: str |
| 292 |
saml_options: |
| 293 |
description: |
| 294 |
- The SAML application configuration for the domain. |
| 295 |
type: dict |
| 296 |
suboptions: |
| 297 |
enabled: |
| 298 |
description: |
| 299 |
- True if SAML is enabled. |
| 300 |
- To use SAML authentication, you must enable fine-grained access control. |
| 301 |
- You can only enable SAML authentication for OpenSearch Dashboards on existing domains, |
| 302 |
not during the creation of new ones. |
| 303 |
- Domains only support one Dashboards authentication method at a time. |
| 304 |
If you have Amazon Cognito authentication for OpenSearch Dashboards enabled, |
| 305 |
you must disable it before you can enable SAML. |
| 306 |
type: bool |
| 307 |
idp: |
| 308 |
description: |
| 309 |
- The SAML Identity Provider's information. |
| 310 |
type: dict |
| 311 |
suboptions: |
| 312 |
metadata_content: |
| 313 |
description: |
| 314 |
- The metadata of the SAML application in XML format. |
| 315 |
type: str |
| 316 |
entity_id: |
| 317 |
description: |
| 318 |
- The unique entity ID of the application in SAML identity provider. |
| 319 |
type: str |
| 320 |
master_user_name: |
| 321 |
description: |
| 322 |
- The SAML master username, which is stored in the Amazon OpenSearch Service domain internal database. |
| 323 |
type: str |
| 324 |
master_backend_role: |
| 325 |
description: |
| 326 |
- The backend role that the SAML master user is mapped to. |
| 327 |
type: str |
| 328 |
subject_key: |
| 329 |
description: |
| 330 |
- Element of the SAML assertion to use for username. Default is NameID. |
| 331 |
type: str |
| 332 |
roles_key: |
| 333 |
description: |
| 334 |
- Element of the SAML assertion to use for backend roles. Default is roles. |
| 335 |
type: str |
| 336 |
session_timeout_minutes: |
| 337 |
description: |
| 338 |
- The duration, in minutes, after which a user session becomes inactive. Acceptable values are between 1 and 1440, and the default value is 60. |
| 339 |
type: int |
| 340 |
auto_tune_options: |
| 341 |
description: |
| 342 |
- Specifies Auto-Tune options. |
| 343 |
type: dict |
| 344 |
suboptions: |
| 345 |
desired_state: |
| 346 |
description: |
| 347 |
- The Auto-Tune desired state. Valid values are ENABLED and DISABLED. |
| 348 |
type: str |
| 349 |
choices: ['ENABLED', 'DISABLED'] |
| 350 |
maintenance_schedules: |
| 351 |
description: |
| 352 |
- A list of maintenance schedules. |
| 353 |
type: list |
| 354 |
elements: dict |
| 355 |
suboptions: |
| 356 |
start_at: |
| 357 |
description: |
| 358 |
- The timestamp at which the Auto-Tune maintenance schedule starts. |
| 359 |
type: str |
| 360 |
duration: |
| 361 |
description: |
| 362 |
- Specifies maintenance schedule duration, duration value and duration unit. |
| 363 |
type: dict |
| 364 |
suboptions: |
| 365 |
value: |
| 366 |
description: |
| 367 |
- Integer to specify the value of a maintenance schedule duration. |
| 368 |
type: int |
| 369 |
unit: |
| 370 |
description: |
| 371 |
- The unit of a maintenance schedule duration. Valid value is HOURS. |
| 372 |
choices: ['HOURS'] |
| 373 |
type: str |
| 374 |
cron_expression_for_recurrence: |
| 375 |
description: |
| 376 |
- A cron expression for a recurring maintenance schedule. |
| 377 |
type: str |
| 378 |
wait: |
| 379 |
description: |
| 380 |
- Whether or not to wait for completion of OpenSearch creation, modification or deletion. |
| 381 |
type: bool |
| 382 |
default: false |
| 383 |
wait_timeout: |
| 384 |
description: |
| 385 |
- how long before wait gives up, in seconds. |
| 386 |
default: 300 |
| 387 |
type: int |
| 388 |
extends_documentation_fragment: |
| 389 |
- amazon.aws.common.modules |
| 390 |
- amazon.aws.region.modules |
| 391 |
- amazon.aws.tags |
| 392 |
- amazon.aws.boto3 |
| 393 |
""" |
| 394 |
|
| 395 |
RETURN = r""" # """ |
| 396 |
|
| 397 |
EXAMPLES = r""" |
| 398 |
|
| 399 |
- name: Create OpenSearch domain for dev environment, no zone awareness, no dedicated masters |
| 400 |
community.aws.opensearch: |
| 401 |
domain_name: "dev-cluster" |
| 402 |
engine_version: Elasticsearch_1.1 |
| 403 |
cluster_config: |
| 404 |
instance_type: "t2.small.search" |
| 405 |
instance_count: 2 |
| 406 |
zone_awareness: false |
| 407 |
dedicated_master: false |
| 408 |
ebs_options: |
| 409 |
ebs_enabled: true |
| 410 |
volume_type: "gp2" |
| 411 |
volume_size: 10 |
| 412 |
access_policies: "{{ lookup('file', 'policy.json') | from_json }}" |
| 413 |
|
| 414 |
- name: Create OpenSearch domain with dedicated masters |
| 415 |
community.aws.opensearch: |
| 416 |
domain_name: "my-domain" |
| 417 |
engine_version: OpenSearch_1.1 |
| 418 |
cluster_config: |
| 419 |
instance_type: "t2.small.search" |
| 420 |
instance_count: 12 |
| 421 |
dedicated_master: true |
| 422 |
zone_awareness: true |
| 423 |
availability_zone_count: 2 |
| 424 |
dedicated_master_instance_type: "t2.small.search" |
| 425 |
dedicated_master_instance_count: 3 |
| 426 |
warm_enabled: true |
| 427 |
warm_type: "ultrawarm1.medium.search" |
| 428 |
warm_count: 1 |
| 429 |
cold_storage_options: |
| 430 |
enabled: false |
| 431 |
ebs_options: |
| 432 |
ebs_enabled: true |
| 433 |
volume_type: "io1" |
| 434 |
volume_size: 10 |
| 435 |
iops: 1000 |
| 436 |
vpc_options: |
| 437 |
subnets: |
| 438 |
- "subnet-e537d64a" |
| 439 |
- "subnet-e537d64b" |
| 440 |
security_groups: |
| 441 |
- "sg-dd2f13cb" |
| 442 |
- "sg-dd2f13cc" |
| 443 |
snapshot_options: |
| 444 |
automated_snapshot_start_hour: 13 |
| 445 |
access_policies: "{{ lookup('file', 'policy.json') | from_json }}" |
| 446 |
encryption_at_rest_options: |
| 447 |
enabled: false |
| 448 |
node_to_node_encryption_options: |
| 449 |
enabled: false |
| 450 |
auto_tune_options: |
| 451 |
enabled: true |
| 452 |
maintenance_schedules: |
| 453 |
- start_at: "2025-01-12" |
| 454 |
duration: |
| 455 |
value: 1 |
| 456 |
unit: "HOURS" |
| 457 |
cron_expression_for_recurrence: "cron(0 12 * * ? *)" |
| 458 |
- start_at: "2032-01-12" |
| 459 |
duration: |
| 460 |
value: 2 |
| 461 |
unit: "HOURS" |
| 462 |
cron_expression_for_recurrence: "cron(0 12 * * ? *)" |
| 463 |
tags: |
| 464 |
Environment: Development |
| 465 |
Application: Search |
| 466 |
wait: true |
| 467 |
|
| 468 |
- name: Increase size of EBS volumes for existing cluster |
| 469 |
community.aws.opensearch: |
| 470 |
domain_name: "my-domain" |
| 471 |
ebs_options: |
| 472 |
volume_size: 5 |
| 473 |
wait: true |
| 474 |
|
| 475 |
- name: Increase instance count for existing cluster |
| 476 |
community.aws.opensearch: |
| 477 |
domain_name: "my-domain" |
| 478 |
cluster_config: |
| 479 |
instance_count: 40 |
| 480 |
wait: true |
| 481 |
""" |
| 482 |
|
| 483 |
import datetime |
| 484 |
import json |
| 485 |
from copy import deepcopy |
| 486 |
|
| 487 |
try: |
| 488 |
import botocore |
| 489 |
except ImportError: |
| 490 |
pass # handled by AnsibleAWSModule |
| 491 |
|
| 492 |
|
| 493 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code |
| 494 |
from ansible_collections.amazon.aws.plugins.module_utils.policy import compare_policies |
| 495 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 496 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 497 |
|
| 498 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 499 |
from ansible_collections.community.aws.plugins.module_utils.opensearch import compare_domain_versions |
| 500 |
from ansible_collections.community.aws.plugins.module_utils.opensearch import ensure_tags |
| 501 |
from ansible_collections.community.aws.plugins.module_utils.opensearch import get_domain_config |
| 502 |
from ansible_collections.community.aws.plugins.module_utils.opensearch import get_domain_status |
| 503 |
from ansible_collections.community.aws.plugins.module_utils.opensearch import get_target_increment_version |
| 504 |
from ansible_collections.community.aws.plugins.module_utils.opensearch import normalize_opensearch |
| 505 |
from ansible_collections.community.aws.plugins.module_utils.opensearch import parse_version |
| 506 |
from ansible_collections.community.aws.plugins.module_utils.opensearch import wait_for_domain_status |
| 507 |
|
| 508 |
|
| 509 |
def ensure_domain_absent(client, module): |
| 510 |
domain_name = module.params.get("domain_name") |
| 511 |
changed = False |
| 512 |
|
| 513 |
domain = get_domain_status(client, module, domain_name) |
| 514 |
if module.check_mode: |
| 515 |
module.exit_json(changed=True, msg="Would have deleted domain if not in check mode") |
| 516 |
try: |
| 517 |
client.delete_domain(DomainName=domain_name) |
| 518 |
changed = True |
| 519 |
except is_boto3_error_code("ResourceNotFoundException"): |
| 520 |
# The resource does not exist, or it has already been deleted |
| 521 |
return dict(changed=False) |
| 522 |
except ( |
| 523 |
botocore.exceptions.ClientError, |
| 524 |
botocore.exceptions.BotoCoreError, |
| 525 |
) as e: # pylint: disable=duplicate-except |
| 526 |
module.fail_json_aws(e, msg="trying to delete domain") |
| 527 |
|
| 528 |
# If we're not waiting for a delete to complete then we're all done |
| 529 |
# so just return |
| 530 |
if not domain or not module.params.get("wait"): |
| 531 |
return dict(changed=changed) |
| 532 |
try: |
| 533 |
wait_for_domain_status(client, module, domain_name, "domain_deleted") |
| 534 |
return dict(changed=changed) |
| 535 |
except is_boto3_error_code("ResourceNotFoundException"): |
| 536 |
return dict(changed=changed) |
| 537 |
except ( |
| 538 |
botocore.exceptions.ClientError, |
| 539 |
botocore.exceptions.BotoCoreError, |
| 540 |
) as e: # pylint: disable=duplicate-except |
| 541 |
module.fail_json_aws(e, "awaiting domain deletion") |
| 542 |
|
| 543 |
|
| 544 |
def upgrade_domain(client, module, source_version, target_engine_version): |
| 545 |
domain_name = module.params.get("domain_name") |
| 546 |
# Determine if it's possible to upgrade directly from source version |
| 547 |
# to target version, or if it's necessary to upgrade through intermediate major versions. |
| 548 |
next_version = target_engine_version |
| 549 |
# When perform_check_only is true, indicates that an upgrade eligibility check needs |
| 550 |
# to be performed. Does not actually perform the upgrade. |
| 551 |
perform_check_only = False |
| 552 |
if module.check_mode: |
| 553 |
perform_check_only = True |
| 554 |
current_version = source_version |
| 555 |
while current_version != target_engine_version: |
| 556 |
v = get_target_increment_version(client, module, domain_name, target_engine_version) |
| 557 |
if v is None: |
| 558 |
# There is no compatible version, according to the get_compatible_versions() API. |
| 559 |
# The upgrade should fail, but try anyway. |
| 560 |
next_version = target_engine_version |
| 561 |
if next_version != target_engine_version: |
| 562 |
# It's not possible to upgrade directly to the target version. |
| 563 |
# Check the module parameters to determine if this is allowed or not. |
| 564 |
if not module.params.get("allow_intermediate_upgrades"): |
| 565 |
module.fail_json( |
| 566 |
msg=f"Cannot upgrade from {source_version} to version {target_engine_version}. The highest compatible version is {next_version}" |
| 567 |
) |
| 568 |
|
| 569 |
parameters = { |
| 570 |
"DomainName": domain_name, |
| 571 |
"TargetVersion": next_version, |
| 572 |
"PerformCheckOnly": perform_check_only, |
| 573 |
} |
| 574 |
|
| 575 |
if not module.check_mode: |
| 576 |
# If background tasks are in progress, wait until they complete. |
| 577 |
# This can take several hours depending on the cluster size and the type of background tasks |
| 578 |
# (maybe an upgrade is already in progress). |
| 579 |
# It's not possible to upgrade a domain that has background tasks are in progress, |
| 580 |
# the call to client.upgrade_domain would fail. |
| 581 |
wait_for_domain_status(client, module, domain_name, "domain_available") |
| 582 |
|
| 583 |
try: |
| 584 |
client.upgrade_domain(**parameters) |
| 585 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 586 |
# In check mode (=> PerformCheckOnly==True), a ValidationException may be |
| 587 |
# raised if it's not possible to upgrade to the target version. |
| 588 |
module.fail_json_aws( |
| 589 |
e, |
| 590 |
msg=f"Couldn't upgrade domain {domain_name} from {current_version} to {next_version}", |
| 591 |
) |
| 592 |
|
| 593 |
if module.check_mode: |
| 594 |
module.exit_json( |
| 595 |
changed=True, |
| 596 |
msg=f"Would have upgraded domain from {current_version} to {next_version} if not in check mode", |
| 597 |
) |
| 598 |
current_version = next_version |
| 599 |
|
| 600 |
if module.params.get("wait"): |
| 601 |
wait_for_domain_status(client, module, domain_name, "domain_available") |
| 602 |
|
| 603 |
|
| 604 |
def set_cluster_config(module, current_domain_config, desired_domain_config, change_set): |
| 605 |
changed = False |
| 606 |
|
| 607 |
cluster_config = desired_domain_config["ClusterConfig"] |
| 608 |
cluster_opts = module.params.get("cluster_config") |
| 609 |
if cluster_opts is not None: |
| 610 |
if cluster_opts.get("instance_type") is not None: |
| 611 |
cluster_config["InstanceType"] = cluster_opts.get("instance_type") |
| 612 |
if cluster_opts.get("instance_count") is not None: |
| 613 |
cluster_config["InstanceCount"] = cluster_opts.get("instance_count") |
| 614 |
if cluster_opts.get("zone_awareness") is not None: |
| 615 |
cluster_config["ZoneAwarenessEnabled"] = cluster_opts.get("zone_awareness") |
| 616 |
if cluster_config["ZoneAwarenessEnabled"]: |
| 617 |
if cluster_opts.get("availability_zone_count") is not None: |
| 618 |
cluster_config["ZoneAwarenessConfig"] = { |
| 619 |
"AvailabilityZoneCount": cluster_opts.get("availability_zone_count"), |
| 620 |
} |
| 621 |
|
| 622 |
if cluster_opts.get("dedicated_master") is not None: |
| 623 |
cluster_config["DedicatedMasterEnabled"] = cluster_opts.get("dedicated_master") |
| 624 |
if cluster_config["DedicatedMasterEnabled"]: |
| 625 |
if cluster_opts.get("dedicated_master_instance_type") is not None: |
| 626 |
cluster_config["DedicatedMasterType"] = cluster_opts.get("dedicated_master_instance_type") |
| 627 |
if cluster_opts.get("dedicated_master_instance_count") is not None: |
| 628 |
cluster_config["DedicatedMasterCount"] = cluster_opts.get("dedicated_master_instance_count") |
| 629 |
|
| 630 |
if cluster_opts.get("warm_enabled") is not None: |
| 631 |
cluster_config["WarmEnabled"] = cluster_opts.get("warm_enabled") |
| 632 |
if cluster_config["WarmEnabled"]: |
| 633 |
if cluster_opts.get("warm_type") is not None: |
| 634 |
cluster_config["WarmType"] = cluster_opts.get("warm_type") |
| 635 |
if cluster_opts.get("warm_count") is not None: |
| 636 |
cluster_config["WarmCount"] = cluster_opts.get("warm_count") |
| 637 |
|
| 638 |
cold_storage_opts = None |
| 639 |
if cluster_opts is not None: |
| 640 |
cold_storage_opts = cluster_opts.get("cold_storage_options") |
| 641 |
if compare_domain_versions(desired_domain_config["EngineVersion"], "Elasticsearch_7.9") < 0: |
| 642 |
# If the engine version is ElasticSearch < 7.9, cold storage is not supported. |
| 643 |
# When querying a domain < 7.9, the AWS API indicates cold storage is disabled (Enabled: False), |
| 644 |
# which makes sense. However, trying to do HTTP POST with Enable: False causes an API error. |
| 645 |
# The 'ColdStorageOptions' attribute should not be present in HTTP POST. |
| 646 |
if cold_storage_opts is not None and cold_storage_opts.get("enabled"): |
| 647 |
module.fail_json(msg="Cold Storage is not supported") |
| 648 |
cluster_config.pop("ColdStorageOptions", None) |
| 649 |
if current_domain_config is not None and "ClusterConfig" in current_domain_config: |
| 650 |
# Remove 'ColdStorageOptions' from the current domain config, otherwise the actual vs desired diff |
| 651 |
# will indicate a change must be done. |
| 652 |
current_domain_config["ClusterConfig"].pop("ColdStorageOptions", None) |
| 653 |
else: |
| 654 |
# Elasticsearch 7.9 and above support ColdStorageOptions. |
| 655 |
if cold_storage_opts is not None and cold_storage_opts.get("enabled") is not None: |
| 656 |
cluster_config["ColdStorageOptions"] = { |
| 657 |
"Enabled": cold_storage_opts.get("enabled"), |
| 658 |
} |
| 659 |
|
| 660 |
if current_domain_config is not None and current_domain_config["ClusterConfig"] != cluster_config: |
| 661 |
change_set.append(f"ClusterConfig changed from {current_domain_config['ClusterConfig']} to {cluster_config}") |
| 662 |
changed = True |
| 663 |
return changed |
| 664 |
|
| 665 |
|
| 666 |
def set_ebs_options(module, current_domain_config, desired_domain_config, change_set): |
| 667 |
changed = False |
| 668 |
ebs_config = desired_domain_config["EBSOptions"] |
| 669 |
ebs_opts = module.params.get("ebs_options") |
| 670 |
if ebs_opts is None: |
| 671 |
return changed |
| 672 |
if ebs_opts.get("ebs_enabled") is not None: |
| 673 |
ebs_config["EBSEnabled"] = ebs_opts.get("ebs_enabled") |
| 674 |
|
| 675 |
if not ebs_config["EBSEnabled"]: |
| 676 |
desired_domain_config["EBSOptions"] = { |
| 677 |
"EBSEnabled": False, |
| 678 |
} |
| 679 |
else: |
| 680 |
if ebs_opts.get("volume_type") is not None: |
| 681 |
ebs_config["VolumeType"] = ebs_opts.get("volume_type") |
| 682 |
if ebs_opts.get("volume_size") is not None: |
| 683 |
ebs_config["VolumeSize"] = ebs_opts.get("volume_size") |
| 684 |
if ebs_opts.get("iops") is not None: |
| 685 |
ebs_config["Iops"] = ebs_opts.get("iops") |
| 686 |
|
| 687 |
if current_domain_config is not None and current_domain_config["EBSOptions"] != ebs_config: |
| 688 |
change_set.append(f"EBSOptions changed from {current_domain_config['EBSOptions']} to {ebs_config}") |
| 689 |
changed = True |
| 690 |
return changed |
| 691 |
|
| 692 |
|
| 693 |
def set_encryption_at_rest_options(module, current_domain_config, desired_domain_config, change_set): |
| 694 |
changed = False |
| 695 |
encryption_at_rest_config = desired_domain_config["EncryptionAtRestOptions"] |
| 696 |
encryption_at_rest_opts = module.params.get("encryption_at_rest_options") |
| 697 |
if encryption_at_rest_opts is None: |
| 698 |
return False |
| 699 |
if encryption_at_rest_opts.get("enabled") is not None: |
| 700 |
encryption_at_rest_config["Enabled"] = encryption_at_rest_opts.get("enabled") |
| 701 |
if not encryption_at_rest_config["Enabled"]: |
| 702 |
desired_domain_config["EncryptionAtRestOptions"] = { |
| 703 |
"Enabled": False, |
| 704 |
} |
| 705 |
else: |
| 706 |
if encryption_at_rest_opts.get("kms_key_id") is not None: |
| 707 |
encryption_at_rest_config["KmsKeyId"] = encryption_at_rest_opts.get("kms_key_id") |
| 708 |
|
| 709 |
if ( |
| 710 |
current_domain_config is not None |
| 711 |
and current_domain_config["EncryptionAtRestOptions"] != encryption_at_rest_config |
| 712 |
): |
| 713 |
change_set.append( |
| 714 |
f"EncryptionAtRestOptions changed from {current_domain_config['EncryptionAtRestOptions']} to" |
| 715 |
f" {encryption_at_rest_config}" |
| 716 |
) |
| 717 |
changed = True |
| 718 |
return changed |
| 719 |
|
| 720 |
|
| 721 |
def set_node_to_node_encryption_options(module, current_domain_config, desired_domain_config, change_set): |
| 722 |
changed = False |
| 723 |
node_to_node_encryption_config = desired_domain_config["NodeToNodeEncryptionOptions"] |
| 724 |
node_to_node_encryption_opts = module.params.get("node_to_node_encryption_options") |
| 725 |
if node_to_node_encryption_opts is None: |
| 726 |
return changed |
| 727 |
if node_to_node_encryption_opts.get("enabled") is not None: |
| 728 |
node_to_node_encryption_config["Enabled"] = node_to_node_encryption_opts.get("enabled") |
| 729 |
|
| 730 |
if ( |
| 731 |
current_domain_config is not None |
| 732 |
and current_domain_config["NodeToNodeEncryptionOptions"] != node_to_node_encryption_config |
| 733 |
): |
| 734 |
change_set.append( |
| 735 |
f"NodeToNodeEncryptionOptions changed from {current_domain_config['NodeToNodeEncryptionOptions']} to" |
| 736 |
f" {node_to_node_encryption_config}" |
| 737 |
) |
| 738 |
changed = True |
| 739 |
return changed |
| 740 |
|
| 741 |
|
| 742 |
def set_vpc_options(module, current_domain_config, desired_domain_config, change_set): |
| 743 |
changed = False |
| 744 |
vpc_config = None |
| 745 |
if "VPCOptions" in desired_domain_config: |
| 746 |
vpc_config = desired_domain_config["VPCOptions"] |
| 747 |
vpc_opts = module.params.get("vpc_options") |
| 748 |
if vpc_opts is None: |
| 749 |
return changed |
| 750 |
vpc_subnets = vpc_opts.get("subnets") |
| 751 |
if vpc_subnets is not None: |
| 752 |
if vpc_config is None: |
| 753 |
vpc_config = {} |
| 754 |
desired_domain_config["VPCOptions"] = vpc_config |
| 755 |
# OpenSearch cluster is attached to VPC |
| 756 |
if isinstance(vpc_subnets, str): |
| 757 |
vpc_subnets = [x.strip() for x in vpc_subnets.split(",")] |
| 758 |
vpc_config["SubnetIds"] = vpc_subnets |
| 759 |
|
| 760 |
vpc_security_groups = vpc_opts.get("security_groups") |
| 761 |
if vpc_security_groups is not None: |
| 762 |
if vpc_config is None: |
| 763 |
vpc_config = {} |
| 764 |
desired_domain_config["VPCOptions"] = vpc_config |
| 765 |
if isinstance(vpc_security_groups, str): |
| 766 |
vpc_security_groups = [x.strip() for x in vpc_security_groups.split(",")] |
| 767 |
vpc_config["SecurityGroupIds"] = vpc_security_groups |
| 768 |
|
| 769 |
if current_domain_config is not None: |
| 770 |
# Modify existing cluster. |
| 771 |
current_cluster_is_vpc = False |
| 772 |
desired_cluster_is_vpc = False |
| 773 |
if ( |
| 774 |
"VPCOptions" in current_domain_config |
| 775 |
and "SubnetIds" in current_domain_config["VPCOptions"] |
| 776 |
and len(current_domain_config["VPCOptions"]["SubnetIds"]) > 0 |
| 777 |
): |
| 778 |
current_cluster_is_vpc = True |
| 779 |
if ( |
| 780 |
"VPCOptions" in desired_domain_config |
| 781 |
and "SubnetIds" in desired_domain_config["VPCOptions"] |
| 782 |
and len(desired_domain_config["VPCOptions"]["SubnetIds"]) > 0 |
| 783 |
): |
| 784 |
desired_cluster_is_vpc = True |
| 785 |
if current_cluster_is_vpc != desired_cluster_is_vpc: |
| 786 |
# AWS does not allow changing the type. Don't fail here so we return the AWS API error. |
| 787 |
change_set.append("VPCOptions changed between Internet and VPC") |
| 788 |
changed = True |
| 789 |
elif desired_cluster_is_vpc is False: |
| 790 |
# There are no VPCOptions to configure. |
| 791 |
pass |
| 792 |
else: |
| 793 |
# Note the subnets may be the same but be listed in a different order. |
| 794 |
if set(current_domain_config["VPCOptions"]["SubnetIds"]) != set(vpc_config["SubnetIds"]): |
| 795 |
change_set.append( |
| 796 |
f"SubnetIds changed from {current_domain_config['VPCOptions']['SubnetIds']} to" |
| 797 |
f" {vpc_config['SubnetIds']}" |
| 798 |
) |
| 799 |
changed = True |
| 800 |
if set(current_domain_config["VPCOptions"]["SecurityGroupIds"]) != set(vpc_config["SecurityGroupIds"]): |
| 801 |
change_set.append( |
| 802 |
f"SecurityGroup changed from {current_domain_config['VPCOptions']['SecurityGroupIds']} to" |
| 803 |
f" {vpc_config['SecurityGroupIds']}" |
| 804 |
) |
| 805 |
changed = True |
| 806 |
return changed |
| 807 |
|
| 808 |
|
| 809 |
def set_snapshot_options(module, current_domain_config, desired_domain_config, change_set): |
| 810 |
changed = False |
| 811 |
snapshot_config = desired_domain_config["SnapshotOptions"] |
| 812 |
snapshot_opts = module.params.get("snapshot_options") |
| 813 |
if snapshot_opts is None: |
| 814 |
return changed |
| 815 |
if snapshot_opts.get("automated_snapshot_start_hour") is not None: |
| 816 |
snapshot_config["AutomatedSnapshotStartHour"] = snapshot_opts.get("automated_snapshot_start_hour") |
| 817 |
if current_domain_config is not None and current_domain_config["SnapshotOptions"] != snapshot_config: |
| 818 |
change_set.append("SnapshotOptions changed") |
| 819 |
changed = True |
| 820 |
return changed |
| 821 |
|
| 822 |
|
| 823 |
def set_cognito_options(module, current_domain_config, desired_domain_config, change_set): |
| 824 |
changed = False |
| 825 |
cognito_config = desired_domain_config["CognitoOptions"] |
| 826 |
cognito_opts = module.params.get("cognito_options") |
| 827 |
if cognito_opts is None: |
| 828 |
return changed |
| 829 |
if cognito_opts.get("enabled") is not None: |
| 830 |
cognito_config["Enabled"] = cognito_opts.get("enabled") |
| 831 |
if not cognito_config["Enabled"]: |
| 832 |
desired_domain_config["CognitoOptions"] = { |
| 833 |
"Enabled": False, |
| 834 |
} |
| 835 |
else: |
| 836 |
if cognito_opts.get("cognito_user_pool_id") is not None: |
| 837 |
cognito_config["UserPoolId"] = cognito_opts.get("cognito_user_pool_id") |
| 838 |
if cognito_opts.get("cognito_identity_pool_id") is not None: |
| 839 |
cognito_config["IdentityPoolId"] = cognito_opts.get("cognito_identity_pool_id") |
| 840 |
if cognito_opts.get("cognito_role_arn") is not None: |
| 841 |
cognito_config["RoleArn"] = cognito_opts.get("cognito_role_arn") |
| 842 |
|
| 843 |
if current_domain_config is not None and current_domain_config["CognitoOptions"] != cognito_config: |
| 844 |
change_set.append(f"CognitoOptions changed from {current_domain_config['CognitoOptions']} to {cognito_config}") |
| 845 |
changed = True |
| 846 |
return changed |
| 847 |
|
| 848 |
|
| 849 |
def set_advanced_security_options(module, current_domain_config, desired_domain_config, change_set): |
| 850 |
changed = False |
| 851 |
advanced_security_config = desired_domain_config["AdvancedSecurityOptions"] |
| 852 |
advanced_security_opts = module.params.get("advanced_security_options") |
| 853 |
if advanced_security_opts is None: |
| 854 |
return changed |
| 855 |
if advanced_security_opts.get("enabled") is not None: |
| 856 |
advanced_security_config["Enabled"] = advanced_security_opts.get("enabled") |
| 857 |
if not advanced_security_config["Enabled"]: |
| 858 |
desired_domain_config["AdvancedSecurityOptions"] = { |
| 859 |
"Enabled": False, |
| 860 |
} |
| 861 |
else: |
| 862 |
if advanced_security_opts.get("internal_user_database_enabled") is not None: |
| 863 |
advanced_security_config["InternalUserDatabaseEnabled"] = advanced_security_opts.get( |
| 864 |
"internal_user_database_enabled" |
| 865 |
) |
| 866 |
master_user_opts = advanced_security_opts.get("master_user_options") |
| 867 |
if master_user_opts is not None: |
| 868 |
advanced_security_config.setdefault("MasterUserOptions", {}) |
| 869 |
if master_user_opts.get("master_user_arn") is not None: |
| 870 |
advanced_security_config["MasterUserOptions"]["MasterUserARN"] = master_user_opts.get("master_user_arn") |
| 871 |
if master_user_opts.get("master_user_name") is not None: |
| 872 |
advanced_security_config["MasterUserOptions"]["MasterUserName"] = master_user_opts.get( |
| 873 |
"master_user_name" |
| 874 |
) |
| 875 |
if master_user_opts.get("master_user_password") is not None: |
| 876 |
advanced_security_config["MasterUserOptions"]["MasterUserPassword"] = master_user_opts.get( |
| 877 |
"master_user_password" |
| 878 |
) |
| 879 |
saml_opts = advanced_security_opts.get("saml_options") |
| 880 |
if saml_opts is not None: |
| 881 |
if saml_opts.get("enabled") is not None: |
| 882 |
advanced_security_config["SamlOptions"]["Enabled"] = saml_opts.get("enabled") |
| 883 |
idp_opts = saml_opts.get("idp") |
| 884 |
if idp_opts is not None: |
| 885 |
if idp_opts.get("metadata_content") is not None: |
| 886 |
advanced_security_config["SamlOptions"]["Idp"]["MetadataContent"] = idp_opts.get("metadata_content") |
| 887 |
if idp_opts.get("entity_id") is not None: |
| 888 |
advanced_security_config["SamlOptions"]["Idp"]["EntityId"] = idp_opts.get("entity_id") |
| 889 |
if saml_opts.get("master_user_name") is not None: |
| 890 |
advanced_security_config["SamlOptions"]["MasterUserName"] = saml_opts.get("master_user_name") |
| 891 |
if saml_opts.get("master_backend_role") is not None: |
| 892 |
advanced_security_config["SamlOptions"]["MasterBackendRole"] = saml_opts.get("master_backend_role") |
| 893 |
if saml_opts.get("subject_key") is not None: |
| 894 |
advanced_security_config["SamlOptions"]["SubjectKey"] = saml_opts.get("subject_key") |
| 895 |
if saml_opts.get("roles_key") is not None: |
| 896 |
advanced_security_config["SamlOptions"]["RolesKey"] = saml_opts.get("roles_key") |
| 897 |
if saml_opts.get("session_timeout_minutes") is not None: |
| 898 |
advanced_security_config["SamlOptions"]["SessionTimeoutMinutes"] = saml_opts.get( |
| 899 |
"session_timeout_minutes" |
| 900 |
) |
| 901 |
|
| 902 |
if ( |
| 903 |
current_domain_config is not None |
| 904 |
and current_domain_config["AdvancedSecurityOptions"] != advanced_security_config |
| 905 |
): |
| 906 |
change_set.append( |
| 907 |
f"AdvancedSecurityOptions changed from {current_domain_config['AdvancedSecurityOptions']} to" |
| 908 |
f" {advanced_security_config}" |
| 909 |
) |
| 910 |
changed = True |
| 911 |
return changed |
| 912 |
|
| 913 |
|
| 914 |
def set_domain_endpoint_options(module, current_domain_config, desired_domain_config, change_set): |
| 915 |
changed = False |
| 916 |
domain_endpoint_config = desired_domain_config["DomainEndpointOptions"] |
| 917 |
domain_endpoint_opts = module.params.get("domain_endpoint_options") |
| 918 |
if domain_endpoint_opts is None: |
| 919 |
return changed |
| 920 |
if domain_endpoint_opts.get("enforce_https") is not None: |
| 921 |
domain_endpoint_config["EnforceHTTPS"] = domain_endpoint_opts.get("enforce_https") |
| 922 |
if domain_endpoint_opts.get("tls_security_policy") is not None: |
| 923 |
domain_endpoint_config["TLSSecurityPolicy"] = domain_endpoint_opts.get("tls_security_policy") |
| 924 |
if domain_endpoint_opts.get("custom_endpoint_enabled") is not None: |
| 925 |
domain_endpoint_config["CustomEndpointEnabled"] = domain_endpoint_opts.get("custom_endpoint_enabled") |
| 926 |
if domain_endpoint_config["CustomEndpointEnabled"]: |
| 927 |
if domain_endpoint_opts.get("custom_endpoint") is not None: |
| 928 |
domain_endpoint_config["CustomEndpoint"] = domain_endpoint_opts.get("custom_endpoint") |
| 929 |
if domain_endpoint_opts.get("custom_endpoint_certificate_arn") is not None: |
| 930 |
domain_endpoint_config["CustomEndpointCertificateArn"] = domain_endpoint_opts.get( |
| 931 |
"custom_endpoint_certificate_arn" |
| 932 |
) |
| 933 |
|
| 934 |
if current_domain_config is not None and current_domain_config["DomainEndpointOptions"] != domain_endpoint_config: |
| 935 |
change_set.append( |
| 936 |
f"DomainEndpointOptions changed from {current_domain_config['DomainEndpointOptions']} to" |
| 937 |
f" {domain_endpoint_config}" |
| 938 |
) |
| 939 |
changed = True |
| 940 |
return changed |
| 941 |
|
| 942 |
|
| 943 |
def set_auto_tune_options(module, current_domain_config, desired_domain_config, change_set): |
| 944 |
changed = False |
| 945 |
auto_tune_config = desired_domain_config["AutoTuneOptions"] |
| 946 |
auto_tune_opts = module.params.get("auto_tune_options") |
| 947 |
if auto_tune_opts is None: |
| 948 |
return changed |
| 949 |
schedules = auto_tune_opts.get("maintenance_schedules") |
| 950 |
if auto_tune_opts.get("desired_state") is not None: |
| 951 |
auto_tune_config["DesiredState"] = auto_tune_opts.get("desired_state") |
| 952 |
if auto_tune_config["DesiredState"] != "ENABLED": |
| 953 |
desired_domain_config["AutoTuneOptions"] = { |
| 954 |
"DesiredState": "DISABLED", |
| 955 |
} |
| 956 |
elif schedules is not None: |
| 957 |
auto_tune_config["MaintenanceSchedules"] = [] |
| 958 |
for s in schedules: |
| 959 |
schedule_entry = {} |
| 960 |
start_at = s.get("start_at") |
| 961 |
if start_at is not None: |
| 962 |
if isinstance(start_at, datetime.datetime): |
| 963 |
# The property was parsed from yaml to datetime, but the AWS API wants a string |
| 964 |
start_at = start_at.strftime("%Y-%m-%d") |
| 965 |
schedule_entry["StartAt"] = start_at |
| 966 |
duration_opt = s.get("duration") |
| 967 |
if duration_opt is not None: |
| 968 |
schedule_entry["Duration"] = {} |
| 969 |
if duration_opt.get("value") is not None: |
| 970 |
schedule_entry["Duration"]["Value"] = duration_opt.get("value") |
| 971 |
if duration_opt.get("unit") is not None: |
| 972 |
schedule_entry["Duration"]["Unit"] = duration_opt.get("unit") |
| 973 |
if s.get("cron_expression_for_recurrence") is not None: |
| 974 |
schedule_entry["CronExpressionForRecurrence"] = s.get("cron_expression_for_recurrence") |
| 975 |
auto_tune_config["MaintenanceSchedules"].append(schedule_entry) |
| 976 |
if current_domain_config is not None: |
| 977 |
if current_domain_config["AutoTuneOptions"]["DesiredState"] != auto_tune_config["DesiredState"]: |
| 978 |
change_set.append( |
| 979 |
"AutoTuneOptions.DesiredState changed from" |
| 980 |
f" {current_domain_config['AutoTuneOptions']['DesiredState']} to {auto_tune_config['DesiredState']}" |
| 981 |
) |
| 982 |
changed = True |
| 983 |
if auto_tune_config["MaintenanceSchedules"] != current_domain_config["AutoTuneOptions"]["MaintenanceSchedules"]: |
| 984 |
change_set.append( |
| 985 |
"AutoTuneOptions.MaintenanceSchedules changed from" |
| 986 |
f" {current_domain_config['AutoTuneOptions']['MaintenanceSchedules']} to" |
| 987 |
f" {auto_tune_config['MaintenanceSchedules']}" |
| 988 |
) |
| 989 |
changed = True |
| 990 |
return changed |
| 991 |
|
| 992 |
|
| 993 |
def set_access_policy(module, current_domain_config, desired_domain_config, change_set): |
| 994 |
access_policy_config = None |
| 995 |
changed = False |
| 996 |
access_policy_opt = module.params.get("access_policies") |
| 997 |
if access_policy_opt is None: |
| 998 |
return changed |
| 999 |
try: |
| 1000 |
access_policy_config = json.dumps(access_policy_opt) |
| 1001 |
except Exception as e: |
| 1002 |
module.fail_json(msg=f"Failed to convert the policy into valid JSON: {str(e)}") |
| 1003 |
if current_domain_config is not None: |
| 1004 |
# Updating existing domain |
| 1005 |
current_access_policy = json.loads(current_domain_config["AccessPolicies"]) |
| 1006 |
if not compare_policies(current_access_policy, access_policy_opt): |
| 1007 |
change_set.append(f"AccessPolicy changed from {current_access_policy} to {access_policy_opt}") |
| 1008 |
changed = True |
| 1009 |
desired_domain_config["AccessPolicies"] = access_policy_config |
| 1010 |
else: |
| 1011 |
# Creating new domain |
| 1012 |
desired_domain_config["AccessPolicies"] = access_policy_config |
| 1013 |
return changed |
| 1014 |
|
| 1015 |
|
| 1016 |
def ensure_domain_present(client, module): |
| 1017 |
domain_name = module.params.get("domain_name") |
| 1018 |
|
| 1019 |
# Create default if OpenSearch does not exist. If domain already exists, |
| 1020 |
# the data is populated by retrieving the current configuration from the API. |
| 1021 |
desired_domain_config = { |
| 1022 |
"DomainName": module.params.get("domain_name"), |
| 1023 |
"EngineVersion": "OpenSearch_1.1", |
| 1024 |
"ClusterConfig": { |
| 1025 |
"InstanceType": "t2.small.search", |
| 1026 |
"InstanceCount": 2, |
| 1027 |
"ZoneAwarenessEnabled": False, |
| 1028 |
"DedicatedMasterEnabled": False, |
| 1029 |
"WarmEnabled": False, |
| 1030 |
}, |
| 1031 |
# By default create ES attached to the Internet. |
| 1032 |
# If the "VPCOptions" property is specified, even if empty, the API server interprets |
| 1033 |
# as incomplete VPC configuration. |
| 1034 |
# "VPCOptions": {}, |
| 1035 |
"EBSOptions": { |
| 1036 |
"EBSEnabled": False, |
| 1037 |
}, |
| 1038 |
"EncryptionAtRestOptions": { |
| 1039 |
"Enabled": False, |
| 1040 |
}, |
| 1041 |
"NodeToNodeEncryptionOptions": { |
| 1042 |
"Enabled": False, |
| 1043 |
}, |
| 1044 |
"SnapshotOptions": { |
| 1045 |
"AutomatedSnapshotStartHour": 0, |
| 1046 |
}, |
| 1047 |
"CognitoOptions": { |
| 1048 |
"Enabled": False, |
| 1049 |
}, |
| 1050 |
"AdvancedSecurityOptions": { |
| 1051 |
"Enabled": False, |
| 1052 |
}, |
| 1053 |
"DomainEndpointOptions": { |
| 1054 |
"CustomEndpointEnabled": False, |
| 1055 |
}, |
| 1056 |
"AutoTuneOptions": { |
| 1057 |
"DesiredState": "DISABLED", |
| 1058 |
}, |
| 1059 |
} |
| 1060 |
# Determine if OpenSearch domain already exists. |
| 1061 |
# current_domain_config may be None if the domain does not exist. |
| 1062 |
(current_domain_config, domain_arn) = get_domain_config(client, module, domain_name) |
| 1063 |
if current_domain_config is not None: |
| 1064 |
desired_domain_config = deepcopy(current_domain_config) |
| 1065 |
|
| 1066 |
if module.params.get("engine_version") is not None: |
| 1067 |
# Validate the engine_version |
| 1068 |
v = parse_version(module.params.get("engine_version")) |
| 1069 |
if v is None: |
| 1070 |
module.fail_json("Invalid engine_version. Must be Elasticsearch_X.Y or OpenSearch_X.Y") |
| 1071 |
desired_domain_config["EngineVersion"] = module.params.get("engine_version") |
| 1072 |
|
| 1073 |
changed = False |
| 1074 |
change_set = [] # For check mode purpose |
| 1075 |
|
| 1076 |
changed |= set_cluster_config(module, current_domain_config, desired_domain_config, change_set) |
| 1077 |
changed |= set_ebs_options(module, current_domain_config, desired_domain_config, change_set) |
| 1078 |
changed |= set_encryption_at_rest_options(module, current_domain_config, desired_domain_config, change_set) |
| 1079 |
changed |= set_node_to_node_encryption_options(module, current_domain_config, desired_domain_config, change_set) |
| 1080 |
changed |= set_vpc_options(module, current_domain_config, desired_domain_config, change_set) |
| 1081 |
changed |= set_snapshot_options(module, current_domain_config, desired_domain_config, change_set) |
| 1082 |
changed |= set_cognito_options(module, current_domain_config, desired_domain_config, change_set) |
| 1083 |
changed |= set_advanced_security_options(module, current_domain_config, desired_domain_config, change_set) |
| 1084 |
changed |= set_domain_endpoint_options(module, current_domain_config, desired_domain_config, change_set) |
| 1085 |
changed |= set_auto_tune_options(module, current_domain_config, desired_domain_config, change_set) |
| 1086 |
changed |= set_access_policy(module, current_domain_config, desired_domain_config, change_set) |
| 1087 |
|
| 1088 |
if current_domain_config is not None: |
| 1089 |
if desired_domain_config["EngineVersion"] != current_domain_config["EngineVersion"]: |
| 1090 |
changed = True |
| 1091 |
change_set.append("EngineVersion changed") |
| 1092 |
upgrade_domain( |
| 1093 |
client, |
| 1094 |
module, |
| 1095 |
current_domain_config["EngineVersion"], |
| 1096 |
desired_domain_config["EngineVersion"], |
| 1097 |
) |
| 1098 |
|
| 1099 |
if changed: |
| 1100 |
if module.check_mode: |
| 1101 |
module.exit_json( |
| 1102 |
changed=True, |
| 1103 |
msg=f"Would have updated domain if not in check mode: {change_set}", |
| 1104 |
) |
| 1105 |
# Remove the "EngineVersion" attribute, the AWS API does not accept this attribute. |
| 1106 |
desired_domain_config.pop("EngineVersion", None) |
| 1107 |
try: |
| 1108 |
client.update_domain_config(**desired_domain_config) |
| 1109 |
except ( |
| 1110 |
botocore.exceptions.BotoCoreError, |
| 1111 |
botocore.exceptions.ClientError, |
| 1112 |
) as e: |
| 1113 |
module.fail_json_aws(e, msg=f"Couldn't update domain {domain_name}") |
| 1114 |
|
| 1115 |
else: |
| 1116 |
# Create new OpenSearch cluster |
| 1117 |
if module.params.get("access_policies") is None: |
| 1118 |
module.fail_json("state is present but the following is missing: access_policies") |
| 1119 |
|
| 1120 |
changed = True |
| 1121 |
if module.check_mode: |
| 1122 |
module.exit_json(changed=True, msg="Would have created a domain if not in check mode") |
| 1123 |
try: |
| 1124 |
response = client.create_domain(**desired_domain_config) |
| 1125 |
domain = response["DomainStatus"] |
| 1126 |
domain_arn = domain["ARN"] |
| 1127 |
except ( |
| 1128 |
botocore.exceptions.BotoCoreError, |
| 1129 |
botocore.exceptions.ClientError, |
| 1130 |
) as e: |
| 1131 |
module.fail_json_aws(e, msg=f"Couldn't update domain {domain_name}") |
| 1132 |
|
| 1133 |
try: |
| 1134 |
existing_tags = boto3_tag_list_to_ansible_dict(client.list_tags(ARN=domain_arn, aws_retry=True)["TagList"]) |
| 1135 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 1136 |
module.fail_json_aws(e, f"Couldn't get tags for domain {domain_name}") |
| 1137 |
|
| 1138 |
desired_tags = module.params["tags"] |
| 1139 |
purge_tags = module.params["purge_tags"] |
| 1140 |
changed |= ensure_tags(client, module, domain_arn, existing_tags, desired_tags, purge_tags) |
| 1141 |
|
| 1142 |
if module.params.get("wait") and not module.check_mode: |
| 1143 |
wait_for_domain_status(client, module, domain_name, "domain_available") |
| 1144 |
|
| 1145 |
domain = get_domain_status(client, module, domain_name) |
| 1146 |
|
| 1147 |
return dict(changed=changed, **normalize_opensearch(client, module, domain)) |
| 1148 |
|
| 1149 |
|
| 1150 |
def main(): |
| 1151 |
module = AnsibleAWSModule( |
| 1152 |
argument_spec=dict( |
| 1153 |
state=dict(choices=["present", "absent"], default="present"), |
| 1154 |
domain_name=dict(required=True), |
| 1155 |
engine_version=dict(), |
| 1156 |
allow_intermediate_upgrades=dict(required=False, type="bool", default=True), |
| 1157 |
access_policies=dict(required=False, type="dict"), |
| 1158 |
cluster_config=dict( |
| 1159 |
type="dict", |
| 1160 |
default=None, |
| 1161 |
options=dict( |
| 1162 |
instance_type=dict(), |
| 1163 |
instance_count=dict(required=False, type="int"), |
| 1164 |
zone_awareness=dict(required=False, type="bool"), |
| 1165 |
availability_zone_count=dict(required=False, type="int"), |
| 1166 |
dedicated_master=dict(required=False, type="bool"), |
| 1167 |
dedicated_master_instance_type=dict(), |
| 1168 |
dedicated_master_instance_count=dict(type="int"), |
| 1169 |
warm_enabled=dict(required=False, type="bool"), |
| 1170 |
warm_type=dict(required=False), |
| 1171 |
warm_count=dict(required=False, type="int"), |
| 1172 |
cold_storage_options=dict( |
| 1173 |
type="dict", |
| 1174 |
default=None, |
| 1175 |
options=dict( |
| 1176 |
enabled=dict(required=False, type="bool"), |
| 1177 |
), |
| 1178 |
), |
| 1179 |
), |
| 1180 |
), |
| 1181 |
snapshot_options=dict( |
| 1182 |
type="dict", |
| 1183 |
default=None, |
| 1184 |
options=dict( |
| 1185 |
automated_snapshot_start_hour=dict(required=False, type="int"), |
| 1186 |
), |
| 1187 |
), |
| 1188 |
ebs_options=dict( |
| 1189 |
type="dict", |
| 1190 |
default=None, |
| 1191 |
options=dict( |
| 1192 |
ebs_enabled=dict(required=False, type="bool"), |
| 1193 |
volume_type=dict(required=False), |
| 1194 |
volume_size=dict(required=False, type="int"), |
| 1195 |
iops=dict(required=False, type="int"), |
| 1196 |
), |
| 1197 |
), |
| 1198 |
vpc_options=dict( |
| 1199 |
type="dict", |
| 1200 |
default=None, |
| 1201 |
options=dict( |
| 1202 |
subnets=dict(type="list", elements="str", required=False), |
| 1203 |
security_groups=dict(type="list", elements="str", required=False), |
| 1204 |
), |
| 1205 |
), |
| 1206 |
cognito_options=dict( |
| 1207 |
type="dict", |
| 1208 |
default=None, |
| 1209 |
options=dict( |
| 1210 |
enabled=dict(required=False, type="bool"), |
| 1211 |
user_pool_id=dict(required=False), |
| 1212 |
identity_pool_id=dict(required=False), |
| 1213 |
role_arn=dict(required=False, no_log=False), |
| 1214 |
), |
| 1215 |
), |
| 1216 |
encryption_at_rest_options=dict( |
| 1217 |
type="dict", |
| 1218 |
default=None, |
| 1219 |
options=dict( |
| 1220 |
enabled=dict(type="bool"), |
| 1221 |
kms_key_id=dict(required=False), |
| 1222 |
), |
| 1223 |
), |
| 1224 |
node_to_node_encryption_options=dict( |
| 1225 |
type="dict", |
| 1226 |
default=None, |
| 1227 |
options=dict( |
| 1228 |
enabled=dict(type="bool"), |
| 1229 |
), |
| 1230 |
), |
| 1231 |
domain_endpoint_options=dict( |
| 1232 |
type="dict", |
| 1233 |
default=None, |
| 1234 |
options=dict( |
| 1235 |
enforce_https=dict(type="bool"), |
| 1236 |
tls_security_policy=dict(), |
| 1237 |
custom_endpoint_enabled=dict(type="bool"), |
| 1238 |
custom_endpoint=dict(), |
| 1239 |
custom_endpoint_certificate_arn=dict(), |
| 1240 |
), |
| 1241 |
), |
| 1242 |
advanced_security_options=dict( |
| 1243 |
type="dict", |
| 1244 |
default=None, |
| 1245 |
options=dict( |
| 1246 |
enabled=dict(type="bool"), |
| 1247 |
internal_user_database_enabled=dict(type="bool"), |
| 1248 |
master_user_options=dict( |
| 1249 |
type="dict", |
| 1250 |
default=None, |
| 1251 |
options=dict( |
| 1252 |
master_user_arn=dict(), |
| 1253 |
master_user_name=dict(), |
| 1254 |
master_user_password=dict(no_log=True), |
| 1255 |
), |
| 1256 |
), |
| 1257 |
saml_options=dict( |
| 1258 |
type="dict", |
| 1259 |
default=None, |
| 1260 |
options=dict( |
| 1261 |
enabled=dict(type="bool"), |
| 1262 |
idp=dict( |
| 1263 |
type="dict", |
| 1264 |
default=None, |
| 1265 |
options=dict( |
| 1266 |
metadata_content=dict(), |
| 1267 |
entity_id=dict(), |
| 1268 |
), |
| 1269 |
), |
| 1270 |
master_user_name=dict(), |
| 1271 |
master_backend_role=dict(), |
| 1272 |
subject_key=dict(no_log=False), |
| 1273 |
roles_key=dict(no_log=False), |
| 1274 |
session_timeout_minutes=dict(type="int"), |
| 1275 |
), |
| 1276 |
), |
| 1277 |
), |
| 1278 |
), |
| 1279 |
auto_tune_options=dict( |
| 1280 |
type="dict", |
| 1281 |
default=None, |
| 1282 |
options=dict( |
| 1283 |
desired_state=dict(choices=["ENABLED", "DISABLED"]), |
| 1284 |
maintenance_schedules=dict( |
| 1285 |
type="list", |
| 1286 |
elements="dict", |
| 1287 |
default=None, |
| 1288 |
options=dict( |
| 1289 |
start_at=dict(), |
| 1290 |
duration=dict( |
| 1291 |
type="dict", |
| 1292 |
default=None, |
| 1293 |
options=dict( |
| 1294 |
value=dict(type="int"), |
| 1295 |
unit=dict(choices=["HOURS"]), |
| 1296 |
), |
| 1297 |
), |
| 1298 |
cron_expression_for_recurrence=dict(), |
| 1299 |
), |
| 1300 |
), |
| 1301 |
), |
| 1302 |
), |
| 1303 |
tags=dict(type="dict", aliases=["resource_tags"]), |
| 1304 |
purge_tags=dict(type="bool", default=True), |
| 1305 |
wait=dict(type="bool", default=False), |
| 1306 |
wait_timeout=dict(type="int", default=300), |
| 1307 |
), |
| 1308 |
supports_check_mode=True, |
| 1309 |
) |
| 1310 |
|
| 1311 |
try: |
| 1312 |
client = module.client("opensearch", retry_decorator=AWSRetry.jittered_backoff()) |
| 1313 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 1314 |
module.fail_json_aws(e, msg="Failed to connect to AWS opensearch service") |
| 1315 |
|
| 1316 |
if module.params["state"] == "absent": |
| 1317 |
ret_dict = ensure_domain_absent(client, module) |
| 1318 |
else: |
| 1319 |
ret_dict = ensure_domain_present(client, module) |
| 1320 |
|
| 1321 |
module.exit_json(**ret_dict) |
| 1322 |
|
| 1323 |
|
| 1324 |
if __name__ == "__main__": |
| 1325 |
main() |