简体   繁体   English

使用Ansible创建AWS目标组

[英]Creating an aws target group with ansible

What Im trying to accomplish is to create a network load balancer target group from dynamically generated list of instances. 我试图完成的任务是从动态生成的实例列表创建网络负载平衡器目标组。

brokerInstancesList is a list of instance ids. brokerInstancesList是实例ID的列表。 I need to iterate over this list and add them as targets to this target group. 我需要遍历此列表,并将它们作为目标添加到该目标组。

- 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 }}"

The issue with my attempt above is that only the last entry in the brokerInstancesList is kept. 我上面尝试的问题在于,仅保留brokerInstancesList中的最后一个条目。 Something like the below is what I need. 我需要以下类似的东西。

- 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

You need to create the list of targets in a previous step: 您需要在上一步中创建目标列表:

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

And then use the target_data list for the targets attribute in the elb_target_group task. 然后在elb_target_group任务中将target_data列表用作targets属性。

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

Use elb_target module to implement it: 使用elb_target模块来实现它:

  • name: Gather facts for all new proxy instances 名称:收集所有新代理实例的事实
    ec2_instance_facts: ec2_instance_facts:
    filters: 筛选器:
    "tag:Name": "{{ ec2_tag_proxy }}" “ tag:Name”:“ {{ec2_tag_proxy}}”
    register: ec2_proxy 注册:ec2_proxy

  • elb_target_group: elb_target_group:
    name: uat-target-proxy 名称:uat-target-proxy
    protocol: http 通讯协定:http
    port: 80 端口:80
    vpc_id: vpc-4e6e8112 vpc_id:vpc-4e6e8112
    deregistration_delay_timeout: 60 deregistration_delay_timeout:60
    stickiness_enabled: True stickiness_enabled:真
    stickiness_lb_cookie_duration: 86400 stickiness_lb_cookie_duration:86400
    health_check_path: / health_check_path:/
    successful_response_codes: "200" success_response_codes:“ 200”
    health_check_interval: "20" health_check_interval:“ 20”
    state: present 状态:存在

  • elb_target: elb_target:
    target_group_name: uat-target-proxy target_group_name:uat-target-proxy
    target_id: "{{ item.instance_id }}" target_id:“ {{item.instance_id}}”
    target_port: 80 target_port:80
    state: present 状态:存在
    with_items: "{{ ec2_proxy.instances }}" with_items:“ {{ec2_proxy.instances}}”
    when: ec2_proxy.instances|length > 0 时间:ec2_proxy.instances | length> 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM