0

我有创建实例并将其添加到负载均衡器的剧本,我可以删除/停止已分配给 ELB 的旧实例的方式是什么,我想确保我们首先停止旧实例,然后添加新的 1,反之亦然。我正在使用 AWS ELB 和 EC2 实例

4

1 回答 1

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
于 2016-07-28T13:44:32.977 回答