Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible: How to stop an EC2 instance started by a different playbook

If I start an EC2 instance (or set of instances) in Ansible, how can I later refer to that instance (or set) and terminate them?

This is across playbooks. So, in one playbook I've already started an instance, then later, I want to terminate those instances with another playbook.

Thanks.

like image 724
mtyson Avatar asked Dec 04 '14 21:12

mtyson


2 Answers

You will need to do something like this (working example):

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 }}"

to terminate instance at address instance.example.com:

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

The ec2 facts module will query the metadata service on the instance to get the instance ID. The ec2 module is used to terminate the instance by its ID.

Note the ec2_facts module needs to run on the instance(s) that you want to terminate and you will probably want to use an inventory file or dynamic inventory to lookup the instance(s) by tag instead of addressing them by hostname.

like image 76
jarv Avatar answered Oct 02 '22 19:10

jarv


Well, if we are going to use the dynamic inventory, then I recommend using count_tags and exact_count with the ec2 module when creating the instances with create.yml:

---
- 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

The env.yml file has all those variables, and the servers dictionary:

---
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

Now, if you want to change the number of servers, just change the count in the servers dictionary. If you want to delete all of them, we all the counts to 0.

Or, if you prefer, copy the create.yml file to delete_all.yml, and replace

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

with

exact_count: 0
like image 42
Jack Avatar answered Oct 02 '22 18:10

Jack