# Are group\_vars re-evaluated for freshly provisioned EC2 instances with tags matching group names?

**URL:** <https://forum.ansible.com/t/are-group-vars-re-evaluated-for-freshly-provisioned-ec2-instances-with-tags-matching-group-names/16126>\
**Category:** Ansible Project\
**Created:** [September 5, 2014, 2:33pm UTC](https://forum.ansible.com/t/are-group-vars-re-evaluated-for-freshly-provisioned-ec2-instances-with-tags-matching-group-names/16126 "2014-09-05T14:33:29Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Craig\_Marvelley](https://avatars.discourse-cdn.com/v4/letter/c/d26b3c/32.png) [@Craig\_Marvelley](https://forum.ansible.com/u/Craig_Marvelley)\
**Post date:** [September 5, 2014, 2:33pm UTC](https://forum.ansible.com/t/are-group-vars-re-evaluated-for-freshly-provisioned-ec2-instances-with-tags-matching-group-names/16126/1 "2014-09-05T14:33:29Z")

</div>

Hi all! Up until now, I’ve provisioned machines in EC2 in two stages - the first set of playbooks launch the machine and tag it as necessary, and then a second set of playbooks does the proper provisioning, using group\_vars identified automatically according to the tag\_NAME\_VALUE convention to dictate how the plays are run.

I’ve just changed this process so the launching and configuration happens all in the same set of playbooks, i.e. there’s no break in between these playbooks running. With this done, it seems the group vars matching the new instance’s tags are not applied automatically - possibly because these are evaluated by the dynamic inventory script when the overarching play begins, and not as new items enter the inventory?

After reading around, I believe that were I to manually add the instance into groups (named after each of its tags) after launch, the matching group\_vars would be loaded and applied (I’ve tried and can’t get this to work, though). In any case that’s not an obvious way to do it - so I was hoping someone could suggest an alternative? Is there a way to force a reload of group vars based on an updated dynamic inventory during a play?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 5, 2014, 7:30pm UTC](https://forum.ansible.com/t/are-group-vars-re-evaluated-for-freshly-provisioned-ec2-instances-with-tags-matching-group-names/16126/2 "2014-09-05T19:30:10Z")

</div>

> Hi all! Up until now, I've provisioned machines in EC2 in two stages - the  
> first set of playbooks launch the machine and tag it as necessary, and then  
> a second set of playbooks does the proper provisioning, using group\_vars  
> identified automatically according to the tag\_NAME\_VALUE convention to  
> dictate how the plays are run.

See how you can include your configuration playbook at the end of your  
provisioning playbook with the "add\_host" trick, as you can do this all in  
one pass.

> I've just changed this process so the launching and configuration happens  
> all in the same set of playbooks, i.e. there's no break in between these  
> playbooks running. With this done, it seems the group vars matching the new  
> instance's tags are not applied automatically - possibly because these are  
> evaluated by the dynamic inventory script when the overarching play begins,  
> and not as new items enter the inventory?

Inventory script gets evaluated at inventory host time, so it's always a  
good idea to use "add\_host" to explicitly add them in memory to those  
specific groups.

---

<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:** [September 5, 2014, 7:37pm UTC](https://forum.ansible.com/t/are-group-vars-re-evaluated-for-freshly-provisioned-ec2-instances-with-tags-matching-group-names/16126/3 "2014-09-05T19:37:34Z")

</div>

> Inventory script gets evaluated at inventory host time, so it's always  
> a good idea to use "add\_host" to explicitly add them in memory to  
> those specific groups.

Is there a way to add variables for them, too? For example, if you want to  
use inventory variables like ec2\_tag\_\<something\> in your tasks; those  
don't seem to exist for hosts that you add with add\_host (perhaps  
unsurprisingly).

If there were a task you could run to explicitly reloads the inventory,  
that might be another way to do this.

&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\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 5, 2014, 7:41pm UTC](https://forum.ansible.com/t/are-group-vars-re-evaluated-for-freshly-provisioned-ec2-instances-with-tags-matching-group-names/16126/4 "2014-09-05T19:41:57Z")

</div>

What version of ansible are you running?

---

<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:** [September 5, 2014, 7:43pm UTC](https://forum.ansible.com/t/are-group-vars-re-evaluated-for-freshly-provisioned-ec2-instances-with-tags-matching-group-names/16126/5 "2014-09-05T19:43:21Z")

</div>

> What version of ansible are you running?

1.6.6 at work, 1.7.1 at home.

&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\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 5, 2014, 7:44pm UTC](https://forum.ansible.com/t/are-group-vars-re-evaluated-for-freshly-provisioned-ec2-instances-with-tags-matching-group-names/16126/6 "2014-09-05T19:44:57Z")

</div>

What version are you running when you are testing that group\_vars/ is not available to hosts added with the add\_host module?

---

<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:** [September 5, 2014, 7:49pm UTC](https://forum.ansible.com/t/are-group-vars-re-evaluated-for-freshly-provisioned-ec2-instances-with-tags-matching-group-names/16126/7 "2014-09-05T19:49:33Z")

</div>

> What version are you running when you are testing that group\_vars/\<foo\> is  
> not available to hosts added with the add\_host module?

1.7.1 -- but it's not that variables from group\_vars aren't available,  
it's that host-specific ec2\_tag\_\* variables that come from the ec2.py  
dynamic inventory script aren't available.

&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:** [September 5, 2014, 7:54pm UTC](https://forum.ansible.com/t/are-group-vars-re-evaluated-for-freshly-provisioned-ec2-instances-with-tags-matching-group-names/16126/8 "2014-09-05T19:54:26Z")

</div>

> 1.7.1 -- but it's not that variables from group\_vars aren't  
> available, it's that host-specific ec2\_tag\_\* variables that come from  
> the ec2.py dynamic inventory script aren't available.

Ah, sorry, I now see that this thread was in fact originally about  
group\_vars. I segued off into host-specific inventory variables, and maybe  
wasn't clear enough that I was doing that, sorry for the confusion.

&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:** [September 5, 2014, 8:29pm UTC](https://forum.ansible.com/t/are-group-vars-re-evaluated-for-freshly-provisioned-ec2-instances-with-tags-matching-group-names/16126/9 "2014-09-05T20:29:31Z")

</div>

You can pass variables into add\_host, so if you have the information  
you need you can pass it along with the name you need. For instance:

&nbsp;&nbsp;add\_host:  
&nbsp;&nbsp;&nbsp;&nbsp;name: blah  
&nbsp;&nbsp;&nbsp;&nbsp;groups: db  
&nbsp;&nbsp;&nbsp;&nbsp;ec2\_tag\_Name: blah  
&nbsp;&nbsp;&nbsp;&nbsp;ec2\_tag\_Environment: staging

I believe they will be considered inventory host\_vars (and not  
group\_vars) but that seems to be what you want, right?

---

<div class="post-metadata">

**Author:** ![Craig\_Marvelley](https://avatars.discourse-cdn.com/v4/letter/c/d26b3c/32.png) [@Craig\_Marvelley](https://forum.ansible.com/u/Craig_Marvelley)\
**Post date:** [September 5, 2014, 9:02pm UTC](https://forum.ansible.com/t/are-group-vars-re-evaluated-for-freshly-provisioned-ec2-instances-with-tags-matching-group-names/16126/10 "2014-09-05T21:02:11Z")

</div>

Ok, that’s fine - tried adding the hosts to the appropriate tag groups after the provisioning run and it seems the group\_vars are being applied now, which is great-just wanted to make sure there wasn’t a way to reevaluate everything automatically. Thanks for the advice!

---

<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:** [September 12, 2014, 12:11pm UTC](https://forum.ansible.com/t/are-group-vars-re-evaluated-for-freshly-provisioned-ec2-instances-with-tags-matching-group-names/16126/11 "2014-09-12T12:11:02Z")

</div>

> You can pass variables into add\_host, so if you have the information  
> you need you can pass it along with the name you need. For instance:
> 
> &nbsp;&nbsp;add\_host:  
> &nbsp;&nbsp;&nbsp;&nbsp;name: blah  
> &nbsp;&nbsp;&nbsp;&nbsp;groups: db  
> &nbsp;&nbsp;&nbsp;&nbsp;ec2\_tag\_Name: blah  
> &nbsp;&nbsp;&nbsp;&nbsp;ec2\_tag\_Environment: staging
> 
> I believe they will be considered inventory host\_vars (and not  
> group\_vars) but that seems to be what you want, right?

I meant to follow up to say: I tried this, and it worked perfectly. Thanks!

&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:** ![Sassy\_Natan](https://avatars.discourse-cdn.com/v4/letter/s/ee59a6/32.png) [@Sassy\_Natan](https://forum.ansible.com/u/Sassy_Natan)\
**Post date:** [June 7, 2015, 3:14pm UTC](https://forum.ansible.com/t/are-group-vars-re-evaluated-for-freshly-provisioned-ec2-instances-with-tags-matching-group-names/16126/12 "2015-06-07T15:14:49Z")

</div>

I have the same issue, and I didn’t manage to solve it.

here is an example from my playbook:

- name: add newly provisioned servers to a group linux\_dsm  
local\_action: add\_host hostname={{item.private\_ip}} groupname=xxx ec2\_tag\_Group=xxx  
with\_items: ec2\_linux\_dsm.instances

I have the tag\_Group\_xxx exists in the group\_vars folder - but this is no applied.

any idea?
