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

时间:2016-07-28 09:42:16

标签: amazon-ec2 ansible ansible-playbook elastic-load-balancer

我有Playbook创建并向Load balancer添加实例,我可以删除/停止已分配给ELB的旧实例的方式是什么,我想确保先停止旧实例,然后添加新的1或者反之亦然。 我正在使用AWS ELB和EC2实例

1 个答案:

答案 0 :(得分:1)

我希望这对你有所帮助。获得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