查找已注册实例的所有AWS ALB目标组的列表

问题描述 投票:0回答:2

对于ELB,如果我们想要从所有elbs中删除实例,我们只需要将实例id传递给elb_instance模块,如果我们想要在同一个playbook中添加实例,它会给我们一个魔术变量ec2_elbs,我们可以迭代这个变量并将实例添加回或注册到他之前注册的所有elbs。

我没有找到任何模块,我可以用它来查找实例注册的目标组列表。如果知道它,有人会指出。

ansible ansible-2.x
2个回答
0
投票

这是elb_target_group模块,它处于预览模式,或者您可以使用python脚本和boto来查找特定目标组中的实例列表


0
投票

我找到了从已注册的所有目标组中动态删除实例的方法:

- name: Collect facts about EC2 Instance
  action: ec2_metadata_facts

- name: Get the list of all target groups to which Instance(s) registered
  local_action:
    module: elb_target_facts
    instance_id: "{{ ansible_ec2_instance_id }}"
    region: "{{ ansible_ec2_placement_region }}"
  register: alb_target_facts
  become: no

- name: Deregister the Instance from all target groups
  local_action:
    module: elb_target
    target_group_arn: "{{ item.target_group_arn }}"
    target_id: "{{ ansible_ec2_instance_id }}"
    target_status_timeout: 300
    target_status: unused
    state: absent
  loop: "{{ alb_target_facts.instance_target_groups }}"
  become: no

因为我已在变量中注册了目标组信息,所以我可以再次使用它来添加它:

- name: Register the Instance back to the target group(s)
  local_action:
    module: elb_target
    target_group_arn: "{{ item.target_group_arn }}"
    target_id: "{{ ansible_ec2_instance_id }}"
    target_status: healthy
    target_status_timeout: 300
    state: present
  loop: "{{ alb_target_facts.instance_target_groups }}"
  become: no
© www.soinside.com 2019 - 2024. All rights reserved.