# Accessing ec2.py dynamic inventory variables in templates

**URL:** <https://forum.ansible.com/t/accessing-ec2-py-dynamic-inventory-variables-in-templates/15504>\
**Category:** Ansible Project\
**Created:** [June 19, 2014, 3:33pm UTC](https://forum.ansible.com/t/accessing-ec2-py-dynamic-inventory-variables-in-templates/15504 "2014-06-19T15:33:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roger\_Hunwicks](https://avatars.discourse-cdn.com/v4/letter/r/cc9497/32.png) [@Roger\_Hunwicks](https://forum.ansible.com/u/Roger_Hunwicks)\
**Post date:** [June 19, 2014, 3:33pm UTC](https://forum.ansible.com/t/accessing-ec2-py-dynamic-inventory-variables-in-templates/15504/1 "2014-06-19T15:33:04Z")

</div>

I’d like to access a variable set by the ec2.py dynamic inventory script in a Jinja2 template, but I can’t work out how to do it.

ec2.py returns a variable called ec2\_tag\_elasticbeanstalk\_environment-name for EC2 instances that are part of an Elastic Beanstalk environment:

`

ec2.py --host=[ec2-54-196-159-144.compute-1.amazonaws.com](http://ec2-54-196-159-144.compute-1.amazonaws.com)

{  
“ec2\_\_in\_monitoring\_element”: false,  
“ec2\_ami\_launch\_index”: “0”,  
“ec2\_architecture”: “x86\_64”,

…snip

“ec2\_tag\_elasticbeanstalk\_environment-name”: “production”,

“ec2\_virtualization\_type”: “paravirtual”,  
“ec2\_vpc\_id”: “”  
}

`

I’d like to access that variable in a template I’m running as part of a playbook on that host:

`

- name: Create Diamond conf file

template: src=diamond\_elasticbeanstalk.conf.j2 dest=/etc/diamond/diamond.conf

notify :

- restart diamond

`

The template contains:

`

path\_prefix = servers.{{ ec2\_tag\_elasticbeanstalk\_environment-name }}

`

I’m guess I have the wrong syntax, but I can’t find anywhere in the docs that tells me the correct approach.

Roger

---

<div class="post-metadata">

**Author:** ![Roger\_Hunwicks](https://avatars.discourse-cdn.com/v4/letter/r/cc9497/32.png) [@Roger\_Hunwicks](https://forum.ansible.com/u/Roger_Hunwicks)\
**Post date:** [June 19, 2014, 4:06pm UTC](https://forum.ansible.com/t/accessing-ec2-py-dynamic-inventory-variables-in-templates/15504/2 "2014-06-19T16:06:21Z")

</div>

After further investigation…

When the playbook runs, on that task I get:

`  
fatal: [[ec2-54-196-159-144.compute-1.amazonaws.com](http://ec2-54-196-159-144.compute-1.amazonaws.com)] =\> {‘msg’: “unsupported operand type(s) for -: ‘StrictUndefined’ and ‘StrictUndefined’”, ‘failed’: True}

`

If I define the variable in the group\_vars file I get the same error.

However, if I change the variable in both the group\_vars file and the template to ec2\_tag\_elasticbeanstalk\_environment\_name - i.e. with an underscore instead of a minus as the last separator then it works.

That implies that the - in the variable name is the issue, but I still don’t know the best way to solve it.

---

<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:** [June 19, 2014, 5:28pm UTC](https://forum.ansible.com/t/accessing-ec2-py-dynamic-inventory-variables-in-templates/15504/3 "2014-06-19T17:28:10Z")

</div>

You can pull it from the hostvars dictionary with something like

&nbsp;&nbsp;&nbsp;&nbsp;hostvars[inventory\_hostname]['ec2\_tag\_elasticbeanstalk\_environment-name']

---

<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:** [June 19, 2014, 9:30pm UTC](https://forum.ansible.com/t/accessing-ec2-py-dynamic-inventory-variables-in-templates/15504/4 "2014-06-19T21:30:26Z")

</div>

Really we should probably replace all “-” with “\_” in tag names so they can be cleanly used as variables.

But then again, the tag had a “-” in it, so I’m not sure that’s the right thing to do.

Anyway, variables can’t have “-” in them, which is why that happens, so it’s easy to fix with a convention.

---

<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:** [June 19, 2014, 9:33pm UTC](https://forum.ansible.com/t/accessing-ec2-py-dynamic-inventory-variables-in-templates/15504/5 "2014-06-19T21:33:00Z")

</div>

I thought about tackling this along with the proposed "downcase\_facts"  
option for ec2.ini. Something like "normalize\_facts" or some such so  
that anything that's not a letter or number gets converted to an  
underscore. Would default to false just like downcase\_facts.

---

<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:** [June 19, 2014, 9:35pm UTC](https://forum.ansible.com/t/accessing-ec2-py-dynamic-inventory-variables-in-templates/15504/6 "2014-06-19T21:35:24Z")

</div>

That seems reasonable to me.

---

<div class="post-metadata">

**Author:** ![Roger\_Hunwicks](https://avatars.discourse-cdn.com/v4/letter/r/cc9497/32.png) [@Roger\_Hunwicks](https://forum.ansible.com/u/Roger_Hunwicks)\
**Post date:** [June 20, 2014, 8:32am UTC](https://forum.ansible.com/t/accessing-ec2-py-dynamic-inventory-variables-in-templates/15504/7 "2014-06-20T08:32:10Z")

</div>

The hostvars approach you suggested worked fine - thanks for the advice.

I concur that adding an option to ec2.ini is a good idea. Even for those people that don’t want to use it, it will serve as a warning that they need to be aware of the difference in variable naming requirements between EC2 and Jinja2

---

<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:** [June 20, 2014, 5:47pm UTC](https://forum.ansible.com/t/accessing-ec2-py-dynamic-inventory-variables-in-templates/15504/8 "2014-06-20T17:47:21Z")

</div>

I've added normalize\_groups as part of  
[https://github.com/ansible/ansible/pull/7867](https://github.com/ansible/ansible/pull/7867)

---

<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:** [June 20, 2014, 9:03pm UTC](https://forum.ansible.com/t/accessing-ec2-py-dynamic-inventory-variables-in-templates/15504/9 "2014-06-20T21:03:44Z")

</div>

Thanks!
