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

Find the list of all the AWS ALB target groups in which instance is registered

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

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

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

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

- 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 }}"
    region: "{{ ansible_ec2_placement_region }}"
    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 }}"
    region: "{{ ansible_ec2_placement_region }}"
    target_status: healthy
    target_status_timeout: 300
    state: present
  loop: "{{ alb_target_facts.instance_target_groups }}"
  become: no