# stuck on creating an EC2 playbook using dynamic inventory

**URL:** <https://forum.ansible.com/t/stuck-on-creating-an-ec2-playbook-using-dynamic-inventory/13939>\
**Category:** Ansible Project\
**Created:** [November 20, 2013, 7:35am UTC](https://forum.ansible.com/t/stuck-on-creating-an-ec2-playbook-using-dynamic-inventory/13939 "2013-11-20T07:35:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sandeep\_Srinivasa](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@Sandeep\_Srinivasa](https://forum.ansible.com/u/Sandeep_Srinivasa)\
**Post date:** [November 20, 2013, 7:35am UTC](https://forum.ansible.com/t/stuck-on-creating-an-ec2-playbook-using-dynamic-inventory/13939/1 "2013-11-20T07:35:16Z")

</div>

hi guys,  
Just started using Ansible - have been using it primarily to work with building clusters on EC2.

Now, my playbook is working perfectly with creating clusters. However I am trying to use another playbook to delete these clusters. I have other planned scripts that resizes the disks, etc.

I’m just not able to figure out how to work with managing running EC2 instances - I am adding them in a EC2 security group, tagging them (using “instance\_tags” - not sure if I should use “ec2\_tag” instead).

Here’s the output of the ec2.py script and the teardown script that I’m experimenting with and which is not working - [https://gist.github.com/sandys/7559115](https://gist.github.com/sandys/7559115)  
I’m running it with “ansible-playbook -i ec2.py”, so I am able to refer to “hosts:security\_group\_default”, but I’m just not sure how to populate “instance\_ids” which is required for “state:absent”.

Now, I took a look at [https://gist.github.com/bhang/6320004](https://gist.github.com/bhang/6320004) (ELB example), but I think they are running the commands _from_ the servers on EC2 - however, I probably need to keep using “hosts:localhost”.

any help ?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 20, 2013, 2:16pm UTC](https://forum.ansible.com/t/stuck-on-creating-an-ec2-playbook-using-dynamic-inventory/13939/2 "2013-11-20T14:16:40Z")

</div>

Before we dive into your pastebins, “I’m just not able to figure out how to work with managing running EC2 instances”

What does “work with” mean?

---

<div class="post-metadata">

**Author:** ![Sandeep\_Srinivasa](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@Sandeep\_Srinivasa](https://forum.ansible.com/u/Sandeep_Srinivasa)\
**Post date:** [November 20, 2013, 5:08pm UTC](https://forum.ansible.com/t/stuck-on-creating-an-ec2-playbook-using-dynamic-inventory/13939/3 "2013-11-20T17:08:38Z")

</div>

> Before we dive into your pastebins, “I’m just not able to figure out how to work with managing running EC2 instances”
> 
> What does “work with” mean?

hi Michael,  
thanks for replying.  
I was speaking colloquially: My whole question was around being able to use ansible to work with the AWS management API itself. I’m pretty clear (I think!! - but not too many examples available) on how to use the dynamic inventory .py file to address certain hosts and work with playbooks on them.

What I dont understand is how do you address these running hosts (categorized into security-groups, tags, etc) and perform actions like shutting them down, changing their security group, etc. Most of the examples I have seen - launch instances, register → “ec2”, shut them down by referring to them as “ec2.instance\_ids”, etc.

regards  
-Sandeep

---

<div class="post-metadata">

**Author:** ![benno\_joy](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@benno\_joy](https://forum.ansible.com/u/benno_joy)\
**Post date:** [November 20, 2013, 5:18pm UTC](https://forum.ansible.com/t/stuck-on-creating-an-ec2-playbook-using-dynamic-inventory/13939/4 "2013-11-20T17:18:42Z")

</div>

HI Sandeep,

Here’s an example where a complete openshift stack is provisioned and deployed in ec2 as well as removed by ansible playbooks.  
Hope this helps.

[https://github.com/ansible/ansible-examples/tree/master/openshift](https://github.com/ansible/ansible-examples/tree/master/openshift)

have a look at ec2.yml and ec2\_remove.yml.

Regards,  
Benno

---

<div class="post-metadata">

**Author:** ![Sandeep\_Srinivasa](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@Sandeep\_Srinivasa](https://forum.ansible.com/u/Sandeep_Srinivasa)\
**Post date:** [November 20, 2013, 7:02pm UTC](https://forum.ansible.com/t/stuck-on-creating-an-ec2-playbook-using-dynamic-inventory/13939/5 "2013-11-20T19:02:13Z")

</div>

> HI Sandeep,
> 
> Here’s an example where a complete openshift stack is provisioned and deployed in ec2 as well as removed by ansible playbooks.  
> Hope this helps.
> 
> [https://github.com/ansible/ansible-examples/tree/master/openshift](https://github.com/ansible/ansible-examples/tree/master/openshift)
> 
> have a look at ec2.yml and ec2\_remove.yml.

hi Benno,  
I did look at it - especially [https://github.com/ansible/ansible-examples/blob/master/openshift/roles/ec2\_remove/tasks/main.yml](https://github.com/ansible/ansible-examples/blob/master/openshift/roles/ec2_remove/tasks/main.yml) .

From what I see what it is trying to do is recreate an instance with the same id, which of course fails because of idempotency, use that data to try and delete the cluster. Why is this done this way ? I’m a beginner in configuration management, so I just wanted to understand if it is a best practice to do things this way.

By extension, does everything in ansible work this way - for example, if I wanted to remove nginx, would I first idempotently install it and then remove it ?

regards  
Sandeep

---

<div class="post-metadata">

**Author:** ![tannerjc](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/tannerjc/32/985_2.png) [@tannerjc](https://forum.ansible.com/u/tannerjc)\
**Post date:** [November 20, 2013, 9:13pm UTC](https://forum.ansible.com/t/stuck-on-creating-an-ec2-playbook-using-dynamic-inventory/13939/6 "2013-11-20T21:13:01Z")

</div>

"""but I'm just not sure how to populate "instance\_ids" which is required for "state:absent"."""

I think this is your main issue, right?

After calling the ec2\_facts module, the inventory hosts will have an ansible\_ec2\_instance-id fact, which you can then use to delete them.

See [https://gist.github.com/jctanner/7467100#unified-single-node-example](https://gist.github.com/jctanner/7467100#unified-single-node-example) for an example.

with\_items: hostvars[inventory\_hostname]['ansible\_ec2\_instance-id']

---

<div class="post-metadata">

**Author:** ![Sandeep\_Srinivasa](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@Sandeep\_Srinivasa](https://forum.ansible.com/u/Sandeep_Srinivasa)\
**Post date:** [November 21, 2013, 5:47am UTC](https://forum.ansible.com/t/stuck-on-creating-an-ec2-playbook-using-dynamic-inventory/13939/7 "2013-11-21T05:47:43Z")

</div>

James,  
thanks ! let me take a look at that.

regards  
Sandeep

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 23, 2013, 4:20pm UTC](https://forum.ansible.com/t/stuck-on-creating-an-ec2-playbook-using-dynamic-inventory/13939/8 "2013-11-23T16:20:11Z")

</div>

"  
By extension, does everything in ansible work this way - for example, if I wanted to remove nginx, would I first idempotently install it and then remove it ?"

No, you never have to do this anywhere

yum: name=nginx state=absent
