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

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. :slight_smile:

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.

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.

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