# Referencing a fact from a variable (and resolve in template)

**URL:** <https://forum.ansible.com/t/referencing-a-fact-from-a-variable-and-resolve-in-template/12454>\
**Category:** Ansible Project\
**Created:** [March 5, 2013, 12:11pm UTC](https://forum.ansible.com/t/referencing-a-fact-from-a-variable-and-resolve-in-template/12454 "2013-03-05T12:11:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michel\_Blanc1](https://avatars.discourse-cdn.com/v4/letter/m/fbc32d/32.png) [@Michel\_Blanc1](https://forum.ansible.com/u/Michel_Blanc1)\
**Post date:** [March 5, 2013, 12:11pm UTC](https://forum.ansible.com/t/referencing-a-fact-from-a-variable-and-resolve-in-template/12454/1 "2013-03-05T12:11:38Z")

</div>

Fellow ansiblers,

Is there a way to reference a gathered fact inside a variable ?

Example use case is to get the ip address of a server (coolhost) and use  
it in another host var so it can issue http requests to it.

For instance, let's say I'm building firewall rules. I need to get IP  
addresses in rules since it's loaded before the device is up

I'd like to be able to do this (excuse the line break) :

---

<div class="post-metadata">

**Author:** ![Daniel\_Hokka\_Zakriss](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Hokka\_Zakriss](https://forum.ansible.com/u/Daniel_Hokka_Zakriss)\
**Post date:** [March 5, 2013, 12:24pm UTC](https://forum.ansible.com/t/referencing-a-fact-from-a-variable-and-resolve-in-template/12454/2 "2013-03-05T12:24:01Z")

</div>

Michel Blanc wrote:

> Fellow ansiblers,
> 
> Is there a way to reference a gathered fact inside a variable ?
> 
> Example use case is to get the ip address of a server (coolhost) and use  
> it in another host var so it can issue http requests to it.
> 
> For instance, let's say I'm building firewall rules. I need to get IP  
> addresses in rules since it's loaded before the device is up
> 
> I'd like to be able to do this (excuse the line break) :
> 
> ---  
> add\_firewall\_rules:  
> &nbsp;&nbsp;&nbsp;&nbsp;- -I OUTPUT -m tcp -p tcp --dport 80 -d {{ hostvars['coolhost']  
> ['network\_interfaces\_ipv4']['eth0']['address'] }} -j DROP

Use ${hostvars.{coolhost}.network\_interfaces\_ipv4.eth0.address} and ansible  
will resolve it before Jinja2 ever sees it. Jinja2 doesn't have any way to  
do variable-referencing, so you have to it using ansible's means.

Daniel

---

<div class="post-metadata">

**Author:** ![Michel\_Blanc1](https://avatars.discourse-cdn.com/v4/letter/m/fbc32d/32.png) [@Michel\_Blanc1](https://forum.ansible.com/u/Michel_Blanc1)\
**Post date:** [March 5, 2013, 12:37pm UTC](https://forum.ansible.com/t/referencing-a-fact-from-a-variable-and-resolve-in-template/12454/3 "2013-03-05T12:37:35Z")

</div>

Oh, I didn't know Ansible was interpolating in vars too.

Very very cool, thanks Daniel !

M
