# When using ec2 or rax in a role, facts set following an add\_host loop are lost when execution returns to the calling playbook

**URL:** <https://forum.ansible.com/t/when-using-ec2-or-rax-in-a-role-facts-set-following-an-add-host-loop-are-lost-when-execution-returns-to-the-calling-playbook/19046>\
**Category:** Ansible Project\
**Created:** [August 19, 2015, 3:04am UTC](https://forum.ansible.com/t/when-using-ec2-or-rax-in-a-role-facts-set-following-an-add-host-loop-are-lost-when-execution-returns-to-the-calling-playbook/19046 "2015-08-19T03:04:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Casey](https://avatars.discourse-cdn.com/v4/letter/m/b4bc9f/32.png) [@Mark\_Casey](https://forum.ansible.com/u/Mark_Casey)\
**Post date:** [August 19, 2015, 3:04am UTC](https://forum.ansible.com/t/when-using-ec2-or-rax-in-a-role-facts-set-following-an-add-host-loop-are-lost-when-execution-returns-to-the-calling-playbook/19046/1 "2015-08-19T03:04:04Z")

</div>

Hello,

I’m actually revisiting a problem I originally just worked around… maybe 18 to 24 months ago. I just now did some searching of the list, but there were just pages and pages for most searches I tried so apologies if this is a solved thing and I missed it.

What I do now to deploy to cloud hosts is have the playbook call a role named rax/add\_rax or ec2/add\_ec2. In that role I use the appropriate cloud module to create the hosts (or discover they are already there, of course), then I run the add\_host loop on the result. The next role that it runs is named gather\_custom\_facts and it detects what provider is in use and uses the appropriate “get\_facts” type module to set facts named ‘private\_ip’ and ‘public\_ip’ for each host. (not to imply that hosts from different providers are in the same ansible-playbook run; that is still on a Someday list somewhere)

I do it this way so that plays work on both clouds with the same names for these IP variables (or facts in this case, I suppose). The problem is that I feel like it adds quite a bit of overhead (measured both in computing resources and to the learning curve for new team members). To just have a single ping: task you’re already two or three plays in just to have inventory.

It seems like the easiest thing would be to use set\_fact in the cloud provider-specific create/“persist” role and have it still exist in the calling playbook. But, of course, the hosts aren’t actually in inventory yet (and even if they are the set\_fact would still be a local\_action). I’ve revisited this once or twice in the past and never really got anywhere so here I am. 🙂

Any ideas?

Thank you,  
Mark

P.S.: For entertainment value only, I was wondering also if it’d be possible to write an inventory script that simply calls ansible-playbook again and spits out some inventory-compatible json after running these two or three host gathering roles.

---

<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:** [August 19, 2015, 3:44pm UTC](https://forum.ansible.com/t/when-using-ec2-or-rax-in-a-role-facts-set-following-an-add-host-loop-are-lost-when-execution-returns-to-the-calling-playbook/19046/2 "2015-08-19T15:44:43Z")

</div>

in 2.0 (current devel) we introduced meta: refresh\_inventory which  
should help with this case, no need to go through add\_hosts or  
set\_facts that would be in the inventory.

---

<div class="post-metadata">

**Author:** ![Mark\_Casey](https://avatars.discourse-cdn.com/v4/letter/m/b4bc9f/32.png) [@Mark\_Casey](https://forum.ansible.com/u/Mark_Casey)\
**Post date:** [August 19, 2015, 3:58pm UTC](https://forum.ansible.com/t/when-using-ec2-or-rax-in-a-role-facts-set-following-an-add-host-loop-are-lost-when-execution-returns-to-the-calling-playbook/19046/3 "2015-08-19T15:58:25Z")

</div>

Awesome, thanks for pointing that out.

I’ll try some things and post back later with how it goes. It may be quite a while, but I kind of suspect curiosity will get the best of me on this one and I may end up trying it from devel.

Thank you,  
Mark
