# \[Dynamic Inventory\] ec2\_tag\_Name is undefined

**URL:** <https://forum.ansible.com/t/dynamic-inventory-ec2-tag-name-is-undefined/18109>\
**Category:** Ansible Project\
**Created:** [April 28, 2015, 5:03pm UTC](https://forum.ansible.com/t/dynamic-inventory-ec2-tag-name-is-undefined/18109 "2015-04-28T17:03:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mateus\_Dubiela\_Olive](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@Mateus\_Dubiela\_Olive](https://forum.ansible.com/u/Mateus_Dubiela_Olive)\
**Post date:** [April 28, 2015, 5:03pm UTC](https://forum.ansible.com/t/dynamic-inventory-ec2-tag-name-is-undefined/18109/1 "2015-04-28T17:03:33Z")

</div>

**TL;DR:** When I create a instance the **ec2\_\*** variables are undefined.

Hello,

I’m using ansible to create instances and using the **ec2\_tag\_Name** to set up the machine’s hostname. The problem is, on the first run the playbook crashes saying that the ‘ **ec2\_tag\_Name** ’ variable is undefinded, which is odd, since it should be automagicly defined by the **ec2.py** script. I’ve tried to run [$ ./ec2.py --refresh-cache]after the machine’s creation but with no luck.

My current workaround is to simply run the playbook twice, the variable is defined on the second run.

[Here](https://gist.github.com/mateusduboli/ca9ebaf6637ded97b46c) is a example playbook with this behaviour.

Thank you!

---

<div class="post-metadata">

**Author:** ![Brenda\_Bell](https://avatars.discourse-cdn.com/v4/letter/b/6bbea6/32.png) [@Brenda\_Bell](https://forum.ansible.com/u/Brenda_Bell)\
**Post date:** [April 29, 2015, 1:32am UTC](https://forum.ansible.com/t/dynamic-inventory-ec2-tag-name-is-undefined/18109/2 "2015-04-29T01:32:26Z")

</div>

For starters, you have a typo in your add\_host task: publid\_dns\_name should be public\_dns\_name.

When you run your playbook the first time, there _is_ no inventory. If you want to be able to access your new instances from the same playbook that creates them, you’ll need to reference them by either the registered variable (ec2) or the group you’re adding them to (my\_cool\_instance\_group).

Dynamic inventory isn’t dynamically modified as you create instances. It simply populates your inventory when the playbook starts. That’s the inventory you have to work with until the playbook is complete and you refresh the cache.

---

<div class="post-metadata">

**Author:** ![Mateus\_Dubiela\_Olive](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@Mateus\_Dubiela\_Olive](https://forum.ansible.com/u/Mateus_Dubiela_Olive)\
**Post date:** [April 30, 2015, 4:49pm UTC](https://forum.ansible.com/t/dynamic-inventory-ec2-tag-name-is-undefined/18109/3 "2015-04-30T16:49:17Z")

</div>

I get it now, I need to use the dynamic inventory in order to use it’s variables.

Thanks for the reply!
