# stopping ec2 spot instances

**URL:** <https://forum.ansible.com/t/stopping-ec2-spot-instances/16630>\
**Category:** Ansible Project\
**Created:** [October 28, 2014, 11:23pm UTC](https://forum.ansible.com/t/stopping-ec2-spot-instances/16630 "2014-10-28T23:23:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bryan\_Hunt](https://avatars.discourse-cdn.com/v4/letter/b/bb73d2/32.png) [@Bryan\_Hunt](https://forum.ansible.com/u/Bryan_Hunt)\
**Post date:** [October 28, 2014, 11:23pm UTC](https://forum.ansible.com/t/stopping-ec2-spot-instances/16630/1 "2014-10-28T23:23:14Z")

</div>

I’m writing an ansible demo, showing how to provision Riak clusters, and I want to make it super self contained.

With that in mind, I’ve been playing with the ec2.py inventory script, and the ansible ec2 module.

Starting the nodes works great (if anyone is interested, I’m happy to share the code, or perhaps just make the repo public)

But, I’m having a problem with stopping the nodes.

I have those vars available:

TASK: [List tagged instances] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [127.0.0.1] =\> {  
“groups["tag\_instance\_type\_ansible\_demo"]”: [  
“54.171.160.194”  
]  
}

TASK: [everything for that one host] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [127.0.0.1] =\> {  
“hostvars["54.171.160.194"]["ec2\_id"]”: “i-f28793b0”  
}

The problem is that in order to stop and ec2 instances, I need to get at the ec2\_id, in order to feed it to the ec2 module (which stops nodes based upon the instance id, not the ip address.

tasks:

- name: shut down all the spot instances  
local\_action:  
module: ec2  
region: eu-west-1  
wait: no  
instance\_ids: ‘{{ instance\_ids }}’  
state: absent

I can’t figure out how to munge this into a usable list, has anyone else experienced this before?

Bryan

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [October 30, 2014, 8:33pm UTC](https://forum.ansible.com/t/stopping-ec2-spot-instances/16630/2 "2014-10-30T20:33:58Z")

</div>

Can you get this using the “ec2\_facts” module or are you using the dynamic inventory script?

---

<div class="post-metadata">

**Author:** ![Carlos\_V](https://avatars.discourse-cdn.com/v4/letter/c/838e76/32.png) [@Carlos\_V](https://forum.ansible.com/u/Carlos_V)\
**Post date:** [November 3, 2014, 10:37am UTC](https://forum.ansible.com/t/stopping-ec2-spot-instances/16630/3 "2014-11-03T10:37:44Z")

</div>

This is how I got something similar working in order to terminate instances. You will need to change the state and I’m using the inventory script as my hosts file. I’m sure it can be cleaned up. In this case, I’m targeting ec2 instances with the Nginx\_Server tag.

`

# Connect to the node and gather facts,

# including the instance-id. These facts

# are added to inventory hostvars for the

# duration of the playbook’s execution

# Typical “provisioning” tasks would go in

# this playbook.

- hosts: tag\_Name\_Nginx\_Server  
gather\_facts: True  
user: root  
sudo: True  
tasks:

# fetch instance data from the metadata servers in ec2

- ec2\_facts:

# show all known facts for this host

#- debug: var=hostvars[inventory\_hostname]

# just show the instance-id

- debug: msg=“{{ hostvars[inventory\_hostname][‘ansible\_ec2\_instance\_id’] }}”

# Using the instanceid, call the ec2 module

# locally to remove the instance by declaring

# its state is “absent”

- hosts: tag\_Name\_Nginx\_Server  
gather\_facts: True  
connection: local  
tasks:
- name: destroy all instances  
ec2: state=‘absent’  
instance\_ids={{ item }}  
wait=true  
with\_items: hostvars[inventory\_hostname][‘ansible\_ec2\_instance\_id’]

`
