Are group_vars re-evaluated for freshly provisioned EC2 instances with tags matching group names?

166 views
Skip to first unread message

Craig Marvelley

unread,
Sep 5, 2014, 10:33:30 AM9/5/14
to ansible...@googlegroups.com
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?

Michael DeHaan

unread,
Sep 5, 2014, 3:30:14 PM9/5/14
to ansible...@googlegroups.com
On Fri, Sep 5, 2014 at 10:33 AM, Craig Marvelley <craig.m...@gmail.com> wrote:
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.
 

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?

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/108e4ebf-159b-48ba-a40d-6026afc3413d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Josh Smift

unread,
Sep 5, 2014, 3:37:49 PM9/5/14
to ansible...@googlegroups.com
MD> Inventory script gets evaluated at inventory host time, so it's always
MD> a good idea to use "add_host" to explicitly add them in memory to
MD> 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.

-Josh (j...@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.

Michael DeHaan

unread,
Sep 5, 2014, 3:42:00 PM9/5/14
to ansible...@googlegroups.com
What version of ansible are you running?




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

Josh Smift

unread,
Sep 5, 2014, 3:43:32 PM9/5/14
to ansible...@googlegroups.com
MD> What version of ansible are you running?

1.6.6 at work, 1.7.1 at home.

Michael DeHaan

unread,
Sep 5, 2014, 3:45:00 PM9/5/14
to ansible...@googlegroups.com
What version are you running when you are testing that group_vars/<foo> is not available to hosts added with the add_host module?




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

Josh Smift

unread,
Sep 5, 2014, 3:49:46 PM9/5/14
to ansible...@googlegroups.com
MD> What version are you running when you are testing that group_vars/<foo> is
MD> 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.

Josh Smift

unread,
Sep 5, 2014, 3:54:49 PM9/5/14
to ansible...@googlegroups.com
JBS> 1.7.1 -- but it's not that variables from group_vars aren't
JBS> available, it's that host-specific ec2_tag_* variables that come from
JBS> 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.

Michael Peters

unread,
Sep 5, 2014, 4:29:41 PM9/5/14
to ansible...@googlegroups.com
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:

add_host:
name: blah
groups: db
ec2_tag_Name: blah
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?
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/21514.5362.813995.167768%40gargle.gargle.HOWL.

Craig Marvelley

unread,
Sep 5, 2014, 5:02:18 PM9/5/14
to ansible...@googlegroups.com
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!
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/gi7S1P3Z8Ew/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.

Josh Smift

unread,
Sep 12, 2014, 8:11:21 AM9/12/14
to ansible...@googlegroups.com
MP> You can pass variables into add_host, so if you have the information
MP> you need you can pass it along with the name you need. For instance:
MP>
MP> add_host:
MP> name: blah
MP> groups: db
MP> ec2_tag_Name: blah
MP> ec2_tag_Environment: staging
MP>
MP> I believe they will be considered inventory host_vars (and not
MP> 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!

Sassy Natan

unread,
Jun 7, 2015, 5:21:19 PM6/7/15
to ansible...@googlegroups.com, j...@care.com
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?
Reply all
Reply to author
Forward
0 new messages