# Refreshing EC2 inventory between playbooks

**URL:** <https://forum.ansible.com/t/refreshing-ec2-inventory-between-playbooks/17396>\
**Category:** Ansible Project\
**Created:** [January 29, 2015, 12:33pm UTC](https://forum.ansible.com/t/refreshing-ec2-inventory-between-playbooks/17396 "2015-01-29T12:33:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tzach\_Livyatan](https://avatars.discourse-cdn.com/v4/letter/t/f07891/32.png) [@Tzach\_Livyatan](https://forum.ansible.com/u/Tzach_Livyatan)\
**Post date:** [January 29, 2015, 12:33pm UTC](https://forum.ansible.com/t/refreshing-ec2-inventory-between-playbooks/17396/1 "2015-01-29T12:33:33Z")

</div>

Hi all  
I’m using ec2 module to launch EC2 instances in one play book, and “-i ec2.py” to query a subset of the instances in a second playbook.  
So far so good.

The problem I have is when I started to call both play books, using include, from one “super” playbook.  
AFAIU, the inventory did not refresh, so the second one failed.  
I can bypass the problem by running both playbooks from a bash script with

`./ec2.py --refresh-cache > /dev/null`  
between them, but it is ugly and cause other problems.

Any Idiomatic alternative?

Thanks  
Tzach

---

<div class="post-metadata">

**Author:** ![Giovanni\_Tirloni](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Giovanni\_Tirloni](https://forum.ansible.com/u/Giovanni_Tirloni)\
**Post date:** [January 29, 2015, 1:01pm UTC](https://forum.ansible.com/t/refreshing-ec2-inventory-between-playbooks/17396/2 "2015-01-29T13:01:29Z")

</div>

Try dynamically including new machines to groups with add\_host:

[http://docs.ansible.com/add\_host\_module.html](http://docs.ansible.com/add_host_module.html)  
[http://docs.ansible.com/ec2\_module.html](http://docs.ansible.com/ec2_module.html) (see examples at bottom)

Giovanni

---

<div class="post-metadata">

**Author:** ![Tzach\_Livyatan](https://avatars.discourse-cdn.com/v4/letter/t/f07891/32.png) [@Tzach\_Livyatan](https://forum.ansible.com/u/Tzach_Livyatan)\
**Post date:** [January 29, 2015, 1:53pm UTC](https://forum.ansible.com/t/refreshing-ec2-inventory-between-playbooks/17396/3 "2015-01-29T13:53:13Z")

</div>

Hi Giovanni  
Thanks for the prompt response.  
If I’m reading the doc correctly, the new group can only be used in the same playbook.  
Is it so?  
I’m using more than one playbook, including all of the from one “super” playbook.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [January 29, 2015, 3:14pm UTC](https://forum.ansible.com/t/refreshing-ec2-inventory-between-playbooks/17396/4 "2015-01-29T15:14:33Z")

</div>

add\_host affects the 'current run', all subsequent plays will have  
access, the distribtion of the plays in different files does not  
matter.

ansible reads all the files passed to it (directly or through  
includes), compiles the plays and then runs them in order, at this  
point the files they came from are irrelevant.

---

<div class="post-metadata">

**Author:** ![Tzach\_Livyatan](https://avatars.discourse-cdn.com/v4/letter/t/f07891/32.png) [@Tzach\_Livyatan](https://forum.ansible.com/u/Tzach_Livyatan)\
**Post date:** [January 31, 2015, 3:23pm UTC](https://forum.ansible.com/t/refreshing-ec2-inventory-between-playbooks/17396/5 "2015-01-31T15:23:44Z")

</div>

Thanks Brain, Giovanni  
I’m confused on when to use ec2.py dynamic inventory and when creating group as you suggested.  
If I understand correctly, the first make sense if I want to query info set by others, while the second make more sense if the servers live and die in the scope of my playbook (my use case).

I’m currently using hostvars from ec2.py to get the internal ips of servers:

- command: echo “{{ hostvars[item].ec2\_private\_ip\_address }}”  
with\_items: groups.tag\_Name\_Cassandra  
register: output  
changed\_when: no

Can I get the internal IP without ec2.py?  
If I can not, refreshing the inventory between plays is mandatory - going back to my original question.

---

<div class="post-metadata">

**Author:** ![Tzach\_Livyatan](https://avatars.discourse-cdn.com/v4/letter/t/f07891/32.png) [@Tzach\_Livyatan](https://forum.ansible.com/u/Tzach_Livyatan)\
**Post date:** [February 1, 2015, 2:44pm UTC](https://forum.ansible.com/t/refreshing-ec2-inventory-between-playbooks/17396/6 "2015-02-01T14:44:48Z")

</div>

Found a solution, let me answer myself if anyone is interested

I got a list the internal IPs by creating a new group, as follow:

- name: Add instances to private ip group  
local\_action: add\_host name={{item.private\_ip}} groupname=CassandraPrivate\_{{load\_name}}  
with\_items: ec2.instances

And using it from a different playbook as follow:

- command: echo “{{ item }}”  
with\_items: groups.CassandraPrivate\_{{load\_name}}  
register: outputPrivate  
changed\_when: no

- set\_fact: ip\_list=“{{ outputPrivate.results|map(attribute=‘stdout’)|join(‘,’) }}”

Thanks again
