# variable interpolation in hostvars

**URL:** <https://forum.ansible.com/t/variable-interpolation-in-hostvars/15683>\
**Category:** Ansible Project\
**Created:** [July 16, 2014, 8:40pm UTC](https://forum.ansible.com/t/variable-interpolation-in-hostvars/15683 "2014-07-16T20:40:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_Peterson](https://avatars.discourse-cdn.com/v4/letter/m/3e96dc/32.png) [@Matt\_Peterson](https://forum.ansible.com/u/Matt_Peterson)\
**Post date:** [July 16, 2014, 8:40pm UTC](https://forum.ansible.com/t/variable-interpolation-in-hostvars/15683/1 "2014-07-16T20:40:09Z")

</div>

Given the following example:

foo: “y”  
boo: “x is {{ foo }}”

This will renders in a template “x is y” (when referenced as {{ boo }}). However when called as hostvars[host][‘boo’] this renders as “x is {{ foo }}”. From what I can tell this is an interpolation or merging problem. I’ve tried different YAML formatting options to no avail. Appreciate any help or direction to share, thank you.

–Matt

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [July 16, 2014, 8:48pm UTC](https://forum.ansible.com/t/variable-interpolation-in-hostvars/15683/2 "2014-07-16T20:48:17Z")

</div>

the reason it 'works' in some places is because there is double  
interpolation in some directives and templates, when accessing the value  
directly you CORRECTLY get "x is {{ foo }}", YAML will NOT evaluate this on  
loading, this evaluates in 'templated' directives and templates using  
jinja2.

---

<div class="post-metadata">

**Author:** ![Tomasz\_Kontusz](https://avatars.discourse-cdn.com/v4/letter/t/bc79bd/32.png) [@Tomasz\_Kontusz](https://forum.ansible.com/u/Tomasz_Kontusz)\
**Post date:** [July 16, 2014, 9:01pm UTC](https://forum.ansible.com/t/variable-interpolation-in-hostvars/15683/3 "2014-07-16T21:01:57Z")

</div>

Nothing gets templated when accessed through hostvars, see (disclaimer: I’ve opened this issue).

---

<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:** [July 16, 2014, 11:51pm UTC](https://forum.ansible.com/t/variable-interpolation-in-hostvars/15683/4 "2014-07-16T23:51:24Z")

</div>

Note that comments on old tickets are not something that will get red, there are simply too many posts of this nature.

Original ticket was rather unclear about what it was about, so it got closed.

To be honest, nested variable evaluation in hostvars is a can of worms to resolve and I don’t think it will be a thing.

---

<div class="post-metadata">

**Author:** ![Tomasz\_Kontusz](https://avatars.discourse-cdn.com/v4/letter/t/bc79bd/32.png) [@Tomasz\_Kontusz](https://forum.ansible.com/u/Tomasz_Kontusz)\
**Post date:** [July 17, 2014, 6:06am UTC](https://forum.ansible.com/t/variable-interpolation-in-hostvars/15683/5 "2014-07-17T06:06:07Z")

</div>

Michael DeHaan \<michael@ansible.com\> napisał:

> To be honest, nested variable evaluation in hostvars is a can of worms  
> to  
> resolve and I don't think it will be a thing.

Can you point to any specific problems? I'm using a filter that just resolves host's vars in the context of hostvars[host], and it seems to work ok

---

<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:** [July 17, 2014, 12:29pm UTC](https://forum.ansible.com/t/variable-interpolation-in-hostvars/15683/6 "2014-07-17T12:29:45Z")

</div>

I could be very very wrong about my assumption.

Can we see the source to your filter? It might be instructive about what this might take.

---

<div class="post-metadata">

**Author:** ![Tomasz\_Kontusz](https://avatars.discourse-cdn.com/v4/letter/t/bc79bd/32.png) [@Tomasz\_Kontusz](https://forum.ansible.com/u/Tomasz_Kontusz)\
**Post date:** [July 17, 2014, 8:34pm UTC](https://forum.ansible.com/t/variable-interpolation-in-hostvars/15683/7 "2014-07-17T20:34:32Z")

</div>

Dnia czw, 17 lip 2014, 14:29:45 Michael DeHaan pisze:

> I could be very very wrong about my assumption.
> 
> Can we see the source to your filter? It might be instructive about  
> what this might take.

I kind of hope you are, as it would be really handy to have this in Ansible core 😉

My filter is attached. I'm afraid it ignores variables set with Jinja2 in current template (as it only looks at the context with "basedir" attribute), and I'm not sure about role's and play's vars. So far I've only used it to access variables that could be resolved without even looking at playbook, only with knowledge from inventory and {group,host}\_vars.  
Example usage: {{ ('other\_host' | get\_hostvars).other\_hosts\_variable }}

> **(attachments)**
>
> [hostvars.py](https://forum.ansible.com/uploads/short-url/vCj83w64ktWW9pJZX9MO1ddiH6Y.py) (661 Bytes)

---

<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:** [July 18, 2014, 1:41am UTC](https://forum.ansible.com/t/variable-interpolation-in-hostvars/15683/8 "2014-07-18T01:41:05Z")

</div>

Not too complicated.

I’d worry abit about the efficiency as this would need to be calculated a fair amount, but it’s something we can look into.

Note there is something of a backlog, but I’d file a ticket with info if not already and we can explore it.
