# accessing a fact in a template

**URL:** <https://forum.ansible.com/t/accessing-a-fact-in-a-template/11896>\
**Category:** Ansible Project\
**Created:** [September 15, 2012, 1:17pm UTC](https://forum.ansible.com/t/accessing-a-fact-in-a-template/11896 "2012-09-15T13:17:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ali\_Asad\_Lotia](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@Ali\_Asad\_Lotia](https://forum.ansible.com/u/Ali_Asad_Lotia)\
**Post date:** [September 15, 2012, 1:17pm UTC](https://forum.ansible.com/t/accessing-a-fact-in-a-template/11896/1 "2012-09-15T13:17:24Z")

</div>

Greetings All,  
Thanks so much for creating such a fantastic configuration management  
system. After working with Ansible and getting results are quickly as  
I have, managing infrastructure is fun again!

I'm at a bit of a loss on how to access a fact for the ethernet  
interface 1:1 (eth1:1) in a jinja template. The following gist  
[https://gist.github.com/3727009](https://gist.github.com/3727009) has a santised facts list for one of  
the hosts I'm managing. I'm trying to refer to  
ansible\_eth1:1.ipv4.address in a template. The relevant section in the  
gist starts on line 75 and the property I'm trying to access is on  
line 78. Any help on the syntax required to access that fact property  
in a template would be much appreciated.

Thank you,  
Ali

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [September 15, 2012, 3:07pm UTC](https://forum.ansible.com/t/accessing-a-fact-in-a-template/11896/2 "2012-09-15T15:07:07Z")

</div>

Since the fact name has a ":" in it, I'm guessing it doesn't make a  
variable name that Jinja2 will let you access.

A workaround would be to go through hostvars:

{{ hostvars[$inventory\_hostname][;ansible\_eth1:1']['ipv4']['address'] }}

We probably should rename facts that have non-legal-variable names,  
though this would break some existing templates, so I'm not sure I  
want to.

It might be cleaner to offer a function like

lookup('fact\_name') that returns  
hostvars[$inventory\_hostname]['fact\_name'] or similar.

It would be nice if this was available to both Jinja2 and the basic templating.

I'm a bit busy at the moment, someone want to take this on, perhaps?
