有没有办法使用Ansible在AWS中创建网络负载平衡? 应用程序LB有一个模块,但NLB没有。是否可以使用Boto3来做到这一点?
答案 0 :(得分:0)
网络负载均衡器(NLB)和应用程序负载均衡器(ALB)均归类于CLI和SDK中的Elastic Load Balancing V2。这是因为与经典负载均衡器(ELB)相比,它们具有不同的底层API。
因此,使用boto3创建NLB将属于elbv2客户端:
import boto3
client = boto3.client('elbv2')
client.create_load_balancer(Name='my-load-balancer', Type='network')
有关详细信息,请查看boto3 docs for elbv2。
Ansible elb_application_lb似乎不支持type
键作为输入。作为解决方法,我建议使用Ansible 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
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。
答案 1 :(得分:0)
我认为最好的方法是使用模块elb_network_lb
,如下所示:
- 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
- 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_network_lb:
name: myelb
state: absent