如何在 ansible 中停止来自 ELB 的旧实例?

How to stop old instance from ELB in ansible?

我有创建实例并将实例添加到负载均衡器的剧本,我可以如何 remove/stop 旧实例已分配给 ELB,我想确保我们先停止旧实例,然后再停止新实例添加 1,反之亦然。 我正在使用 AWS ELB 和 EC2 实例

希望对您有所帮助。一旦你有了 instance id 然后你可以做任何你想做的事,我只是将它从 ELB 中删除但是你可以使用 ec2 模块来删除它。

---
- hosts: localhost
  connection: local
  gather_facts: no
  tasks:
   - name: Get the facts about the ELB
     ec2_elb_facts:
       names: rbgeek-dev-web-elb
       region: "eu-west-1"
     register: elb_facts

   - name: Instance(s) ID that are currently register to the ELB
     debug:
       msg: "{{ elb_facts.elbs.0.instances }}"

   - name: Tag the Old instances as zombie
     ec2_tag:
       resource: "{{ item }}"
       region: "eu-west-1"
       state: present
       tags:
         Instance_Status: "zombie"
     with_items: "{{ elb_facts.elbs.0.instances }}"

   - name: Refresh the ec2.py cache
     shell: ./inventory/ec2.py --refresh-cache
     changed_when: no

   - name: Refresh inventory
     meta: refresh_inventory


# must check this step with ec2_remote_facts_module.html ##http://docs.ansible.com/ansible/ec2_remote_facts_module.html
- hosts: tag_Instance_Status_zombie
  gather_facts: no
  tasks:
    - name: Get the facts about the zombie instances
      ec2_facts:
    - name: remove instance by instance id
      ec2:
        state: absent
        region: "eu-west-1"
        instance_ids: "{{ ansible_ec2_instance_id }}"
        wait: true
      delegate_to: localhost