有没有办法使用 Ansible 在 AWS 中创建网络负载平衡?Application LB 有一个模块,但 NLB 没有。是否可以使用 Boto3 来做到这一点?
1073 次
2 回答
0
我认为最好的方法是使用elb_network_lb如下所示的模块:
创建一个 ELB 并附加一个监听器
- elb_network_lb:
name: myelb
subnets:
- subnet-012345678
- subnet-abcdef000
listeners:
- Protocol: TCP # Required. The protocol for connections from clients to the load balancer (Only TCP is available) (case-sensitive).
Port: 80 # Required. The port on which the load balancer is listening.
DefaultActions:
- Type: forward # Required. Only 'forward' is accepted at this time
TargetGroupName: mytargetgroup # Required. The name of the target group
state: present
使用附加的弹性 IP 地址创建 ELB
- elb_network_lb:
name: myelb
subnet_mappings:
- SubnetId: subnet-012345678
AllocationId: eipalloc-aabbccdd
listeners:
- Protocol: TCP # Required. The protocol for connections from clients to the load balancer (Only TCP is available) (case-sensitive).
Port: 80 # Required. The port on which the load balancer is listening.
DefaultActions:
- Type: forward # Required. Only 'forward' is accepted at this time
TargetGroupName: mytargetgroup # Required. The name of the target group
state: present
移除 ELB
- elb_network_lb:
name: myelb
state: absent
于 2019-10-14T12:44:23.213 回答
0
通过 Boto3 创建网络负载均衡器
网络负载均衡器 (NLB) 和应用程序负载均衡器 (ALB) 在 CLI 和 SDK 中都归类为 Elastic Load Balancing V2。这是因为与 Classic Load Balancer (ELB) 相比,它们具有不同的底层 API。
因此,使用 boto3 创建 NLB 将属于 elbv2 客户端:
import boto3
client = boto3.client('elbv2')
client.create_load_balancer(Name='my-load-balancer', Type='network')
有关更多信息,请查看elbv2 的 boto3 文档。
通过 Ansible 配置负载均衡器
Ansible elb_application_lb似乎不支持将type密钥作为输入。作为一种解决方法,我建议使用 Ansible cloudFormation模块来配置负载均衡器。
CloudFormation 模板 -my-nlb-stack.yml
Resources:
NetworkLoadBalancer:
Type: "AWS::ElasticLoadBalancingV2::LoadBalancer"
Properties:
Name: my-network-lb
Type: network
Subnets:
- subnet-aabbccdd
- subnet-ddeeff11
- subnet-22334455
Outputs:
MyNLB:
Description: The ARN of the newly provisioned NLB
Value: !Ref NetworkLoadBalancer
Ansible 剧本 -playbook.yml
---
- hosts: all
tasks:
- name: launch ansible network lb stack with cloudformation
cloudformation:
stack_name: MyNetworkLBStack
state: present
region: eu-west-1
template: my-lb-stack.yml
register: nlbstack
- name: check the facts of the load balancer
elb_application_lb_facts:
load_balancer_arns:
- "{{ nlbstack.stack_outputs.MyNLB }}"
配置堆栈后,您将能够毫无问题地针对 NLB 使用应用程序负载平衡器模块。
于 2018-05-01T16:42:24.450 回答