# Force re-read of dynamic inventory script?

**URL:** <https://forum.ansible.com/t/force-re-read-of-dynamic-inventory-script/16807>\
**Category:** Ansible Project\
**Created:** [November 17, 2014, 9:57pm UTC](https://forum.ansible.com/t/force-re-read-of-dynamic-inventory-script/16807 "2014-11-17T21:57:21Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josh\_Smift1](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@Josh\_Smift1](https://forum.ansible.com/u/Josh_Smift1)\
**Post date:** [November 17, 2014, 9:57pm UTC](https://forum.ansible.com/t/force-re-read-of-dynamic-inventory-script/16807/1 "2014-11-17T21:57:21Z")

</div>

I feel like I've seen questions like this on the list before, but I'm  
having a hard time finding one now; apologies if this is old ground.

I've got a playbook that provisions a new Rackspace Cloud host, and then  
includes a role specific to that type of host. In that role, there are  
some templates that need to know what groups the host is in. Because the  
host didn't exist when our dynamic inventory script was run, it wasn't in  
any groups then; but if the dynamic inventory script were run again, it  
would be in some new groups now, including some that the script comes up  
with -- wait for it -- dynamically.

So: Is there a way to force Ansible to refresh the inventory in the middle  
of a playbook?

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-Josh (jbs@care.com)

This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.

---

<div class="post-metadata">

**Author:** ![Michael\_Peters](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@Michael\_Peters](https://forum.ansible.com/u/Michael_Peters)\
**Post date:** [November 17, 2014, 10:19pm UTC](https://forum.ansible.com/t/force-re-read-of-dynamic-inventory-script/16807/2 "2014-11-17T22:19:45Z")

</div>

No, but you can add the machine to the necessary groups when you use  
add\_host after its created.

---

<div class="post-metadata">

**Author:** ![jorginator74](https://avatars.discourse-cdn.com/v4/letter/j/f19dbf/32.png) [@jorginator74](https://forum.ansible.com/u/jorginator74)\
**Post date:** [November 17, 2014, 10:27pm UTC](https://forum.ansible.com/t/force-re-read-of-dynamic-inventory-script/16807/3 "2014-11-17T22:27:39Z")

</div>

Hi

> I feel like I've seen questions like this on the list before, but I'm  
> having a hard time finding one now; apologies if this is old ground.
> 
> I've got a playbook that provisions a new Rackspace Cloud host, and then  
> includes a role specific to that type of host. In that role, there are  
> some templates that need to know what groups the host is in. Because the  
> host didn't exist when our dynamic inventory script was run, it wasn't in  
> any groups then; but if the dynamic inventory script were run again, it  
> would be in some new groups now, including some that the script comes up  
> with -- wait for it -- dynamically.

[http://docs.ansible.com/add\_host\_module.html](http://docs.ansible.com/add_host_module.html) may be useful for you.

> So: Is there a way to force Ansible to refresh the inventory in the middle  
> of a playbook?

Not that I know of.

> This email is intended for the person(s) to whom it is addressed and  
> may contain information that is PRIVILEGED or CONFIDENTIAL. Any  
> unauthorized use, distribution, copying, or disclosure by any person  
> other than the addressee(s) is strictly prohibited. If you have  
> received this email in error, please notify the sender immediately  
> by return email and delete the message and any attachments from your  
> system.

(sigh). Really??? Really?? Surely nobody uses that legalese rubbish  
noise anymore?

I am now guilty of something that was prohibited - after all, this was  
not addressed to me. I re-distributed and copied and disclosed your  
possibly PRIVILEGED and CONFIDENTIAL email! Quick! Tell everybody to  
delete it! (and then delete the emails about deleting it. Deny  
everything!!). Perhaps I should turn myself in?

But I'm such a law breaker. Sometimes I press the button for the 3RD  
FLOOR on the lift, even when only goin to the 2ND FLOOR! I'm not even  
going to return the original mail to you! Hah! Take that! You'll  
have to get NEW ones and zeros for your next email! That'll teach you!

bwhahahahaha!

---

<div class="post-metadata">

**Author:** ![Josh\_Smift1](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@Josh\_Smift1](https://forum.ansible.com/u/Josh_Smift1)\
**Post date:** [November 17, 2014, 11:14pm UTC](https://forum.ansible.com/t/force-re-read-of-dynamic-inventory-script/16807/4 "2014-11-17T23:14:00Z")

</div>

> No, but you can add the machine to the necessary groups when you use  
> add\_host after its created.

Yeah, I can mostly do this; the tricky thing is that there are some  
derived groups that the host gets added to based on logic in the dynamic  
inventory script. I could in theory duplicate that logic in my playbook,  
but it'd be hard to be sure to get right.

Another workaround would be to have a wrapper script that calls Ansible  
twice, once to launch and then a second time to configure; the tricky  
thing there is that we're using exact\_count, and registering the new  
instances, so that we know which ones are new. I'm not sure how we'd  
preserve that across Ansible runs.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-Josh (jbs@care.com)

This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.

---

<div class="post-metadata">

**Author:** ![Josh\_Smift1](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@Josh\_Smift1](https://forum.ansible.com/u/Josh_Smift1)\
**Post date:** [November 17, 2014, 11:15pm UTC](https://forum.ansible.com/t/force-re-read-of-dynamic-inventory-script/16807/5 "2014-11-17T23:15:16Z")

</div>

> This email is intended for the person(s) to whom it is addressed and  
> may contain information that is PRIVILEGED or CONFIDENTIAL.

> (sigh). Really??? Really?? Surely nobody uses that legalese rubbish  
> noise anymore?

Surely not. I can't explain where that text came from. Probably nobody  
wrote it.

> I am now guilty of something that was prohibited - after all, this  
> was not addressed to me. I re-distributed and copied and disclosed  
> your possibly PRIVILEGED and CONFIDENTIAL email! Quick! Tell  
> everybody to delete it! (and then delete the emails about deleting  
> it. Deny everything!!). Perhaps I should turn myself in?
> 
> But I'm such a law breaker. Sometimes I press the button for the 3RD  
> FLOOR on the lift, even when only goin to the 2ND FLOOR! I'm not even  
> going to return the original mail to you! Hah! Take that! You'll have  
> to get NEW ones and zeros for your next email! That'll teach you!
> 
> bwhahahahaha!

You are very funny and clever.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-Josh (jbs@care.com)

This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.

---

<div class="post-metadata">

**Author:** ![Michael\_Peters](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@Michael\_Peters](https://forum.ansible.com/u/Michael_Peters)\
**Post date:** [November 17, 2014, 11:22pm UTC](https://forum.ansible.com/t/force-re-read-of-dynamic-inventory-script/16807/6 "2014-11-17T23:22:29Z")

</div>

You could try abstracting out the logic into a separate script that  
both your inventory and your provisioning playbook could call to  
determine which groups to put it in. I agree that it's a bit of a  
pain. I'd really like to have this be possible as well since it would  
really simplify some workflows.

---

<div class="post-metadata">

**Author:** ![Tomasz\_Kontusz](https://avatars.discourse-cdn.com/v4/letter/t/bc79bd/32.png) [@Tomasz\_Kontusz](https://forum.ansible.com/u/Tomasz_Kontusz)\
**Post date:** [November 18, 2014, 7:50am UTC](https://forum.ansible.com/t/force-re-read-of-dynamic-inventory-script/16807/7 "2014-11-18T07:50:00Z")

</div>

It should be possible to write something like that as an action plugin. The only problem I see would be: what to do with group\_vars?

Michael Peters [michael00peters@gmail.com](mailto:michael00peters@gmail.com) napisał:

---

<div class="post-metadata">

**Author:** ![Josh\_Smift1](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@Josh\_Smift1](https://forum.ansible.com/u/Josh_Smift1)\
**Post date:** [November 18, 2014, 3:38pm UTC](https://forum.ansible.com/t/force-re-read-of-dynamic-inventory-script/16807/8 "2014-11-18T15:38:44Z")

</div>

Bleah, so the latest snag I've run into is that our dynamic inventory  
script sets some host variables, including some that are based on the  
derived groups that it comes up with, and of course \*those\* aren't  
available even after adding the host to a group during a run, because  
they're not "group variables" per se. There's not a way to have a dynamic  
inventory script create group variables, is there? It's all just host  
variables?

It seems like the best approach here might be to just do it in two passes,  
one run to provision the systems and a second one to configure them,  
perhaps setting some metadata at Rackspace (in this case, or the analogous  
thing for EC2 or whoever) so that the second pass can find hosts that need  
post-provisioning configuration. (Which I also have a question about...  
Stay tuned. :^)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-Josh (jbs@care.com)

This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [November 18, 2014, 4:02pm UTC](https://forum.ansible.com/t/force-re-read-of-dynamic-inventory-script/16807/9 "2014-11-18T16:02:55Z")

</div>

​That is certainly possible:  
[http://docs.ansible.com/developing\_inventory.html#script-conventions](http://docs.ansible.com/developing_inventory.html#script-conventions)

​

---

<div class="post-metadata">

**Author:** ![Josh\_Smift1](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@Josh\_Smift1](https://forum.ansible.com/u/Josh_Smift1)\
**Post date:** [November 18, 2014, 9:33pm UTC](https://forum.ansible.com/t/force-re-read-of-dynamic-inventory-script/16807/10 "2014-11-18T21:33:30Z")

</div>

> There's not a way to have a dynamic inventory script create group  
> variables, is there? It's all just host variables?

> That is certainly possible:  
> [http://docs.ansible.com/developing\_inventory.html#script-conventions](http://docs.ansible.com/developing_inventory.html#script-conventions)

Oooh, that is super, and after modifying our custom dynamic inventory  
script, it works great. Thanks!

One best-practices type question about this: A side effect of the change  
to our script is that before, for each group, we were iterating over the  
hosts in that group, and turning the variables associated with the group  
into hostvars for the hosts; now, putting the variables into the groups  
section, those variables don't show up as hostvars any more. We could, of  
course, \*also\* put them into hostvars, but my intuition is that this would  
be less correct: That if a variable is set as a group variable, you  
wouldn't expect to see it in hostvars. True?

Thanks again for the tip!

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-Josh (jbs@care.com)

This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.
