| 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: elb_target_group |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Manage a target group for an Application or Network load balancer |
| 12 |
description: |
| 13 |
- Manage an AWS Elastic Load Balancer target group. See |
| 14 |
U(https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-target-groups.html) or |
| 15 |
U(https://docs.aws.amazon.com/elasticloadbalancing/latest/network/load-balancer-target-groups.html) for details. |
| 16 |
author: |
| 17 |
- "Rob White (@wimnat)" |
| 18 |
options: |
| 19 |
deregistration_delay_timeout: |
| 20 |
description: |
| 21 |
- The amount time for Elastic Load Balancing to wait before changing the state of a deregistering target from draining to unused. |
| 22 |
The range is 0-3600 seconds. |
| 23 |
type: int |
| 24 |
deregistration_connection_termination: |
| 25 |
description: |
| 26 |
- Indicates whether the load balancer terminates connections at the end of the deregistration timeout. |
| 27 |
- Using this option is only supported when attaching to a Network Load Balancer (NLB). |
| 28 |
type: bool |
| 29 |
default: false |
| 30 |
required: false |
| 31 |
version_added: 3.1.0 |
| 32 |
health_check_protocol: |
| 33 |
description: |
| 34 |
- The protocol the load balancer uses when performing health checks on targets. |
| 35 |
required: false |
| 36 |
choices: [ 'http', 'https', 'tcp', 'tls', 'udp', 'tcp_udp', 'HTTP', 'HTTPS', 'TCP', 'TLS', 'UDP', 'TCP_UDP'] |
| 37 |
type: str |
| 38 |
health_check_port: |
| 39 |
description: |
| 40 |
- The port the load balancer uses when performing health checks on targets. |
| 41 |
Can be set to 'traffic-port' to match target port. |
| 42 |
- When not defined will default to the port on which each target receives traffic from the load balancer. |
| 43 |
required: false |
| 44 |
type: str |
| 45 |
health_check_path: |
| 46 |
description: |
| 47 |
- The ping path that is the destination on the targets for health checks. The path must be defined in order to set a health check. |
| 48 |
- Requires the I(health_check_protocol) parameter to be set. |
| 49 |
required: false |
| 50 |
type: str |
| 51 |
health_check_interval: |
| 52 |
description: |
| 53 |
- The approximate amount of time, in seconds, between health checks of an individual target. |
| 54 |
required: false |
| 55 |
type: int |
| 56 |
health_check_timeout: |
| 57 |
description: |
| 58 |
- The amount of time, in seconds, during which no response from a target means a failed health check. |
| 59 |
required: false |
| 60 |
type: int |
| 61 |
healthy_threshold_count: |
| 62 |
description: |
| 63 |
- The number of consecutive health checks successes required before considering an unhealthy target healthy. |
| 64 |
required: false |
| 65 |
type: int |
| 66 |
modify_targets: |
| 67 |
description: |
| 68 |
- Whether or not to alter existing targets in the group to match what is passed with the module |
| 69 |
required: false |
| 70 |
default: true |
| 71 |
type: bool |
| 72 |
name: |
| 73 |
description: |
| 74 |
- The name of the target group. |
| 75 |
required: true |
| 76 |
type: str |
| 77 |
port: |
| 78 |
description: |
| 79 |
- The port on which the targets receive traffic. This port is used unless you specify a port override when registering the target. |
| 80 |
- Required when I(state) is C(present) and I(target_type) is C(instance), C(ip), or C(alb). |
| 81 |
required: false |
| 82 |
type: int |
| 83 |
protocol: |
| 84 |
description: |
| 85 |
- The protocol to use for routing traffic to the targets. |
| 86 |
- Required when I(state) is C(present) and I(target_type) is C(instance), C(ip), or C(alb). |
| 87 |
required: false |
| 88 |
choices: [ 'http', 'https', 'tcp', 'tls', 'udp', 'tcp_udp', 'HTTP', 'HTTPS', 'TCP', 'TLS', 'UDP', 'TCP_UDP'] |
| 89 |
type: str |
| 90 |
protocol_version: |
| 91 |
description: |
| 92 |
- Specifies protocol version. |
| 93 |
- The protocol_version parameter is immutable and cannot be changed when updating an elb_target_group. |
| 94 |
required: false |
| 95 |
choices: ['GRPC', 'HTTP1', 'HTTP2'] |
| 96 |
type: str |
| 97 |
version_added: 5.1.0 |
| 98 |
state: |
| 99 |
description: |
| 100 |
- Create or destroy the target group. |
| 101 |
required: true |
| 102 |
choices: [ 'present', 'absent' ] |
| 103 |
type: str |
| 104 |
stickiness_enabled: |
| 105 |
description: |
| 106 |
- Indicates whether sticky sessions are enabled. |
| 107 |
type: bool |
| 108 |
stickiness_lb_cookie_duration: |
| 109 |
description: |
| 110 |
- The time period, in seconds, during which requests from a client should be routed to the same target. After this time period expires, the load |
| 111 |
balancer-generated cookie is considered stale. The range is 1 second to 1 week (604800 seconds). |
| 112 |
type: int |
| 113 |
stickiness_app_cookie_duration: |
| 114 |
description: |
| 115 |
- The time period, in seconds, during which requests from a client |
| 116 |
should be routed to the same target. After this time period expires, |
| 117 |
the application-generated cookie is considered stale. The range is 1 second to 1 week (604800 seconds). |
| 118 |
type: int |
| 119 |
version_added: 1.5.0 |
| 120 |
stickiness_app_cookie_name: |
| 121 |
description: |
| 122 |
- The name of the application cookie. Required if I(stickiness_type=app_cookie). |
| 123 |
type: str |
| 124 |
version_added: 1.5.0 |
| 125 |
stickiness_type: |
| 126 |
description: |
| 127 |
- The type of sticky sessions. |
| 128 |
- Valid values are C(lb_cookie), C(app_cookie) or C(source_ip). |
| 129 |
- If not set AWS will default to C(lb_cookie) for Application Load Balancers or C(source_ip) for Network Load Balancers. |
| 130 |
type: str |
| 131 |
load_balancing_algorithm_type: |
| 132 |
description: |
| 133 |
- The type of load balancing algorithm to use. |
| 134 |
- Changing the load balancing algorithm is only supported when used with Application Load Balancers (ALB). |
| 135 |
- If not set AWS will default to C(round_robin). |
| 136 |
choices: ['round_robin', 'least_outstanding_requests'] |
| 137 |
type: str |
| 138 |
version_added: 3.2.0 |
| 139 |
successful_response_codes: |
| 140 |
description: |
| 141 |
- The HTTP codes to use when checking for a successful response from a target. |
| 142 |
- Accepts multiple values (for example, "200,202") or a range of values (for example, "200-299"). |
| 143 |
- Requires the I(health_check_protocol) parameter to be set. |
| 144 |
required: false |
| 145 |
type: str |
| 146 |
target_type: |
| 147 |
description: |
| 148 |
- The type of target that you must specify when registering targets with this target group. The possible values are |
| 149 |
C(instance) (targets are specified by instance ID), C(ip) (targets are specified by IP address), C(lambda) (target is specified by ARN), |
| 150 |
or C(alb) (target is specified by ARN). |
| 151 |
Note that you can't specify targets for a target group using more than one type. Target types lambda and alb only accept one target. When more than |
| 152 |
one target is specified, only the first one is used. All additional targets are ignored. |
| 153 |
If the target type is ip, specify IP addresses from the subnets of the virtual private cloud (VPC) for the target |
| 154 |
group, the RFC 1918 range (10.0.0.0/8, 172.16.0.0/12, and 192.168.0.0/16), and the RFC 6598 range (100.64.0.0/10). |
| 155 |
You can't specify publicly routable IP addresses. |
| 156 |
- The default behavior is C(instance). |
| 157 |
required: false |
| 158 |
choices: ['instance', 'ip', 'lambda', 'alb'] |
| 159 |
type: str |
| 160 |
targets: |
| 161 |
description: |
| 162 |
- A list of targets to assign to the target group. This parameter defaults to an empty list. Unless you set the 'modify_targets' parameter then |
| 163 |
all existing targets will be removed from the group. The list should be an Id and a Port parameter. See the Examples for detail. |
| 164 |
required: false |
| 165 |
type: list |
| 166 |
elements: dict |
| 167 |
unhealthy_threshold_count: |
| 168 |
description: |
| 169 |
- The number of consecutive health check failures required before considering a target unhealthy. |
| 170 |
required: false |
| 171 |
type: int |
| 172 |
vpc_id: |
| 173 |
description: |
| 174 |
- The identifier of the virtual private cloud (VPC). |
| 175 |
- Required when I(state) is C(present) and I(target_type) is C(instance), C(ip), or C(alb). |
| 176 |
required: false |
| 177 |
type: str |
| 178 |
preserve_client_ip_enabled: |
| 179 |
description: |
| 180 |
- Indicates whether client IP preservation is enabled. |
| 181 |
- The default is disabled if the target group type is C(ip) address and the target group protocol is C(tcp) or C(tls). |
| 182 |
Otherwise, the default is enabled. Client IP preservation cannot be disabled for C(udp) and C(tcp_udp) target groups. |
| 183 |
- I(preserve_client_ip_enabled) is supported only by Network Load Balancers. |
| 184 |
type: bool |
| 185 |
required: false |
| 186 |
version_added: 2.1.0 |
| 187 |
proxy_protocol_v2_enabled: |
| 188 |
description: |
| 189 |
- Indicates whether Proxy Protocol version 2 is enabled. |
| 190 |
- The value is C(true) or C(false). |
| 191 |
- I(proxy_protocol_v2_enabled) is supported only by Network Load Balancers. |
| 192 |
type: bool |
| 193 |
required: false |
| 194 |
version_added: 2.1.0 |
| 195 |
wait: |
| 196 |
description: |
| 197 |
- Whether or not to wait for the target group. |
| 198 |
type: bool |
| 199 |
default: false |
| 200 |
wait_timeout: |
| 201 |
description: |
| 202 |
- The time to wait for the target group. |
| 203 |
default: 200 |
| 204 |
type: int |
| 205 |
|
| 206 |
notes: |
| 207 |
- Once a target group has been created, only its health check can then be modified using subsequent calls |
| 208 |
|
| 209 |
extends_documentation_fragment: |
| 210 |
- amazon.aws.common.modules |
| 211 |
- amazon.aws.region.modules |
| 212 |
- amazon.aws.tags |
| 213 |
- amazon.aws.boto3 |
| 214 |
""" |
| 215 |
|
| 216 |
EXAMPLES = r""" |
| 217 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 218 |
|
| 219 |
- name: Create a target group with a default health check |
| 220 |
community.aws.elb_target_group: |
| 221 |
name: mytargetgroup |
| 222 |
protocol: http |
| 223 |
port: 80 |
| 224 |
vpc_id: vpc-01234567 |
| 225 |
state: present |
| 226 |
|
| 227 |
- name: Create a target group with protocol_version 'GRPC' |
| 228 |
community.aws.elb_target_group: |
| 229 |
name: mytargetgroup |
| 230 |
protocol: http |
| 231 |
port: 80 |
| 232 |
vpc_id: vpc-01234567 |
| 233 |
protocol_version: GRPC |
| 234 |
state: present |
| 235 |
|
| 236 |
- name: Modify the target group with a custom health check |
| 237 |
community.aws.elb_target_group: |
| 238 |
name: mytargetgroup |
| 239 |
protocol: http |
| 240 |
port: 80 |
| 241 |
vpc_id: vpc-01234567 |
| 242 |
health_check_protocol: http |
| 243 |
health_check_path: /health_check |
| 244 |
health_check_port: 80 |
| 245 |
successful_response_codes: 200 |
| 246 |
health_check_interval: 15 |
| 247 |
health_check_timeout: 3 |
| 248 |
healthy_threshold_count: 4 |
| 249 |
unhealthy_threshold_count: 3 |
| 250 |
state: present |
| 251 |
|
| 252 |
- name: Delete a target group |
| 253 |
community.aws.elb_target_group: |
| 254 |
name: mytargetgroup |
| 255 |
state: absent |
| 256 |
|
| 257 |
- name: Create a target group with instance targets |
| 258 |
community.aws.elb_target_group: |
| 259 |
name: mytargetgroup |
| 260 |
protocol: http |
| 261 |
port: 81 |
| 262 |
vpc_id: vpc-01234567 |
| 263 |
health_check_protocol: http |
| 264 |
health_check_path: / |
| 265 |
successful_response_codes: "200,250-260" |
| 266 |
targets: |
| 267 |
- Id: i-01234567 |
| 268 |
Port: 80 |
| 269 |
- Id: i-98765432 |
| 270 |
Port: 80 |
| 271 |
state: present |
| 272 |
wait_timeout: 200 |
| 273 |
wait: true |
| 274 |
|
| 275 |
- name: Create a target group with IP address targets |
| 276 |
community.aws.elb_target_group: |
| 277 |
name: mytargetgroup |
| 278 |
protocol: http |
| 279 |
port: 81 |
| 280 |
vpc_id: vpc-01234567 |
| 281 |
health_check_protocol: http |
| 282 |
health_check_path: / |
| 283 |
successful_response_codes: "200,250-260" |
| 284 |
target_type: ip |
| 285 |
targets: |
| 286 |
- Id: 10.0.0.10 |
| 287 |
Port: 80 |
| 288 |
AvailabilityZone: all |
| 289 |
- Id: 10.0.0.20 |
| 290 |
Port: 80 |
| 291 |
state: present |
| 292 |
wait_timeout: 200 |
| 293 |
wait: true |
| 294 |
|
| 295 |
# Using lambda as targets require that the target group |
| 296 |
# itself is allow to invoke the lambda function. |
| 297 |
# therefore you need first to create an empty target group |
| 298 |
# to receive its arn, second, allow the target group |
| 299 |
# to invoke the lambda function and third, add the target |
| 300 |
# to the target group |
| 301 |
- name: first, create empty target group |
| 302 |
community.aws.elb_target_group: |
| 303 |
name: my-lambda-targetgroup |
| 304 |
target_type: lambda |
| 305 |
state: present |
| 306 |
modify_targets: false |
| 307 |
register: out |
| 308 |
|
| 309 |
- name: second, allow invoke of the lambda |
| 310 |
community.aws.lambda_policy: |
| 311 |
state: "{{ state | default('present') }}" |
| 312 |
function_name: my-lambda-function |
| 313 |
statement_id: someID |
| 314 |
action: lambda:InvokeFunction |
| 315 |
principal: elasticloadbalancing.amazonaws.com |
| 316 |
source_arn: "{{ out.target_group_arn }}" |
| 317 |
|
| 318 |
- name: third, add target |
| 319 |
community.aws.elb_target_group: |
| 320 |
name: my-lambda-targetgroup |
| 321 |
target_type: lambda |
| 322 |
state: present |
| 323 |
targets: |
| 324 |
- Id: arn:aws:lambda:eu-central-1:123456789012:function:my-lambda-function |
| 325 |
""" |
| 326 |
|
| 327 |
RETURN = r""" |
| 328 |
deregistration_delay_timeout_seconds: |
| 329 |
description: The amount time for Elastic Load Balancing to wait before changing the state of a deregistering target from draining to unused. |
| 330 |
returned: when state present |
| 331 |
type: int |
| 332 |
sample: 300 |
| 333 |
deregistration_connection_termination: |
| 334 |
description: Indicates whether the load balancer terminates connections at the end of the deregistration timeout. |
| 335 |
returned: when state present |
| 336 |
type: bool |
| 337 |
sample: True |
| 338 |
health_check_interval_seconds: |
| 339 |
description: The approximate amount of time, in seconds, between health checks of an individual target. |
| 340 |
returned: when state present |
| 341 |
type: int |
| 342 |
sample: 30 |
| 343 |
health_check_path: |
| 344 |
description: The destination for the health check request. |
| 345 |
returned: when state present |
| 346 |
type: str |
| 347 |
sample: /index.html |
| 348 |
health_check_port: |
| 349 |
description: The port to use to connect with the target. |
| 350 |
returned: when state present |
| 351 |
type: str |
| 352 |
sample: traffic-port |
| 353 |
health_check_protocol: |
| 354 |
description: The protocol to use to connect with the target. |
| 355 |
returned: when state present |
| 356 |
type: str |
| 357 |
sample: HTTP |
| 358 |
health_check_timeout_seconds: |
| 359 |
description: The amount of time, in seconds, during which no response means a failed health check. |
| 360 |
returned: when state present |
| 361 |
type: int |
| 362 |
sample: 5 |
| 363 |
healthy_threshold_count: |
| 364 |
description: The number of consecutive health checks successes required before considering an unhealthy target healthy. |
| 365 |
returned: when state present |
| 366 |
type: int |
| 367 |
sample: 5 |
| 368 |
load_balancer_arns: |
| 369 |
description: The Amazon Resource Names (ARN) of the load balancers that route traffic to this target group. |
| 370 |
returned: when state present |
| 371 |
type: list |
| 372 |
sample: [] |
| 373 |
matcher: |
| 374 |
description: The HTTP codes to use when checking for a successful response from a target. |
| 375 |
returned: when state present |
| 376 |
type: dict |
| 377 |
sample: { |
| 378 |
"http_code": "200" |
| 379 |
} |
| 380 |
port: |
| 381 |
description: The port on which the targets are listening. |
| 382 |
returned: when state present |
| 383 |
type: int |
| 384 |
sample: 80 |
| 385 |
protocol: |
| 386 |
description: The protocol to use for routing traffic to the targets. |
| 387 |
returned: when state present |
| 388 |
type: str |
| 389 |
sample: HTTP |
| 390 |
stickiness_enabled: |
| 391 |
description: Indicates whether sticky sessions are enabled. |
| 392 |
returned: when state present |
| 393 |
type: bool |
| 394 |
sample: true |
| 395 |
stickiness_lb_cookie_duration_seconds: |
| 396 |
description: The time period, in seconds, during which requests from a client should be routed to the same target. |
| 397 |
returned: when state present |
| 398 |
type: int |
| 399 |
sample: 86400 |
| 400 |
stickiness_type: |
| 401 |
description: The type of sticky sessions. |
| 402 |
returned: when state present |
| 403 |
type: str |
| 404 |
sample: lb_cookie |
| 405 |
load_balancing_algorithm_type: |
| 406 |
description: The type load balancing algorithm used. |
| 407 |
returned: when state present |
| 408 |
type: str |
| 409 |
version_added: 3.2.0 |
| 410 |
sample: least_outstanding_requests |
| 411 |
tags: |
| 412 |
description: The tags attached to the target group. |
| 413 |
returned: when state present |
| 414 |
type: dict |
| 415 |
sample: "{ |
| 416 |
'Tag': 'Example' |
| 417 |
}" |
| 418 |
target_group_arn: |
| 419 |
description: The Amazon Resource Name (ARN) of the target group. |
| 420 |
returned: when state present |
| 421 |
type: str |
| 422 |
sample: "arn:aws:elasticloadbalancing:ap-southeast-2:123456789012:targetgroup/mytargetgroup/aabbccddee0044332211" |
| 423 |
target_group_name: |
| 424 |
description: The name of the target group. |
| 425 |
returned: when state present |
| 426 |
type: str |
| 427 |
sample: mytargetgroup |
| 428 |
unhealthy_threshold_count: |
| 429 |
description: The number of consecutive health check failures required before considering the target unhealthy. |
| 430 |
returned: when state present |
| 431 |
type: int |
| 432 |
sample: 2 |
| 433 |
vpc_id: |
| 434 |
description: The ID of the VPC for the targets. |
| 435 |
returned: when state present |
| 436 |
type: str |
| 437 |
sample: vpc-0123456 |
| 438 |
""" |
| 439 |
|
| 440 |
import time |
| 441 |
|
| 442 |
try: |
| 443 |
import botocore |
| 444 |
except ImportError: |
| 445 |
pass # caught by AnsibleAWSModule |
| 446 |
|
| 447 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 448 |
|
| 449 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code |
| 450 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 451 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import ansible_dict_to_boto3_tag_list |
| 452 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 453 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import compare_aws_tags |
| 454 |
|
| 455 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 456 |
|
| 457 |
|
| 458 |
def get_tg_attributes(connection, module, tg_arn): |
| 459 |
try: |
| 460 |
_attributes = connection.describe_target_group_attributes(TargetGroupArn=tg_arn, aws_retry=True) |
| 461 |
tg_attributes = boto3_tag_list_to_ansible_dict(_attributes["Attributes"]) |
| 462 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 463 |
module.fail_json_aws(e, msg="Couldn't get target group attributes") |
| 464 |
|
| 465 |
# Replace '.' with '_' in attribute key names to make it more Ansible friendly |
| 466 |
return dict((k.replace(".", "_"), v) for k, v in tg_attributes.items()) |
| 467 |
|
| 468 |
|
| 469 |
def get_target_group_tags(connection, module, target_group_arn): |
| 470 |
try: |
| 471 |
_tags = connection.describe_tags(ResourceArns=[target_group_arn], aws_retry=True) |
| 472 |
return _tags["TagDescriptions"][0]["Tags"] |
| 473 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 474 |
module.fail_json_aws(e, msg="Couldn't get target group tags") |
| 475 |
|
| 476 |
|
| 477 |
def get_target_group(connection, module, retry_missing=False): |
| 478 |
extra_codes = ["TargetGroupNotFound"] if retry_missing else [] |
| 479 |
try: |
| 480 |
target_group_paginator = connection.get_paginator("describe_target_groups").paginate( |
| 481 |
Names=[module.params.get("name")] |
| 482 |
) |
| 483 |
jittered_retry = AWSRetry.jittered_backoff(retries=10, catch_extra_error_codes=extra_codes) |
| 484 |
result = jittered_retry(target_group_paginator.build_full_result)() |
| 485 |
except is_boto3_error_code("TargetGroupNotFound"): |
| 486 |
return None |
| 487 |
except ( |
| 488 |
botocore.exceptions.ClientError, |
| 489 |
botocore.exceptions.BotoCoreError, |
| 490 |
) as e: # pylint: disable=duplicate-except |
| 491 |
module.fail_json_aws(e, msg="Couldn't get target group") |
| 492 |
|
| 493 |
return result["TargetGroups"][0] |
| 494 |
|
| 495 |
|
| 496 |
def wait_for_status(connection, module, target_group_arn, targets, status): |
| 497 |
polling_increment_secs = 5 |
| 498 |
max_retries = module.params.get("wait_timeout") // polling_increment_secs |
| 499 |
status_achieved = False |
| 500 |
|
| 501 |
for x in range(0, max_retries): |
| 502 |
try: |
| 503 |
response = connection.describe_target_health( |
| 504 |
TargetGroupArn=target_group_arn, Targets=targets, aws_retry=True |
| 505 |
) |
| 506 |
if response["TargetHealthDescriptions"][0]["TargetHealth"]["State"] == status: |
| 507 |
status_achieved = True |
| 508 |
break |
| 509 |
time.sleep(polling_increment_secs) |
| 510 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 511 |
module.fail_json_aws(e, msg="Couldn't describe target health") |
| 512 |
|
| 513 |
result = response |
| 514 |
return status_achieved, result |
| 515 |
|
| 516 |
|
| 517 |
def create_or_update_attributes(connection, module, target_group, new_target_group): |
| 518 |
changed = False |
| 519 |
target_type = module.params.get("target_type") |
| 520 |
deregistration_delay_timeout = module.params.get("deregistration_delay_timeout") |
| 521 |
deregistration_connection_termination = module.params.get("deregistration_connection_termination") |
| 522 |
stickiness_enabled = module.params.get("stickiness_enabled") |
| 523 |
stickiness_lb_cookie_duration = module.params.get("stickiness_lb_cookie_duration") |
| 524 |
stickiness_type = module.params.get("stickiness_type") |
| 525 |
stickiness_app_cookie_duration = module.params.get("stickiness_app_cookie_duration") |
| 526 |
stickiness_app_cookie_name = module.params.get("stickiness_app_cookie_name") |
| 527 |
preserve_client_ip_enabled = module.params.get("preserve_client_ip_enabled") |
| 528 |
proxy_protocol_v2_enabled = module.params.get("proxy_protocol_v2_enabled") |
| 529 |
load_balancing_algorithm_type = module.params.get("load_balancing_algorithm_type") |
| 530 |
|
| 531 |
# Now set target group attributes |
| 532 |
update_attributes = [] |
| 533 |
|
| 534 |
# Get current attributes |
| 535 |
current_tg_attributes = get_tg_attributes(connection, module, target_group["TargetGroupArn"]) |
| 536 |
|
| 537 |
if deregistration_delay_timeout is not None: |
| 538 |
if str(deregistration_delay_timeout) != current_tg_attributes["deregistration_delay_timeout_seconds"]: |
| 539 |
update_attributes.append( |
| 540 |
{"Key": "deregistration_delay.timeout_seconds", "Value": str(deregistration_delay_timeout)} |
| 541 |
) |
| 542 |
if deregistration_connection_termination is not None: |
| 543 |
if ( |
| 544 |
deregistration_connection_termination |
| 545 |
and current_tg_attributes.get("deregistration_delay_connection_termination_enabled") != "true" |
| 546 |
): |
| 547 |
update_attributes.append({"Key": "deregistration_delay.connection_termination.enabled", "Value": "true"}) |
| 548 |
if stickiness_enabled is not None: |
| 549 |
if stickiness_enabled and current_tg_attributes["stickiness_enabled"] != "true": |
| 550 |
update_attributes.append({"Key": "stickiness.enabled", "Value": "true"}) |
| 551 |
if stickiness_lb_cookie_duration is not None: |
| 552 |
if str(stickiness_lb_cookie_duration) != current_tg_attributes["stickiness_lb_cookie_duration_seconds"]: |
| 553 |
update_attributes.append( |
| 554 |
{"Key": "stickiness.lb_cookie.duration_seconds", "Value": str(stickiness_lb_cookie_duration)} |
| 555 |
) |
| 556 |
if stickiness_type is not None: |
| 557 |
if stickiness_type != current_tg_attributes.get("stickiness_type"): |
| 558 |
update_attributes.append({"Key": "stickiness.type", "Value": stickiness_type}) |
| 559 |
if stickiness_app_cookie_name is not None: |
| 560 |
if stickiness_app_cookie_name != current_tg_attributes.get("stickiness_app_cookie_name"): |
| 561 |
update_attributes.append( |
| 562 |
{"Key": "stickiness.app_cookie.cookie_name", "Value": str(stickiness_app_cookie_name)} |
| 563 |
) |
| 564 |
if stickiness_app_cookie_duration is not None: |
| 565 |
if str(stickiness_app_cookie_duration) != current_tg_attributes["stickiness_app_cookie_duration_seconds"]: |
| 566 |
update_attributes.append( |
| 567 |
{"Key": "stickiness.app_cookie.duration_seconds", "Value": str(stickiness_app_cookie_duration)} |
| 568 |
) |
| 569 |
if preserve_client_ip_enabled is not None: |
| 570 |
if target_type not in ("udp", "tcp_udp"): |
| 571 |
if str(preserve_client_ip_enabled).lower() != current_tg_attributes.get("preserve_client_ip_enabled"): |
| 572 |
update_attributes.append( |
| 573 |
{"Key": "preserve_client_ip.enabled", "Value": str(preserve_client_ip_enabled).lower()} |
| 574 |
) |
| 575 |
if proxy_protocol_v2_enabled is not None: |
| 576 |
if str(proxy_protocol_v2_enabled).lower() != current_tg_attributes.get("proxy_protocol_v2_enabled"): |
| 577 |
update_attributes.append( |
| 578 |
{"Key": "proxy_protocol_v2.enabled", "Value": str(proxy_protocol_v2_enabled).lower()} |
| 579 |
) |
| 580 |
if load_balancing_algorithm_type is not None: |
| 581 |
if str(load_balancing_algorithm_type) != current_tg_attributes["load_balancing_algorithm_type"]: |
| 582 |
update_attributes.append( |
| 583 |
{"Key": "load_balancing.algorithm.type", "Value": str(load_balancing_algorithm_type)} |
| 584 |
) |
| 585 |
|
| 586 |
if update_attributes: |
| 587 |
try: |
| 588 |
connection.modify_target_group_attributes( |
| 589 |
TargetGroupArn=target_group["TargetGroupArn"], Attributes=update_attributes, aws_retry=True |
| 590 |
) |
| 591 |
changed = True |
| 592 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 593 |
# Something went wrong setting attributes. If this target group was created during this task, delete it to leave a consistent state |
| 594 |
if new_target_group: |
| 595 |
connection.delete_target_group(TargetGroupArn=target_group["TargetGroupArn"], aws_retry=True) |
| 596 |
module.fail_json_aws(e, msg="Couldn't delete target group") |
| 597 |
|
| 598 |
return changed |
| 599 |
|
| 600 |
|
| 601 |
def create_or_update_target_group(connection, module): |
| 602 |
changed = False |
| 603 |
new_target_group = False |
| 604 |
params = dict() |
| 605 |
target_type = module.params.get("target_type") |
| 606 |
params["Name"] = module.params.get("name") |
| 607 |
params["TargetType"] = target_type |
| 608 |
if target_type != "lambda": |
| 609 |
params["Protocol"] = module.params.get("protocol").upper() |
| 610 |
if module.params.get("protocol_version") is not None: |
| 611 |
params["ProtocolVersion"] = module.params.get("protocol_version") |
| 612 |
params["Port"] = module.params.get("port") |
| 613 |
params["VpcId"] = module.params.get("vpc_id") |
| 614 |
tags = module.params.get("tags") |
| 615 |
purge_tags = module.params.get("purge_tags") |
| 616 |
|
| 617 |
health_option_keys = [ |
| 618 |
"health_check_path", |
| 619 |
"health_check_protocol", |
| 620 |
"health_check_interval", |
| 621 |
"health_check_timeout", |
| 622 |
"healthy_threshold_count", |
| 623 |
"unhealthy_threshold_count", |
| 624 |
"successful_response_codes", |
| 625 |
] |
| 626 |
health_options = any(module.params[health_option_key] is not None for health_option_key in health_option_keys) |
| 627 |
|
| 628 |
# Set health check if anything set |
| 629 |
if health_options: |
| 630 |
if module.params.get("health_check_protocol") is not None: |
| 631 |
params["HealthCheckProtocol"] = module.params.get("health_check_protocol").upper() |
| 632 |
|
| 633 |
if module.params.get("health_check_port") is not None: |
| 634 |
params["HealthCheckPort"] = module.params.get("health_check_port") |
| 635 |
|
| 636 |
if module.params.get("health_check_interval") is not None: |
| 637 |
params["HealthCheckIntervalSeconds"] = module.params.get("health_check_interval") |
| 638 |
|
| 639 |
if module.params.get("health_check_timeout") is not None: |
| 640 |
params["HealthCheckTimeoutSeconds"] = module.params.get("health_check_timeout") |
| 641 |
|
| 642 |
if module.params.get("healthy_threshold_count") is not None: |
| 643 |
params["HealthyThresholdCount"] = module.params.get("healthy_threshold_count") |
| 644 |
|
| 645 |
if module.params.get("unhealthy_threshold_count") is not None: |
| 646 |
params["UnhealthyThresholdCount"] = module.params.get("unhealthy_threshold_count") |
| 647 |
|
| 648 |
# Only need to check response code and path for http(s) health checks |
| 649 |
protocol = module.params.get("health_check_protocol") |
| 650 |
if protocol is not None and protocol.upper() in ["HTTP", "HTTPS"]: |
| 651 |
if module.params.get("health_check_path") is not None: |
| 652 |
params["HealthCheckPath"] = module.params.get("health_check_path") |
| 653 |
|
| 654 |
if module.params.get("successful_response_codes") is not None: |
| 655 |
params["Matcher"] = {} |
| 656 |
code_key = "HttpCode" |
| 657 |
protocol_version = module.params.get("protocol_version") |
| 658 |
if protocol_version is not None and protocol_version.upper() == "GRPC": |
| 659 |
code_key = "GrpcCode" |
| 660 |
params["Matcher"][code_key] = module.params.get("successful_response_codes") |
| 661 |
|
| 662 |
# Get target group |
| 663 |
target_group = get_target_group(connection, module) |
| 664 |
|
| 665 |
if target_group: |
| 666 |
diffs = [param for param in ("Port", "Protocol", "VpcId") if target_group.get(param) != params.get(param)] |
| 667 |
if diffs: |
| 668 |
module.fail_json(msg=f"Cannot modify {', '.join(diffs)} parameter(s) for a target group") |
| 669 |
# Target group exists so check health check parameters match what has been passed |
| 670 |
health_check_params = dict() |
| 671 |
|
| 672 |
# Modify health check if anything set |
| 673 |
if health_options: |
| 674 |
# Health check protocol |
| 675 |
if "HealthCheckProtocol" in params and target_group["HealthCheckProtocol"] != params["HealthCheckProtocol"]: |
| 676 |
health_check_params["HealthCheckProtocol"] = params["HealthCheckProtocol"] |
| 677 |
|
| 678 |
# Health check port |
| 679 |
if "HealthCheckPort" in params and target_group["HealthCheckPort"] != params["HealthCheckPort"]: |
| 680 |
health_check_params["HealthCheckPort"] = params["HealthCheckPort"] |
| 681 |
|
| 682 |
# Health check interval |
| 683 |
if ( |
| 684 |
"HealthCheckIntervalSeconds" in params |
| 685 |
and target_group["HealthCheckIntervalSeconds"] != params["HealthCheckIntervalSeconds"] |
| 686 |
): |
| 687 |
health_check_params["HealthCheckIntervalSeconds"] = params["HealthCheckIntervalSeconds"] |
| 688 |
|
| 689 |
# Health check timeout |
| 690 |
if ( |
| 691 |
"HealthCheckTimeoutSeconds" in params |
| 692 |
and target_group["HealthCheckTimeoutSeconds"] != params["HealthCheckTimeoutSeconds"] |
| 693 |
): |
| 694 |
health_check_params["HealthCheckTimeoutSeconds"] = params["HealthCheckTimeoutSeconds"] |
| 695 |
|
| 696 |
# Healthy threshold |
| 697 |
if ( |
| 698 |
"HealthyThresholdCount" in params |
| 699 |
and target_group["HealthyThresholdCount"] != params["HealthyThresholdCount"] |
| 700 |
): |
| 701 |
health_check_params["HealthyThresholdCount"] = params["HealthyThresholdCount"] |
| 702 |
|
| 703 |
# Unhealthy threshold |
| 704 |
if ( |
| 705 |
"UnhealthyThresholdCount" in params |
| 706 |
and target_group["UnhealthyThresholdCount"] != params["UnhealthyThresholdCount"] |
| 707 |
): |
| 708 |
health_check_params["UnhealthyThresholdCount"] = params["UnhealthyThresholdCount"] |
| 709 |
|
| 710 |
# Only need to check response code and path for http(s) health checks |
| 711 |
if target_group["HealthCheckProtocol"] in ["HTTP", "HTTPS"]: |
| 712 |
# Health check path |
| 713 |
if "HealthCheckPath" in params and target_group["HealthCheckPath"] != params["HealthCheckPath"]: |
| 714 |
health_check_params["HealthCheckPath"] = params["HealthCheckPath"] |
| 715 |
|
| 716 |
# Matcher (successful response codes) |
| 717 |
# TODO: required and here? |
| 718 |
if "Matcher" in params: |
| 719 |
code_key = "HttpCode" |
| 720 |
if target_group.get("ProtocolVersion") == "GRPC": |
| 721 |
code_key = "GrpcCode" |
| 722 |
current_matcher_list = target_group["Matcher"][code_key].split(",") |
| 723 |
requested_matcher_list = params["Matcher"][code_key].split(",") |
| 724 |
if set(current_matcher_list) != set(requested_matcher_list): |
| 725 |
health_check_params["Matcher"] = {} |
| 726 |
health_check_params["Matcher"][code_key] = ",".join(requested_matcher_list) |
| 727 |
|
| 728 |
try: |
| 729 |
if health_check_params: |
| 730 |
connection.modify_target_group( |
| 731 |
TargetGroupArn=target_group["TargetGroupArn"], aws_retry=True, **health_check_params |
| 732 |
) |
| 733 |
changed = True |
| 734 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 735 |
module.fail_json_aws(e, msg="Couldn't update target group") |
| 736 |
|
| 737 |
# Do we need to modify targets? |
| 738 |
if module.params.get("modify_targets"): |
| 739 |
# get list of current target instances. I can't see anything like a describe targets in the doco so |
| 740 |
# describe_target_health seems to be the only way to get them |
| 741 |
try: |
| 742 |
current_targets = connection.describe_target_health( |
| 743 |
TargetGroupArn=target_group["TargetGroupArn"], aws_retry=True |
| 744 |
) |
| 745 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 746 |
module.fail_json_aws(e, msg="Couldn't get target group health") |
| 747 |
|
| 748 |
if module.params.get("targets"): |
| 749 |
if target_type != "lambda": |
| 750 |
params["Targets"] = module.params.get("targets") |
| 751 |
|
| 752 |
# Correct type of target ports |
| 753 |
for target in params["Targets"]: |
| 754 |
target["Port"] = int(target.get("Port", module.params.get("port"))) |
| 755 |
|
| 756 |
current_instance_ids = [] |
| 757 |
|
| 758 |
for instance in current_targets["TargetHealthDescriptions"]: |
| 759 |
current_instance_ids.append(instance["Target"]["Id"]) |
| 760 |
|
| 761 |
new_instance_ids = [] |
| 762 |
for instance in params["Targets"]: |
| 763 |
new_instance_ids.append(instance["Id"]) |
| 764 |
|
| 765 |
add_instances = set(new_instance_ids) - set(current_instance_ids) |
| 766 |
|
| 767 |
if add_instances: |
| 768 |
instances_to_add = [] |
| 769 |
for target in params["Targets"]: |
| 770 |
if target["Id"] in add_instances: |
| 771 |
tmp_item = {"Id": target["Id"], "Port": target["Port"]} |
| 772 |
if target.get("AvailabilityZone"): |
| 773 |
tmp_item["AvailabilityZone"] = target["AvailabilityZone"] |
| 774 |
instances_to_add.append(tmp_item) |
| 775 |
|
| 776 |
changed = True |
| 777 |
try: |
| 778 |
connection.register_targets( |
| 779 |
TargetGroupArn=target_group["TargetGroupArn"], Targets=instances_to_add, aws_retry=True |
| 780 |
) |
| 781 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 782 |
module.fail_json_aws(e, msg="Couldn't register targets") |
| 783 |
|
| 784 |
if module.params.get("wait"): |
| 785 |
status_achieved, registered_instances = wait_for_status( |
| 786 |
connection, module, target_group["TargetGroupArn"], instances_to_add, "healthy" |
| 787 |
) |
| 788 |
if not status_achieved: |
| 789 |
module.fail_json( |
| 790 |
msg="Error waiting for target registration to be healthy - please check the AWS console" |
| 791 |
) |
| 792 |
|
| 793 |
remove_instances = set(current_instance_ids) - set(new_instance_ids) |
| 794 |
|
| 795 |
if remove_instances: |
| 796 |
instances_to_remove = [] |
| 797 |
for target in current_targets["TargetHealthDescriptions"]: |
| 798 |
if target["Target"]["Id"] in remove_instances: |
| 799 |
instances_to_remove.append( |
| 800 |
{"Id": target["Target"]["Id"], "Port": target["Target"]["Port"]} |
| 801 |
) |
| 802 |
|
| 803 |
changed = True |
| 804 |
try: |
| 805 |
connection.deregister_targets( |
| 806 |
TargetGroupArn=target_group["TargetGroupArn"], |
| 807 |
Targets=instances_to_remove, |
| 808 |
aws_retry=True, |
| 809 |
) |
| 810 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 811 |
module.fail_json_aws(e, msg="Couldn't remove targets") |
| 812 |
|
| 813 |
if module.params.get("wait"): |
| 814 |
status_achieved, registered_instances = wait_for_status( |
| 815 |
connection, module, target_group["TargetGroupArn"], instances_to_remove, "unused" |
| 816 |
) |
| 817 |
if not status_achieved: |
| 818 |
module.fail_json( |
| 819 |
msg="Error waiting for target deregistration - please check the AWS console" |
| 820 |
) |
| 821 |
|
| 822 |
# register lambda target |
| 823 |
else: |
| 824 |
try: |
| 825 |
changed = False |
| 826 |
target = module.params.get("targets")[0] |
| 827 |
if len(current_targets["TargetHealthDescriptions"]) == 0: |
| 828 |
changed = True |
| 829 |
else: |
| 830 |
for item in current_targets["TargetHealthDescriptions"]: |
| 831 |
if target["Id"] != item["Target"]["Id"]: |
| 832 |
changed = True |
| 833 |
break # only one target is possible with lambda |
| 834 |
|
| 835 |
if changed: |
| 836 |
if target.get("Id"): |
| 837 |
connection.register_targets( |
| 838 |
TargetGroupArn=target_group["TargetGroupArn"], |
| 839 |
Targets=[{"Id": target["Id"]}], |
| 840 |
aws_retry=True, |
| 841 |
) |
| 842 |
|
| 843 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 844 |
module.fail_json_aws(e, msg="Couldn't register targets") |
| 845 |
else: |
| 846 |
if target_type != "lambda": |
| 847 |
current_instances = current_targets["TargetHealthDescriptions"] |
| 848 |
|
| 849 |
if current_instances: |
| 850 |
instances_to_remove = [] |
| 851 |
for target in current_targets["TargetHealthDescriptions"]: |
| 852 |
instances_to_remove.append({"Id": target["Target"]["Id"], "Port": target["Target"]["Port"]}) |
| 853 |
|
| 854 |
changed = True |
| 855 |
try: |
| 856 |
connection.deregister_targets( |
| 857 |
TargetGroupArn=target_group["TargetGroupArn"], |
| 858 |
Targets=instances_to_remove, |
| 859 |
aws_retry=True, |
| 860 |
) |
| 861 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 862 |
module.fail_json_aws(e, msg="Couldn't remove targets") |
| 863 |
|
| 864 |
if module.params.get("wait"): |
| 865 |
status_achieved, registered_instances = wait_for_status( |
| 866 |
connection, module, target_group["TargetGroupArn"], instances_to_remove, "unused" |
| 867 |
) |
| 868 |
if not status_achieved: |
| 869 |
module.fail_json( |
| 870 |
msg="Error waiting for target deregistration - please check the AWS console" |
| 871 |
) |
| 872 |
|
| 873 |
# remove lambda targets |
| 874 |
else: |
| 875 |
changed = False |
| 876 |
if current_targets["TargetHealthDescriptions"]: |
| 877 |
changed = True |
| 878 |
# only one target is possible with lambda |
| 879 |
target_to_remove = current_targets["TargetHealthDescriptions"][0]["Target"]["Id"] |
| 880 |
if changed: |
| 881 |
connection.deregister_targets( |
| 882 |
TargetGroupArn=target_group["TargetGroupArn"], |
| 883 |
Targets=[{"Id": target_to_remove}], |
| 884 |
aws_retry=True, |
| 885 |
) |
| 886 |
else: |
| 887 |
try: |
| 888 |
connection.create_target_group(aws_retry=True, **params) |
| 889 |
changed = True |
| 890 |
new_target_group = True |
| 891 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 892 |
module.fail_json_aws(e, msg="Couldn't create target group") |
| 893 |
|
| 894 |
target_group = get_target_group(connection, module, retry_missing=True) |
| 895 |
|
| 896 |
if module.params.get("targets"): |
| 897 |
if target_type != "lambda": |
| 898 |
params["Targets"] = module.params.get("targets") |
| 899 |
try: |
| 900 |
connection.register_targets( |
| 901 |
TargetGroupArn=target_group["TargetGroupArn"], Targets=params["Targets"], aws_retry=True |
| 902 |
) |
| 903 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 904 |
module.fail_json_aws(e, msg="Couldn't register targets") |
| 905 |
|
| 906 |
if module.params.get("wait"): |
| 907 |
status_achieved, registered_instances = wait_for_status( |
| 908 |
connection, module, target_group["TargetGroupArn"], params["Targets"], "healthy" |
| 909 |
) |
| 910 |
if not status_achieved: |
| 911 |
module.fail_json( |
| 912 |
msg="Error waiting for target registration to be healthy - please check the AWS console" |
| 913 |
) |
| 914 |
|
| 915 |
else: |
| 916 |
try: |
| 917 |
target = module.params.get("targets")[0] |
| 918 |
connection.register_targets( |
| 919 |
TargetGroupArn=target_group["TargetGroupArn"], Targets=[{"Id": target["Id"]}], aws_retry=True |
| 920 |
) |
| 921 |
changed = True |
| 922 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 923 |
module.fail_json_aws(e, msg="Couldn't register targets") |
| 924 |
|
| 925 |
attributes_update = create_or_update_attributes(connection, module, target_group, new_target_group) |
| 926 |
|
| 927 |
if attributes_update: |
| 928 |
changed = True |
| 929 |
|
| 930 |
# Tags - only need to play with tags if tags parameter has been set to something |
| 931 |
if tags is not None: |
| 932 |
# Get tags |
| 933 |
current_tags = get_target_group_tags(connection, module, target_group["TargetGroupArn"]) |
| 934 |
|
| 935 |
# Delete necessary tags |
| 936 |
tags_need_modify, tags_to_delete = compare_aws_tags( |
| 937 |
boto3_tag_list_to_ansible_dict(current_tags), tags, purge_tags |
| 938 |
) |
| 939 |
if tags_to_delete: |
| 940 |
try: |
| 941 |
connection.remove_tags( |
| 942 |
ResourceArns=[target_group["TargetGroupArn"]], TagKeys=tags_to_delete, aws_retry=True |
| 943 |
) |
| 944 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 945 |
module.fail_json_aws(e, msg="Couldn't delete tags from target group") |
| 946 |
changed = True |
| 947 |
|
| 948 |
# Add/update tags |
| 949 |
if tags_need_modify: |
| 950 |
try: |
| 951 |
connection.add_tags( |
| 952 |
ResourceArns=[target_group["TargetGroupArn"]], |
| 953 |
Tags=ansible_dict_to_boto3_tag_list(tags_need_modify), |
| 954 |
aws_retry=True, |
| 955 |
) |
| 956 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 957 |
module.fail_json_aws(e, msg="Couldn't add tags to target group") |
| 958 |
changed = True |
| 959 |
|
| 960 |
# Get the target group again |
| 961 |
target_group = get_target_group(connection, module) |
| 962 |
|
| 963 |
# Get the target group attributes again |
| 964 |
target_group.update(get_tg_attributes(connection, module, target_group["TargetGroupArn"])) |
| 965 |
|
| 966 |
# Convert target_group to snake_case |
| 967 |
snaked_tg = camel_dict_to_snake_dict(target_group) |
| 968 |
|
| 969 |
snaked_tg["tags"] = boto3_tag_list_to_ansible_dict( |
| 970 |
get_target_group_tags(connection, module, target_group["TargetGroupArn"]) |
| 971 |
) |
| 972 |
|
| 973 |
module.exit_json(changed=changed, **snaked_tg) |
| 974 |
|
| 975 |
|
| 976 |
def delete_target_group(connection, module): |
| 977 |
changed = False |
| 978 |
tg = get_target_group(connection, module) |
| 979 |
|
| 980 |
if tg: |
| 981 |
try: |
| 982 |
connection.delete_target_group(TargetGroupArn=tg["TargetGroupArn"], aws_retry=True) |
| 983 |
changed = True |
| 984 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 985 |
module.fail_json_aws(e, msg="Couldn't delete target group") |
| 986 |
|
| 987 |
module.exit_json(changed=changed) |
| 988 |
|
| 989 |
|
| 990 |
def main(): |
| 991 |
protocols_list = ["http", "https", "tcp", "tls", "udp", "tcp_udp", "HTTP", "HTTPS", "TCP", "TLS", "UDP", "TCP_UDP"] |
| 992 |
argument_spec = dict( |
| 993 |
deregistration_delay_timeout=dict(type="int"), |
| 994 |
deregistration_connection_termination=dict(type="bool", default=False), |
| 995 |
health_check_protocol=dict(choices=protocols_list), |
| 996 |
health_check_port=dict(), |
| 997 |
health_check_path=dict(), |
| 998 |
health_check_interval=dict(type="int"), |
| 999 |
health_check_timeout=dict(type="int"), |
| 1000 |
healthy_threshold_count=dict(type="int"), |
| 1001 |
modify_targets=dict(default=True, type="bool"), |
| 1002 |
name=dict(required=True), |
| 1003 |
port=dict(type="int"), |
| 1004 |
protocol=dict(choices=protocols_list), |
| 1005 |
protocol_version=dict(type="str", choices=["GRPC", "HTTP1", "HTTP2"]), |
| 1006 |
purge_tags=dict(default=True, type="bool"), |
| 1007 |
stickiness_enabled=dict(type="bool"), |
| 1008 |
stickiness_type=dict(), |
| 1009 |
stickiness_lb_cookie_duration=dict(type="int"), |
| 1010 |
stickiness_app_cookie_duration=dict(type="int"), |
| 1011 |
stickiness_app_cookie_name=dict(), |
| 1012 |
load_balancing_algorithm_type=dict(type="str", choices=["round_robin", "least_outstanding_requests"]), |
| 1013 |
state=dict(required=True, choices=["present", "absent"]), |
| 1014 |
successful_response_codes=dict(), |
| 1015 |
tags=dict(type="dict", aliases=["resource_tags"]), |
| 1016 |
target_type=dict(choices=["instance", "ip", "lambda", "alb"]), |
| 1017 |
targets=dict(type="list", elements="dict"), |
| 1018 |
unhealthy_threshold_count=dict(type="int"), |
| 1019 |
vpc_id=dict(), |
| 1020 |
preserve_client_ip_enabled=dict(type="bool"), |
| 1021 |
proxy_protocol_v2_enabled=dict(type="bool"), |
| 1022 |
wait_timeout=dict(type="int", default=200), |
| 1023 |
wait=dict(type="bool", default=False), |
| 1024 |
) |
| 1025 |
required_by = dict( |
| 1026 |
health_check_path=["health_check_protocol"], |
| 1027 |
successful_response_codes=["health_check_protocol"], |
| 1028 |
) |
| 1029 |
required_if = [ |
| 1030 |
["target_type", "instance", ["protocol", "port", "vpc_id"]], |
| 1031 |
["target_type", "ip", ["protocol", "port", "vpc_id"]], |
| 1032 |
["target_type", "alb", ["protocol", "port", "vpc_id"]], |
| 1033 |
] |
| 1034 |
|
| 1035 |
module = AnsibleAWSModule(argument_spec=argument_spec, required_by=required_by, required_if=required_if) |
| 1036 |
|
| 1037 |
if module.params.get("target_type") is None: |
| 1038 |
module.params["target_type"] = "instance" |
| 1039 |
|
| 1040 |
connection = module.client("elbv2", retry_decorator=AWSRetry.jittered_backoff(retries=10)) |
| 1041 |
|
| 1042 |
if module.params.get("state") == "present": |
| 1043 |
if module.params.get("protocol") in ["http", "https", "HTTP", "HTTPS"] and module.params.get( |
| 1044 |
"deregistration_connection_termination", None |
| 1045 |
): |
| 1046 |
module.fail_json( |
| 1047 |
msg="A target group with HTTP/S protocol does not support setting deregistration_connection_termination" |
| 1048 |
) |
| 1049 |
|
| 1050 |
create_or_update_target_group(connection, module) |
| 1051 |
else: |
| 1052 |
delete_target_group(connection, module) |
| 1053 |
|
| 1054 |
|
| 1055 |
if __name__ == "__main__": |
| 1056 |
main() |