| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, Rob White (@wimnat) |
| 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_network_lb |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Manage a Network Load Balancer |
| 12 |
description: |
| 13 |
- Manage an AWS Network Elastic Load Balancer. See |
| 14 |
U(https://aws.amazon.com/blogs/aws/new-network-load-balancer-effortless-scaling-to-millions-of-requests-per-second/) for details. |
| 15 |
author: |
| 16 |
- "Rob White (@wimnat)" |
| 17 |
options: |
| 18 |
cross_zone_load_balancing: |
| 19 |
description: |
| 20 |
- Indicates whether cross-zone load balancing is enabled. |
| 21 |
- Defaults to C(false). |
| 22 |
type: bool |
| 23 |
deletion_protection: |
| 24 |
description: |
| 25 |
- Indicates whether deletion protection for the ELB is enabled. |
| 26 |
- Defaults to C(false). |
| 27 |
type: bool |
| 28 |
listeners: |
| 29 |
description: |
| 30 |
- A list of dicts containing listeners to attach to the ELB. See examples for detail of the dict required. Note that listener keys |
| 31 |
are CamelCased. |
| 32 |
type: list |
| 33 |
elements: dict |
| 34 |
suboptions: |
| 35 |
Port: |
| 36 |
description: The port on which the load balancer is listening. |
| 37 |
type: int |
| 38 |
required: true |
| 39 |
Protocol: |
| 40 |
description: The protocol for connections from clients to the load balancer. |
| 41 |
type: str |
| 42 |
required: true |
| 43 |
Certificates: |
| 44 |
description: The SSL server certificate. |
| 45 |
type: list |
| 46 |
elements: dict |
| 47 |
suboptions: |
| 48 |
CertificateArn: |
| 49 |
description: The Amazon Resource Name (ARN) of the certificate. |
| 50 |
type: str |
| 51 |
SslPolicy: |
| 52 |
description: The security policy that defines which ciphers and protocols are supported. |
| 53 |
type: str |
| 54 |
DefaultActions: |
| 55 |
description: The default actions for the listener. |
| 56 |
required: true |
| 57 |
type: list |
| 58 |
elements: dict |
| 59 |
suboptions: |
| 60 |
Type: |
| 61 |
description: The type of action. |
| 62 |
type: str |
| 63 |
TargetGroupArn: |
| 64 |
description: |
| 65 |
- The Amazon Resource Name (ARN) of the target group. |
| 66 |
- Mutually exclusive with I(TargetGroupName). |
| 67 |
type: str |
| 68 |
TargetGroupName: |
| 69 |
description: |
| 70 |
- The name of the target group. |
| 71 |
- Mutually exclusive with I(TargetGroupArn). |
| 72 |
AlpnPolicy: |
| 73 |
description: |
| 74 |
- The name of the Application-Layer Protocol Negotiation (ALPN) policy. |
| 75 |
type: str |
| 76 |
choices: |
| 77 |
- HTTP1Only |
| 78 |
- HTTP2Only |
| 79 |
- HTTP2Optional |
| 80 |
- HTTP2Preferred |
| 81 |
- None |
| 82 |
version_added: 7.1.0 |
| 83 |
name: |
| 84 |
description: |
| 85 |
- The name of the load balancer. This name must be unique within your AWS account, can have a maximum of 32 characters, must contain only alphanumeric |
| 86 |
characters or hyphens, and must not begin or end with a hyphen. |
| 87 |
required: true |
| 88 |
type: str |
| 89 |
purge_listeners: |
| 90 |
description: |
| 91 |
- If I(purge_listeners=true), existing listeners will be purged from the ELB to match exactly what is defined by I(listeners) parameter. |
| 92 |
- If the I(listeners) parameter is not set then listeners will not be modified. |
| 93 |
default: true |
| 94 |
type: bool |
| 95 |
subnet_mappings: |
| 96 |
description: |
| 97 |
- A list of dicts containing the IDs of the subnets to attach to the load balancer. You can also specify the allocation ID of an Elastic IP |
| 98 |
to attach to the load balancer or the internal IP address for an internal load balancer. You can specify one Elastic IP address or internal |
| 99 |
address per subnet. |
| 100 |
- This parameter is mutually exclusive with I(subnets). |
| 101 |
type: list |
| 102 |
elements: dict |
| 103 |
subnets: |
| 104 |
description: |
| 105 |
- A list of the IDs of the subnets to attach to the load balancer. You can specify only one subnet per Availability Zone. You must specify subnets from |
| 106 |
at least two Availability Zones. |
| 107 |
- Required when I(state=present). |
| 108 |
- This parameter is mutually exclusive with I(subnet_mappings). |
| 109 |
type: list |
| 110 |
elements: str |
| 111 |
scheme: |
| 112 |
description: |
| 113 |
- Internet-facing or internal load balancer. An ELB scheme can not be modified after creation. |
| 114 |
default: internet-facing |
| 115 |
choices: [ 'internet-facing', 'internal' ] |
| 116 |
type: str |
| 117 |
state: |
| 118 |
description: |
| 119 |
- Create or destroy the load balancer. |
| 120 |
- The default changed from C('absent') to C('present') in release 4.0.0. |
| 121 |
choices: [ 'present', 'absent' ] |
| 122 |
type: str |
| 123 |
default: 'present' |
| 124 |
wait: |
| 125 |
description: |
| 126 |
- Whether or not to wait for the network load balancer to reach the desired state. |
| 127 |
type: bool |
| 128 |
wait_timeout: |
| 129 |
description: |
| 130 |
- The duration in seconds to wait, used in conjunction with I(wait). |
| 131 |
type: int |
| 132 |
ip_address_type: |
| 133 |
description: |
| 134 |
- Sets the type of IP addresses used by the subnets of the specified Application Load Balancer. |
| 135 |
choices: [ 'ipv4', 'dualstack' ] |
| 136 |
type: str |
| 137 |
notes: |
| 138 |
- Listeners are matched based on port. If a listener's port is changed then a new listener will be created. |
| 139 |
- Listener rules are matched based on priority. If a rule's priority is changed then a new rule will be created. |
| 140 |
extends_documentation_fragment: |
| 141 |
- amazon.aws.common.modules |
| 142 |
- amazon.aws.region.modules |
| 143 |
- amazon.aws.tags |
| 144 |
- amazon.aws.boto3 |
| 145 |
""" |
| 146 |
|
| 147 |
EXAMPLES = r""" |
| 148 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 149 |
|
| 150 |
- name: Create an ELB and attach a listener |
| 151 |
community.aws.elb_network_lb: |
| 152 |
name: myelb |
| 153 |
subnets: |
| 154 |
- subnet-012345678 |
| 155 |
- subnet-abcdef000 |
| 156 |
listeners: |
| 157 |
- Protocol: TCP # Required. The protocol for connections from clients to the load balancer (TCP, TLS, UDP or TCP_UDP) (case-sensitive). |
| 158 |
Port: 80 # Required. The port on which the load balancer is listening. |
| 159 |
DefaultActions: |
| 160 |
- Type: forward # Required. Only 'forward' is accepted at this time |
| 161 |
TargetGroupName: mytargetgroup # Required. The name of the target group |
| 162 |
state: present |
| 163 |
|
| 164 |
- name: Create an ELB with an attached Elastic IP address |
| 165 |
community.aws.elb_network_lb: |
| 166 |
name: myelb |
| 167 |
subnet_mappings: |
| 168 |
- SubnetId: subnet-012345678 |
| 169 |
AllocationId: eipalloc-aabbccdd |
| 170 |
listeners: |
| 171 |
- Protocol: TCP # Required. The protocol for connections from clients to the load balancer (TCP, TLS, UDP or TCP_UDP) (case-sensitive). |
| 172 |
Port: 80 # Required. The port on which the load balancer is listening. |
| 173 |
DefaultActions: |
| 174 |
- Type: forward # Required. Only 'forward' is accepted at this time |
| 175 |
TargetGroupName: mytargetgroup # Required. The name of the target group |
| 176 |
state: present |
| 177 |
|
| 178 |
- name: Create an internal ELB with a specified IP address |
| 179 |
community.aws.elb_network_lb: |
| 180 |
name: myelb |
| 181 |
scheme: internal |
| 182 |
subnet_mappings: |
| 183 |
- SubnetId: subnet-012345678 |
| 184 |
PrivateIPv4Address: 192.168.0.1 # Must be an address from within the CIDR of the subnet. |
| 185 |
listeners: |
| 186 |
- Protocol: TCP # Required. The protocol for connections from clients to the load balancer (TCP, TLS, UDP or TCP_UDP) (case-sensitive). |
| 187 |
Port: 80 # Required. The port on which the load balancer is listening. |
| 188 |
DefaultActions: |
| 189 |
- Type: forward # Required. Only 'forward' is accepted at this time |
| 190 |
TargetGroupName: mytargetgroup # Required. The name of the target group |
| 191 |
state: present |
| 192 |
|
| 193 |
- name: Remove an ELB |
| 194 |
community.aws.elb_network_lb: |
| 195 |
name: myelb |
| 196 |
state: absent |
| 197 |
""" |
| 198 |
|
| 199 |
RETURN = r""" |
| 200 |
load_balancer: |
| 201 |
description: A representation of the Network Load Balancer |
| 202 |
returned: when state is present |
| 203 |
type: dict |
| 204 |
version_added: 5.0.0 |
| 205 |
contains: |
| 206 |
availability_zones: |
| 207 |
description: The Availability Zones for the load balancer. |
| 208 |
returned: when state is present |
| 209 |
type: list |
| 210 |
sample: "[{'subnet_id': 'subnet-aabbccddff', 'zone_name': 'ap-southeast-2a', 'load_balancer_addresses': []}]" |
| 211 |
canonical_hosted_zone_id: |
| 212 |
description: The ID of the Amazon Route 53 hosted zone associated with the load balancer. |
| 213 |
returned: when state is present |
| 214 |
type: str |
| 215 |
sample: ABCDEF12345678 |
| 216 |
created_time: |
| 217 |
description: The date and time the load balancer was created. |
| 218 |
returned: when state is present |
| 219 |
type: str |
| 220 |
sample: "2015-02-12T02:14:02+00:00" |
| 221 |
deletion_protection_enabled: |
| 222 |
description: Indicates whether deletion protection is enabled. |
| 223 |
returned: when state is present |
| 224 |
type: str |
| 225 |
sample: true |
| 226 |
dns_name: |
| 227 |
description: The public DNS name of the load balancer. |
| 228 |
returned: when state is present |
| 229 |
type: str |
| 230 |
sample: internal-my-elb-123456789.ap-southeast-2.elb.amazonaws.com |
| 231 |
idle_timeout_timeout_seconds: |
| 232 |
description: The idle timeout value, in seconds. |
| 233 |
returned: when state is present |
| 234 |
type: str |
| 235 |
sample: 60 |
| 236 |
ip_address_type: |
| 237 |
description: The type of IP addresses used by the subnets for the load balancer. |
| 238 |
returned: when state is present |
| 239 |
type: str |
| 240 |
sample: ipv4 |
| 241 |
listeners: |
| 242 |
description: Information about the listeners. |
| 243 |
returned: when state is present |
| 244 |
type: complex |
| 245 |
contains: |
| 246 |
listener_arn: |
| 247 |
description: The Amazon Resource Name (ARN) of the listener. |
| 248 |
returned: when state is present |
| 249 |
type: str |
| 250 |
sample: "" |
| 251 |
load_balancer_arn: |
| 252 |
description: The Amazon Resource Name (ARN) of the load balancer. |
| 253 |
returned: when state is present |
| 254 |
type: str |
| 255 |
sample: "" |
| 256 |
port: |
| 257 |
description: The port on which the load balancer is listening. |
| 258 |
returned: when state is present |
| 259 |
type: int |
| 260 |
sample: 80 |
| 261 |
protocol: |
| 262 |
description: The protocol for connections from clients to the load balancer. |
| 263 |
returned: when state is present |
| 264 |
type: str |
| 265 |
sample: HTTPS |
| 266 |
certificates: |
| 267 |
description: The SSL server certificate. |
| 268 |
returned: when state is present |
| 269 |
type: complex |
| 270 |
contains: |
| 271 |
certificate_arn: |
| 272 |
description: The Amazon Resource Name (ARN) of the certificate. |
| 273 |
returned: when state is present |
| 274 |
type: str |
| 275 |
sample: "" |
| 276 |
ssl_policy: |
| 277 |
description: The security policy that defines which ciphers and protocols are supported. |
| 278 |
returned: when state is present |
| 279 |
type: str |
| 280 |
sample: "" |
| 281 |
default_actions: |
| 282 |
description: The default actions for the listener. |
| 283 |
returned: when state is present |
| 284 |
type: str |
| 285 |
contains: |
| 286 |
type: |
| 287 |
description: The type of action. |
| 288 |
returned: when state is present |
| 289 |
type: str |
| 290 |
sample: "" |
| 291 |
target_group_arn: |
| 292 |
description: The Amazon Resource Name (ARN) of the target group. |
| 293 |
returned: when state is present |
| 294 |
type: str |
| 295 |
sample: "" |
| 296 |
alpn_policy: |
| 297 |
description: The name of the Application-Layer Protocol Negotiation (ALPN) policy. |
| 298 |
returned: when state is present |
| 299 |
type: list |
| 300 |
elements: str |
| 301 |
version_added: 7.1.0 |
| 302 |
sample: ["HTTP1Only", "HTTP2Only"] |
| 303 |
load_balancer_arn: |
| 304 |
description: The Amazon Resource Name (ARN) of the load balancer. |
| 305 |
returned: when state is present |
| 306 |
type: str |
| 307 |
sample: arn:aws:elasticloadbalancing:ap-southeast-2:123456789012:loadbalancer/app/my-elb/001122334455 |
| 308 |
load_balancer_name: |
| 309 |
description: The name of the load balancer. |
| 310 |
returned: when state is present |
| 311 |
type: str |
| 312 |
sample: my-elb |
| 313 |
load_balancing_cross_zone_enabled: |
| 314 |
description: Indicates whether cross-zone load balancing is enabled. |
| 315 |
returned: when state is present |
| 316 |
type: str |
| 317 |
sample: true |
| 318 |
scheme: |
| 319 |
description: Internet-facing or internal load balancer. |
| 320 |
returned: when state is present |
| 321 |
type: str |
| 322 |
sample: internal |
| 323 |
state: |
| 324 |
description: The state of the load balancer. |
| 325 |
returned: when state is present |
| 326 |
type: dict |
| 327 |
sample: "{'code': 'active'}" |
| 328 |
tags: |
| 329 |
description: The tags attached to the load balancer. |
| 330 |
returned: when state is present |
| 331 |
type: dict |
| 332 |
sample: "{ |
| 333 |
'Tag': 'Example' |
| 334 |
}" |
| 335 |
type: |
| 336 |
description: The type of load balancer. |
| 337 |
returned: when state is present |
| 338 |
type: str |
| 339 |
sample: network |
| 340 |
vpc_id: |
| 341 |
description: The ID of the VPC for the load balancer. |
| 342 |
returned: when state is present |
| 343 |
type: str |
| 344 |
sample: vpc-0011223344 |
| 345 |
""" |
| 346 |
|
| 347 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 348 |
|
| 349 |
from ansible_collections.amazon.aws.plugins.module_utils.elbv2 import ELBListener |
| 350 |
from ansible_collections.amazon.aws.plugins.module_utils.elbv2 import ELBListeners |
| 351 |
from ansible_collections.amazon.aws.plugins.module_utils.elbv2 import NetworkLoadBalancer |
| 352 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 353 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import compare_aws_tags |
| 354 |
|
| 355 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 356 |
|
| 357 |
|
| 358 |
def create_or_update_elb(elb_obj): |
| 359 |
"""Create ELB or modify main attributes. json_exit here""" |
| 360 |
if elb_obj.elb: |
| 361 |
# ELB exists so check subnets, security groups and tags match what has been passed |
| 362 |
|
| 363 |
# Subnets |
| 364 |
if not elb_obj.compare_subnets(): |
| 365 |
elb_obj.modify_subnets() |
| 366 |
|
| 367 |
# Tags - only need to play with tags if tags parameter has been set to something |
| 368 |
if elb_obj.tags is not None: |
| 369 |
# Delete necessary tags |
| 370 |
tags_need_modify, tags_to_delete = compare_aws_tags( |
| 371 |
boto3_tag_list_to_ansible_dict(elb_obj.elb["tags"]), |
| 372 |
boto3_tag_list_to_ansible_dict(elb_obj.tags), |
| 373 |
elb_obj.purge_tags, |
| 374 |
) |
| 375 |
if tags_to_delete: |
| 376 |
elb_obj.delete_tags(tags_to_delete) |
| 377 |
|
| 378 |
# Add/update tags |
| 379 |
if tags_need_modify: |
| 380 |
elb_obj.modify_tags() |
| 381 |
|
| 382 |
else: |
| 383 |
# Create load balancer |
| 384 |
elb_obj.create_elb() |
| 385 |
|
| 386 |
# ELB attributes |
| 387 |
elb_obj.update_elb_attributes() |
| 388 |
elb_obj.modify_elb_attributes() |
| 389 |
|
| 390 |
# Listeners |
| 391 |
listeners_obj = ELBListeners(elb_obj.connection, elb_obj.module, elb_obj.elb["LoadBalancerArn"]) |
| 392 |
|
| 393 |
listeners_to_add, listeners_to_modify, listeners_to_delete = listeners_obj.compare_listeners() |
| 394 |
|
| 395 |
# Delete listeners |
| 396 |
for listener_to_delete in listeners_to_delete: |
| 397 |
listener_obj = ELBListener( |
| 398 |
elb_obj.connection, elb_obj.module, listener_to_delete, elb_obj.elb["LoadBalancerArn"] |
| 399 |
) |
| 400 |
listener_obj.delete() |
| 401 |
listeners_obj.changed = True |
| 402 |
|
| 403 |
# Add listeners |
| 404 |
for listener_to_add in listeners_to_add: |
| 405 |
listener_obj = ELBListener(elb_obj.connection, elb_obj.module, listener_to_add, elb_obj.elb["LoadBalancerArn"]) |
| 406 |
listener_obj.add() |
| 407 |
listeners_obj.changed = True |
| 408 |
|
| 409 |
# Modify listeners |
| 410 |
for listener_to_modify in listeners_to_modify: |
| 411 |
listener_obj = ELBListener( |
| 412 |
elb_obj.connection, elb_obj.module, listener_to_modify, elb_obj.elb["LoadBalancerArn"] |
| 413 |
) |
| 414 |
listener_obj.modify() |
| 415 |
listeners_obj.changed = True |
| 416 |
|
| 417 |
# If listeners changed, mark ELB as changed |
| 418 |
if listeners_obj.changed: |
| 419 |
elb_obj.changed = True |
| 420 |
|
| 421 |
# Update ELB ip address type only if option has been provided |
| 422 |
if elb_obj.module.params.get("ip_address_type") is not None: |
| 423 |
elb_obj.modify_ip_address_type(elb_obj.module.params.get("ip_address_type")) |
| 424 |
|
| 425 |
# Update the objects to pickup changes |
| 426 |
# Get the ELB again |
| 427 |
elb_obj.update() |
| 428 |
# Get the ELB listeners again |
| 429 |
listeners_obj.update() |
| 430 |
# Update the ELB attributes |
| 431 |
elb_obj.update_elb_attributes() |
| 432 |
|
| 433 |
# Convert to snake_case and merge in everything we want to return to the user |
| 434 |
snaked_elb = camel_dict_to_snake_dict(elb_obj.elb) |
| 435 |
snaked_elb.update(camel_dict_to_snake_dict(elb_obj.elb_attributes)) |
| 436 |
snaked_elb["listeners"] = [] |
| 437 |
for listener in listeners_obj.current_listeners: |
| 438 |
snaked_elb["listeners"].append(camel_dict_to_snake_dict(listener)) |
| 439 |
|
| 440 |
# Change tags to ansible friendly dict |
| 441 |
snaked_elb["tags"] = boto3_tag_list_to_ansible_dict(snaked_elb["tags"]) |
| 442 |
|
| 443 |
# ip address type |
| 444 |
snaked_elb["ip_address_type"] = elb_obj.get_elb_ip_address_type() |
| 445 |
|
| 446 |
elb_obj.module.exit_json(changed=elb_obj.changed, load_balancer=snaked_elb, **snaked_elb) |
| 447 |
|
| 448 |
|
| 449 |
def delete_elb(elb_obj): |
| 450 |
if elb_obj.elb: |
| 451 |
elb_obj.delete() |
| 452 |
|
| 453 |
elb_obj.module.exit_json(changed=elb_obj.changed) |
| 454 |
|
| 455 |
|
| 456 |
def main(): |
| 457 |
argument_spec = dict( |
| 458 |
cross_zone_load_balancing=dict(type="bool"), |
| 459 |
deletion_protection=dict(type="bool"), |
| 460 |
listeners=dict( |
| 461 |
type="list", |
| 462 |
elements="dict", |
| 463 |
options=dict( |
| 464 |
Protocol=dict(type="str", required=True), |
| 465 |
Port=dict(type="int", required=True), |
| 466 |
SslPolicy=dict(type="str"), |
| 467 |
Certificates=dict(type="list", elements="dict"), |
| 468 |
DefaultActions=dict(type="list", required=True, elements="dict"), |
| 469 |
AlpnPolicy=dict( |
| 470 |
type="str", |
| 471 |
choices=["HTTP1Only", "HTTP2Only", "HTTP2Optional", "HTTP2Preferred", "None"], |
| 472 |
), |
| 473 |
), |
| 474 |
), |
| 475 |
name=dict(required=True, type="str"), |
| 476 |
purge_listeners=dict(default=True, type="bool"), |
| 477 |
purge_tags=dict(default=True, type="bool"), |
| 478 |
subnets=dict(type="list", elements="str"), |
| 479 |
subnet_mappings=dict(type="list", elements="dict"), |
| 480 |
scheme=dict(default="internet-facing", choices=["internet-facing", "internal"]), |
| 481 |
state=dict(choices=["present", "absent"], type="str", default="present"), |
| 482 |
tags=dict(type="dict", aliases=["resource_tags"]), |
| 483 |
wait_timeout=dict(type="int"), |
| 484 |
wait=dict(type="bool"), |
| 485 |
ip_address_type=dict(type="str", choices=["ipv4", "dualstack"]), |
| 486 |
) |
| 487 |
|
| 488 |
required_if = [ |
| 489 |
["state", "present", ["subnets", "subnet_mappings"], True], |
| 490 |
] |
| 491 |
|
| 492 |
module = AnsibleAWSModule( |
| 493 |
argument_spec=argument_spec, |
| 494 |
required_if=required_if, |
| 495 |
mutually_exclusive=[["subnets", "subnet_mappings"]], |
| 496 |
) |
| 497 |
|
| 498 |
# Check for subnets or subnet_mappings if state is present |
| 499 |
state = module.params.get("state") |
| 500 |
|
| 501 |
# Quick check of listeners parameters |
| 502 |
listeners = module.params.get("listeners") |
| 503 |
if listeners is not None: |
| 504 |
for listener in listeners: |
| 505 |
for key in listener.keys(): |
| 506 |
protocols_list = ["TCP", "TLS", "UDP", "TCP_UDP"] |
| 507 |
if key == "Protocol" and listener[key] not in protocols_list: |
| 508 |
module.fail_json(msg="'Protocol' must be either " + ", ".join(protocols_list)) |
| 509 |
|
| 510 |
connection = module.client("elbv2") |
| 511 |
connection_ec2 = module.client("ec2") |
| 512 |
|
| 513 |
elb = NetworkLoadBalancer(connection, connection_ec2, module) |
| 514 |
|
| 515 |
if state == "present": |
| 516 |
create_or_update_elb(elb) |
| 517 |
else: |
| 518 |
delete_elb(elb) |
| 519 |
|
| 520 |
|
| 521 |
if __name__ == "__main__": |
| 522 |
main() |