使用ansible创建aws目标组



我试图完成的是从动态生成的实例列表中创建一个网络负载均衡器目标组。

brokerInstancesList是实例ID的列表。我需要迭代这个列表,并将它们作为目标添加到这个目标组中。

- name: "Create 9092 target group"
elb_target_group:
name: "tg-{{ ClusterName }}"
protocol: tcp
port: 9092
vpc_id: "{{ VPCID }}"
targets:
- Id: "{{ item }}"
Port: 9092
state: present
loop: "{{ brokerInstancesList }}"

我上面尝试的问题是,只保留了brokerInstancesList中的最后一个条目。我需要下面这样的东西。

- name: "Create 9092 target group"
elb_target_group:
name: "tg-{{ ClusterName }}"
protocol: tcp
port: 9092
vpc_id: "{{ VPCID }}"
targets:
{% for item in {{ brokerInstancesList }} -%}
- Id: "{{ apple }}"
Port: 9092
{%- endfor %}
state: present

使用elb_target模块来实现它:

  • name:收集所有新代理实例的事实
    ec2_instance_facts:
    筛选器:
    "tag:name":"{{ec2_tag_proxy}}">
    寄存器:ec2_proxy

  • elb_target_group:
    名称:uat目标代理
    协议:http
    端口:80
    vpc_id:vpc-4e6e8112
    注销延迟超时:60
    粘性启用:True
    黏性_lb_cookie_duration:86400
    health_check_path:/
    成功响应代码:"200">
    健康检查间隔:"20">
    state:当前

  • elb_target:
    target_group_name:uat目标代理
    target _id:"{{item.instance_id}}">
    target_port:80
    state:present
    with_items:"{ec2_proxy.instances}">

您需要在上一步中创建目标列表:

- name: Create the custom fact for targets                                       
set_fact:                                                                      
target_data: "{{ target_data|default([]) + [{'Id': item, 'Port': 9092 }]}}"
with_items: "{{ brokerInstancesList }}"

然后将target_data列表用于elb_target_group任务中的targets属性。

来源:https://github.com/ansible/ansible/issues/32218#issuecomment-339792059

最新更新