| 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_application_lb_info |
| 10 |
version_added: 5.0.0 |
| 11 |
short_description: Gather information about Application Load Balancers in AWS |
| 12 |
description: |
| 13 |
- Gather information about Application Load Balancers in AWS. |
| 14 |
- This module was originally added to C(community.aws) in release 1.0.0. |
| 15 |
author: |
| 16 |
- Rob White (@wimnat) |
| 17 |
options: |
| 18 |
load_balancer_arns: |
| 19 |
description: |
| 20 |
- The Amazon Resource Names (ARN) of the load balancers. You can specify up to 20 load balancers in a single call. |
| 21 |
required: false |
| 22 |
type: list |
| 23 |
elements: str |
| 24 |
names: |
| 25 |
description: |
| 26 |
- The names of the load balancers. |
| 27 |
required: false |
| 28 |
type: list |
| 29 |
elements: str |
| 30 |
include_attributes: |
| 31 |
description: |
| 32 |
- Whether or not to include load balancer attributes in the response. |
| 33 |
required: false |
| 34 |
type: bool |
| 35 |
default: true |
| 36 |
version_added: 7.0.0 |
| 37 |
include_listeners: |
| 38 |
description: |
| 39 |
- Whether or not to include load balancer listeners in the response. |
| 40 |
required: false |
| 41 |
type: bool |
| 42 |
default: true |
| 43 |
version_added: 7.0.0 |
| 44 |
include_listener_rules: |
| 45 |
description: |
| 46 |
- Whether or not to include load balancer listener rules in the response. |
| 47 |
- Implies O(include_listeners=true) |
| 48 |
required: false |
| 49 |
type: bool |
| 50 |
default: true |
| 51 |
version_added: 7.0.0 |
| 52 |
|
| 53 |
extends_documentation_fragment: |
| 54 |
- amazon.aws.common.modules |
| 55 |
- amazon.aws.region.modules |
| 56 |
- amazon.aws.boto3 |
| 57 |
""" |
| 58 |
|
| 59 |
EXAMPLES = r""" |
| 60 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 61 |
|
| 62 |
- name: Gather information about all ALBs |
| 63 |
amazon.aws.elb_application_lb_info: |
| 64 |
|
| 65 |
# Equivalent to aws elbv2 describe-load-balancers |
| 66 |
- name: Gather minimal information about all ALBs |
| 67 |
amazon.aws.elb_application_lb_info: |
| 68 |
include_attributes: false |
| 69 |
include_listeners: false |
| 70 |
include_listener_rules: false |
| 71 |
|
| 72 |
- name: Gather information about a particular ALB given its ARN |
| 73 |
amazon.aws.elb_application_lb_info: |
| 74 |
load_balancer_arns: |
| 75 |
- "arn:aws:elasticloadbalancing:ap-southeast-2:123456789012:loadbalancer/app/my-alb/aabbccddeeff" |
| 76 |
|
| 77 |
- name: Gather information about ALBs named 'alb1' and 'alb2' |
| 78 |
amazon.aws.elb_application_lb_info: |
| 79 |
names: |
| 80 |
- alb1 |
| 81 |
- alb2 |
| 82 |
|
| 83 |
- name: Gather information about specific ALB |
| 84 |
amazon.aws.elb_application_lb_info: |
| 85 |
names: "alb-name" |
| 86 |
region: "aws-region" |
| 87 |
register: alb_info |
| 88 |
- ansible.builtin.debug: |
| 89 |
var: alb_info |
| 90 |
""" |
| 91 |
|
| 92 |
RETURN = r""" |
| 93 |
load_balancers: |
| 94 |
description: A list of load balancers. |
| 95 |
returned: always |
| 96 |
type: complex |
| 97 |
contains: |
| 98 |
access_logs_s3_bucket: |
| 99 |
description: The name of the S3 bucket for the access logs. |
| 100 |
returned: when O(include_attributes=true) |
| 101 |
type: str |
| 102 |
sample: "mys3bucket" |
| 103 |
access_logs_s3_enabled: |
| 104 |
description: Indicates whether access logs stored in Amazon S3 are enabled. |
| 105 |
returned: when O(include_attributes=true) |
| 106 |
type: str |
| 107 |
sample: "true" |
| 108 |
access_logs_s3_prefix: |
| 109 |
description: The prefix for the location in the S3 bucket. |
| 110 |
returned: when O(include_attributes=true) |
| 111 |
type: str |
| 112 |
sample: "my/logs" |
| 113 |
availability_zones: |
| 114 |
description: The Availability Zones for the load balancer. |
| 115 |
type: list |
| 116 |
elements: dict |
| 117 |
sample: [{ "load_balancer_addresses": [], "subnet_id": "subnet-aabbccddff", "zone_name": "ap-southeast-2a" }] |
| 118 |
contains: |
| 119 |
load_balancer_addresses: |
| 120 |
description: Information about static IP addresses for a load balancer. |
| 121 |
type: list |
| 122 |
elements: dict |
| 123 |
contains: |
| 124 |
ip_address: |
| 125 |
description: The static IP address. |
| 126 |
type: str |
| 127 |
allocation_id: |
| 128 |
description: The allocation ID of the Elastic IP address for an internal-facing load balancer. |
| 129 |
type: str |
| 130 |
private_ipv4_address: |
| 131 |
description: The private IPv4 address for an internal load balancer. |
| 132 |
type: str |
| 133 |
ipv6_address: |
| 134 |
description: The IPv6 address. |
| 135 |
type: str |
| 136 |
subnet_id: |
| 137 |
description: The ID of the subnet. |
| 138 |
type: str |
| 139 |
zone_name: |
| 140 |
description: The name of the Availability Zone. |
| 141 |
type: str |
| 142 |
canonical_hosted_zone_id: |
| 143 |
description: The ID of the Amazon Route 53 hosted zone associated with the load balancer. |
| 144 |
type: str |
| 145 |
sample: "ABCDEF12345678" |
| 146 |
created_time: |
| 147 |
description: The date and time the load balancer was created. |
| 148 |
type: str |
| 149 |
sample: "2015-02-12T02:14:02+00:00" |
| 150 |
client_keep_alive_seconds: |
| 151 |
description: The client keep alive value, in seconds. |
| 152 |
returned: when O(include_attributes=true) |
| 153 |
type: str |
| 154 |
sample: "3600" |
| 155 |
connection_logs_s3_bucket: |
| 156 |
description: The name of the S3 bucket for the connection logs. |
| 157 |
returned: when O(include_attributes=true) |
| 158 |
type: str |
| 159 |
sample: "" |
| 160 |
connection_logs_s3_enabled: |
| 161 |
description: Indicates whether connection logs are enabled. |
| 162 |
returned: when O(include_attributes=true) |
| 163 |
type: str |
| 164 |
sample: "false" |
| 165 |
connection_logs_s3_prefix: |
| 166 |
description: The prefix for the location in the S3 bucket for the connection logs. |
| 167 |
returned: when O(include_attributes=true) |
| 168 |
type: str |
| 169 |
sample: "" |
| 170 |
deletion_protection_enabled: |
| 171 |
description: Indicates whether deletion protection is enabled. |
| 172 |
returned: when O(include_attributes=true) |
| 173 |
type: str |
| 174 |
sample: "true" |
| 175 |
dns_name: |
| 176 |
description: The public DNS name of the load balancer. |
| 177 |
type: str |
| 178 |
sample: "internal-my-alb-123456789.ap-southeast-2.elb.amazonaws.com" |
| 179 |
idle_timeout_timeout_seconds: |
| 180 |
description: The idle timeout value, in seconds. |
| 181 |
returned: when O(include_attributes=true) |
| 182 |
type: str |
| 183 |
sample: "60" |
| 184 |
ip_address_type: |
| 185 |
description: The type of IP addresses used by the subnets for the load balancer. |
| 186 |
type: str |
| 187 |
sample: "ipv4" |
| 188 |
ipv6_deny_all_igw_traffic: |
| 189 |
description: Locks internet gateway (IGW) access to the load balancer. |
| 190 |
returned: when O(include_attributes=true) |
| 191 |
type: str |
| 192 |
sample: "false" |
| 193 |
listeners: |
| 194 |
description: Information about the listeners. |
| 195 |
returned: when O(include_listeners=true) or O(include_listener_rules=true) |
| 196 |
type: complex |
| 197 |
contains: |
| 198 |
listener_arn: |
| 199 |
description: The Amazon Resource Name (ARN) of the listener. |
| 200 |
type: str |
| 201 |
sample: "" |
| 202 |
load_balancer_arn: |
| 203 |
description: The Amazon Resource Name (ARN) of the load balancer. |
| 204 |
type: str |
| 205 |
sample: "" |
| 206 |
port: |
| 207 |
description: The port on which the load balancer is listening. |
| 208 |
type: int |
| 209 |
sample: 80 |
| 210 |
protocol: |
| 211 |
description: The protocol for connections from clients to the load balancer. |
| 212 |
type: str |
| 213 |
sample: "HTTPS" |
| 214 |
rules: |
| 215 |
description: List of listener rules. |
| 216 |
returned: when O(include_listener_rules=true) |
| 217 |
type: list |
| 218 |
elements: dict |
| 219 |
contains: |
| 220 |
rule_arn: |
| 221 |
description: The Amazon Resource Name (ARN) of the rule. |
| 222 |
type: str |
| 223 |
sample: "" |
| 224 |
priority: |
| 225 |
description: The priority. |
| 226 |
type: str |
| 227 |
sample: "default" |
| 228 |
is_default: |
| 229 |
description: Indicates whether this is the default rule. |
| 230 |
type: bool |
| 231 |
sample: false |
| 232 |
conditions: |
| 233 |
description: The conditions. |
| 234 |
type: list |
| 235 |
sample: [] |
| 236 |
actions: |
| 237 |
description: The actions. |
| 238 |
type: list |
| 239 |
elements: dict |
| 240 |
contains: |
| 241 |
type: |
| 242 |
description: The type of action. |
| 243 |
type: str |
| 244 |
target_group_arn: |
| 245 |
description: The Amazon Resource Name (ARN) of the target group. |
| 246 |
type: str |
| 247 |
forward_config: |
| 248 |
description: Information for creating an action that distributes requests among one or more target groups. |
| 249 |
type: dict |
| 250 |
contains: |
| 251 |
target_groups: |
| 252 |
description: The target groups. |
| 253 |
type: dict |
| 254 |
contains: |
| 255 |
target_group_arn: |
| 256 |
description: The Amazon Resource Name (ARN) of the target group. |
| 257 |
type: str |
| 258 |
weight: |
| 259 |
description: The weight. |
| 260 |
type: int |
| 261 |
target_group_stickiness_config: |
| 262 |
description: The target group stickiness for the rule. |
| 263 |
type: dict |
| 264 |
contains: |
| 265 |
enabled: |
| 266 |
description: Indicates whether target group stickiness is enabled. |
| 267 |
type: bool |
| 268 |
sample: [ |
| 269 |
{ |
| 270 |
"forward_config": { |
| 271 |
"target_group_stickiness_config": { |
| 272 |
"enabled": false |
| 273 |
}, |
| 274 |
"target_groups": [ |
| 275 |
{ |
| 276 |
"target_group_arn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/alb-test/09ba111f8079fb83", |
| 277 |
"weight": 1 |
| 278 |
} |
| 279 |
] |
| 280 |
}, |
| 281 |
"target_group_arn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/alb-test/09ba111f8079fb83", |
| 282 |
"type": "forward" |
| 283 |
} |
| 284 |
] |
| 285 |
certificates: |
| 286 |
description: The SSL server certificate. |
| 287 |
type: list |
| 288 |
elements: dict |
| 289 |
contains: |
| 290 |
certificate_arn: |
| 291 |
description: The Amazon Resource Name (ARN) of the certificate. |
| 292 |
type: str |
| 293 |
sample: "arn:aws:acm:us-east-1:123456789012:certificate/28d2f3d9-cb2f-4033-a9aa-e75e704125a2" |
| 294 |
ssl_policy: |
| 295 |
description: The security policy that defines which ciphers and protocols are supported. |
| 296 |
type: str |
| 297 |
sample: "ELBSecurityPolicy-2016-08" |
| 298 |
default_actions: |
| 299 |
description: The default actions for the listener. |
| 300 |
type: str |
| 301 |
contains: |
| 302 |
type: |
| 303 |
description: The type of action. |
| 304 |
type: str |
| 305 |
target_group_arn: |
| 306 |
description: The Amazon Resource Name (ARN) of the target group. |
| 307 |
type: str |
| 308 |
forward_config: |
| 309 |
description: Information for creating an action that distributes requests among one or more target groups. |
| 310 |
type: dict |
| 311 |
contains: |
| 312 |
target_groups: |
| 313 |
description: The target groups. |
| 314 |
type: dict |
| 315 |
contains: |
| 316 |
target_groups: |
| 317 |
description: The Amazon Resource Name (ARN) of the target group. |
| 318 |
type: str |
| 319 |
weight: |
| 320 |
description: The weight. |
| 321 |
type: int |
| 322 |
target_group_stickiness_config: |
| 323 |
description: The target group stickiness for the rule. |
| 324 |
type: dict |
| 325 |
contains: |
| 326 |
enabled: |
| 327 |
description: Indicates whether target group stickiness is enabled. |
| 328 |
type: bool |
| 329 |
sample: [ |
| 330 |
{ |
| 331 |
"forward_config": { |
| 332 |
"target_group_stickiness_config": { |
| 333 |
"enabled": false |
| 334 |
}, |
| 335 |
"target_groups": [ |
| 336 |
{ |
| 337 |
"target_group_arn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/alb-test/bf43c68602c51c02", |
| 338 |
"weight": 1 |
| 339 |
} |
| 340 |
] |
| 341 |
}, |
| 342 |
"target_group_arn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/alb-test/bf43c68602c51c02", |
| 343 |
"type": "forward" |
| 344 |
} |
| 345 |
] |
| 346 |
load_balancer_arn: |
| 347 |
description: The Amazon Resource Name (ARN) of the load balancer. |
| 348 |
type: str |
| 349 |
sample: "arn:aws:elasticloadbalancing:ap-southeast-2:123456789012:loadbalancer/app/my-alb/001122334455" |
| 350 |
load_balancer_name: |
| 351 |
description: The name of the load balancer. |
| 352 |
type: str |
| 353 |
sample: "my-alb" |
| 354 |
load_balancing_cross_zone_enabled: |
| 355 |
description: Indicates whether or not cross-zone load balancing is enabled. |
| 356 |
returned: when O(include_attributes=true) |
| 357 |
type: str |
| 358 |
sample: "true" |
| 359 |
routing_http2_enabled: |
| 360 |
description: Indicates whether HTTP/2 is enabled. |
| 361 |
returned: when O(include_attributes=true) |
| 362 |
type: str |
| 363 |
sample: "true" |
| 364 |
routing_http_desync_mitigation_mode: |
| 365 |
description: Determines how the load balancer handles requests that might pose a security risk to an application. |
| 366 |
returned: when O(include_attributes=true) |
| 367 |
type: str |
| 368 |
sample: "defensive" |
| 369 |
routing_http_drop_invalid_header_fields_enabled: |
| 370 |
description: Indicates whether HTTP headers with invalid header fields are removed by the load balancer (true) or routed to targets (false). |
| 371 |
returned: when O(include_attributes=true) |
| 372 |
type: str |
| 373 |
sample: "false" |
| 374 |
routing_http_preserve_host_header_enabled: |
| 375 |
description: |
| 376 |
- Indicates whether the Application Load Balancer should preserve the Host header in the HTTP request and send it to the target without any |
| 377 |
change. |
| 378 |
returned: when O(include_attributes=true) |
| 379 |
type: str |
| 380 |
sample: "false" |
| 381 |
routing_http_x_amzn_tls_version_and_cipher_suite_enabled: |
| 382 |
description: Indicates whether the two headers are added to the client request before sending it to the target. |
| 383 |
returned: when O(include_attributes=true) |
| 384 |
type: str |
| 385 |
sample: "false" |
| 386 |
routing_http_xff_client_port_enabled: |
| 387 |
description: Indicates whether the X-Forwarded-For header should preserve the source port that the client used to connect to the load balancer. |
| 388 |
returned: when O(include_attributes=true) |
| 389 |
type: str |
| 390 |
sample: "false" |
| 391 |
routing_http_xff_header_processing_mode: |
| 392 |
description: |
| 393 |
- Enables you to modify, preserve, or remove the X-Forwarded-For header in the HTTP request before the Application Load Balancer sends the |
| 394 |
request to the target. |
| 395 |
returned: when O(include_attributes=true) |
| 396 |
type: str |
| 397 |
sample: "append" |
| 398 |
scheme: |
| 399 |
description: Internet-facing or internal load balancer. |
| 400 |
type: str |
| 401 |
sample: "internal" |
| 402 |
security_groups: |
| 403 |
description: The IDs of the security groups for the load balancer. |
| 404 |
type: list |
| 405 |
sample: ["sg-0011223344"] |
| 406 |
state: |
| 407 |
description: The state of the load balancer. |
| 408 |
type: dict |
| 409 |
contains: |
| 410 |
code: |
| 411 |
description: The state code. |
| 412 |
type: str |
| 413 |
reason: |
| 414 |
description: A description of the state. |
| 415 |
returned: when available |
| 416 |
type: str |
| 417 |
sample: {"code": "active"} |
| 418 |
tags: |
| 419 |
description: The tags attached to the load balancer. |
| 420 |
type: dict |
| 421 |
sample: { |
| 422 |
"Tag": "Example" |
| 423 |
} |
| 424 |
type: |
| 425 |
description: The type of load balancer. |
| 426 |
type: str |
| 427 |
sample: "application" |
| 428 |
vpc_id: |
| 429 |
description: The ID of the VPC for the load balancer. |
| 430 |
type: str |
| 431 |
sample: "vpc-0011223344" |
| 432 |
waf_fail_open_enabled: |
| 433 |
description: Indicates whether to allow a AWS WAF-enabled load balancer to route requests to targets |
| 434 |
if it is unable to forward the request to AWS WAF. |
| 435 |
returned: when O(include_attributes=true) |
| 436 |
type: str |
| 437 |
sample: "false" |
| 438 |
""" |
| 439 |
|
| 440 |
from typing import Dict |
| 441 |
|
| 442 |
try: |
| 443 |
import botocore |
| 444 |
except ImportError: |
| 445 |
pass # Handled by AnsibleAWSModule |
| 446 |
|
| 447 |
from ansible_collections.amazon.aws.plugins.module_utils.elb_utils import AnsibleELBv2Error |
| 448 |
from ansible_collections.amazon.aws.plugins.module_utils.elb_utils import describe_load_balancers |
| 449 |
from ansible_collections.amazon.aws.plugins.module_utils.elb_utils import describe_tags |
| 450 |
from ansible_collections.amazon.aws.plugins.module_utils.elbv2 import build_application_load_balancer_description |
| 451 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 452 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 453 |
|
| 454 |
|
| 455 |
def get_load_balancer_tags(connection, load_balancer_arn: str) -> Dict[str, str]: |
| 456 |
tag_descriptions = describe_tags(connection, [load_balancer_arn]) |
| 457 |
return boto3_tag_list_to_ansible_dict(tag_descriptions[0]["Tags"]) |
| 458 |
|
| 459 |
|
| 460 |
def list_load_balancers(connection, module: AnsibleAWSModule) -> None: |
| 461 |
load_balancer_arns = module.params.get("load_balancer_arns") |
| 462 |
names = module.params.get("names") |
| 463 |
include_attributes = module.params.get("include_attributes") |
| 464 |
include_listeners = module.params.get("include_listeners") |
| 465 |
include_listener_rules = module.params.get("include_listener_rules") |
| 466 |
|
| 467 |
try: |
| 468 |
load_balancers = describe_load_balancers(connection, load_balancer_arns=load_balancer_arns, names=names) |
| 469 |
|
| 470 |
# Build complete ALB descriptions with normalization |
| 471 |
snaked_load_balancers = [ |
| 472 |
build_application_load_balancer_description( |
| 473 |
connection, |
| 474 |
lb, |
| 475 |
include_attributes=include_attributes, |
| 476 |
include_listeners=include_listeners, |
| 477 |
include_listener_rules=include_listener_rules, |
| 478 |
) |
| 479 |
for lb in load_balancers |
| 480 |
] |
| 481 |
|
| 482 |
# Get tags for each load balancer (not included in the boto3 describe_load_balancers response) |
| 483 |
for snaked_load_balancer in snaked_load_balancers: |
| 484 |
snaked_load_balancer["tags"] = get_load_balancer_tags(connection, snaked_load_balancer["load_balancer_arn"]) |
| 485 |
except AnsibleELBv2Error as e: |
| 486 |
module.fail_json_aws_error(e) |
| 487 |
|
| 488 |
module.exit_json(load_balancers=snaked_load_balancers) |
| 489 |
|
| 490 |
|
| 491 |
def main(): |
| 492 |
argument_spec = dict( |
| 493 |
load_balancer_arns=dict(type="list", elements="str"), |
| 494 |
names=dict(type="list", elements="str"), |
| 495 |
include_attributes=dict(default=True, type="bool"), |
| 496 |
include_listeners=dict(default=True, type="bool"), |
| 497 |
include_listener_rules=dict(default=True, type="bool"), |
| 498 |
) |
| 499 |
|
| 500 |
module = AnsibleAWSModule( |
| 501 |
argument_spec=argument_spec, |
| 502 |
mutually_exclusive=[["load_balancer_arns", "names"]], |
| 503 |
supports_check_mode=True, |
| 504 |
) |
| 505 |
|
| 506 |
try: |
| 507 |
connection = module.client("elbv2") |
| 508 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 509 |
module.fail_json_aws(e) |
| 510 |
|
| 511 |
list_load_balancers(connection, module) |
| 512 |
|
| 513 |
|
| 514 |
if __name__ == "__main__": |
| 515 |
main() |