2014-12-04 61 views
6

如果我在Ansible中啓動一個EC2實例(或一組實例),我以後如何引用該實例(或設置)並終止它們?Ansible:如何停止由另一個劇本啓動的EC2實例

這是跨劇本。所以,在一個劇本中,我已經開始了一個實例,然後,我想用另一個劇本來終止這些實例。

謝謝。

回答

10

你需要做這樣的事情(工作示例):

terminate.yml:

$ ansible-playbook -i instance.example.com, terminate.yml 

- name: terminate single instance 
    hosts: all 
    tasks: 
    - action: ec2_facts 
    - name: terminating single instance 
     local_action: 
     module: ec2 
     state: 'absent' 
     region: us-east-1 
     instance_ids: "{{ ansible_ec2_instance_id }}" 

在地址instance.example.com終止實例ec2 facts module將查詢實例上的元數據服務以獲取實例ID。 ec2 module用於通過ID終止實例。

請注意,ec2_facts模塊需要在要終止的實例上運行,並且您可能希望使用清單文件或dynamic inventory通過標記查找實例,而不是通過主機名尋址它們。

1

我將用一個工作示例來擴展jarv的答案,在該示例中,您可以使用完整的劇本終止單個或一組服務器。

讓我們假設,你要終止的實例(S),該​​刪除組下屬於你的hosts文件:

[delete] 
X.X.X.X # IP address of an EC2 instance 

現在你的劇本將是這個樣子(在我的情況,我命名它 「ec2_terminate.yml」):

--- 
- hosts: delete 
    gather_facts: True 
    user: ubuntu 
    sudo: True 
    tasks: 

    # fetch instance data from the metadata servers in ec2 
    - ec2_facts: 

    # just show the instance-id 
    - debug: msg= "{{ hostvars[inventory_hostname]['ansible_ec2_instance_id'] }}" 

- hosts: delete 
    gather_facts: True 
    connection: local 
    vars: 
    region: "us-east-1" 
    tasks: 
    - name: destroy all instances 
     ec2: state='absent' 
      region={{ region }} 
      instance_ids={{ item }} 
      wait=true 
     with_items: hostvars[inventory_hostname]['ansible_ec2_instance_id'] 

現在運行這個劇本是這樣的:

ansible-playbook -i hosts ec2_terminate.yml 
+0

,這似乎運行ec2_facts後我的工作:{{ansible_ec2_instance_id}} – radtek 2015-03-12 06:39:49

2

那麼,如果我們爲G卷板機使用動態庫存,那麼我建議使用count_tagsexact_countcreate.yml創建實例時的EC2模塊:

--- 
- hosts: localhost 
    connection: local 
    gather_facts: false 
    vars_files: { ./env.yml } 
    tasks: 
    - name: Provision a set of instances 
    ec2: 
     instance_type: "{{ item.value.instance_type }}" 
     image: "{{ image }}" 
     region: "{{ region }}" 
     vpc_subnet_id: "{{ item.value.vpc_subnet_id }}" 
     tenancy: "{{ tenancy }}" 
     group_id: "{{ group_id }}" 
     key_name: "{{ key_name }}" 
     wait: true 
     instance_tags: 
      Name: "{{ env_id }}" 
      Type: "{{ item.key }}" 
     count_tag: 
      Type: "{{ item.key }}" 
     exact_count: "{{ item.value.count }}" 
    with_dict: "{{ servers }}" 
    register: ec2 

的env.yml文件擁有所有這些變量,和服務器字典:

--- 
env_id: JaxDemo 
key_name: JaxMagicKeyPair 
image: "ami-xxxxxxxx" 
region: us-east-1 
group_id: "sg-xxxxxxxx,sg-yyyyyyyy,sg-zzzzzzzz" 
tenancy: dedicated 

servers: 
    app: 
     count: 2 
     vpc_subnet_id: subnet-xxxxxxxx 
     instance_type: m3.medium 
    httpd: 
     count: 1 
     vpc_subnet_id: subnet-yyyyyyyy 
     instance_type: m3.medium 
    oracle: 
     count: 1 
     vpc_subnet_id: subnet-zzzzzzzz 
     instance_type: m4.4xlarge 

現在,如果要更改服務器數量,只需更改服務器字典中的計數即可。如果要刪除它們全部,我們將所有計數設爲0.

或者,如果您願意,請複製create。陽明海運文件delete_all.yml,並且只有一臺服務器代替

exact_count: "{{ item.value.count }}" 

exact_count: 0