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

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?

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.

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.

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

What version of ansible are you running?

What version of ansible are you running?

1.6.6 at work, 1.7.1 at home.

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

What version are you running when you are testing that group_vars/ is not available to hosts added with the add_host module?

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.

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

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.

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

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?

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 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?

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

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

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?