<p>No, you cannot nest jinja2 expansion i.e. the following <strong>won't work</strong>:</p>
<pre class="lang-yaml prettyprint-override"><code># Warning ! Those will fail !
some_var: &quot;{{ some_dict[{{ some_dynamic_key }}] }}&quot;
other_var: &quot;{{ {{ dynamicaly_prefixed }}_var }}&quot;
# Did I forget to say the above examples will fail ?
</code></pre>
<p>But you can use your var without any problem, just as in almost any other programming language dealing with list and indexes. There is actually an <a href="https://docs.ansible.com/ansible/latest/reference_appendices/faq.html#when-should-i-use-also-how-to-interpolate-variables-or-dynamic-variable-names" rel="nofollow noreferrer">ansible FAQ entry on this particular subject</a></p>
<pre class="lang-yaml prettyprint-override"><code>some_inventory_hostname: 192.168.0.162
node1_hostname: &quot;{{ hostvars[some_inventory_hostname].node1_hostname }}&quot;
</code></pre>

CC BY-SA 4.0
2021-03-27T20:02:12.743Z
Okay, understood. I just tested it and it's working with a normal variable declare at the top of the play. 

So, expansion of a variable defined via set_fact inside of hostvar does not work, yet declaring the variable the the root of the play works? Why is that? It seems that one kind of expansion works but not the other. 

Non-working expansion (with set_fact doing the declare):
https://i.imgur.com/RgXAg4s.png

Working expansion (variable declare at the top of play):
https://i.imgur.com/RgXAg4s.png
synth45
CC BY-SA 4.0
2021-03-28T08:30:55.040Z
You should define "It's not working" (error message, what you get vs what you expect...). In this case, I'm almost sure your problem is not related to using set_fact or not but to mandatory quotes in ansible when a value is a jinja2 exepression and starts with double moustaches: https://docs.ansible.com/ansible/3/user_guide/playbooks_variables.html#when-to-quote-variables-a-yaml-gotcha. I also suggest you save yourself some headaches by adopting the yaml only syntax for tasks rather than the old ini style.
Zeitounator
CC BY-SA 4.0
2021-03-28T08:52:16.883Z
In case my comment was not clear enough: https://gist.github.com/zeitounator/8c5834bf149a514441f3cdf8cd683f05
Zeitounator
CC BY-SA 4.0
2021-03-27T18:17:22.063Z
2021-03-28T22:49:41.547Z
2021-03-28T22:49:41.547Z
Zeitounator
<p>I have a hardcoded hostvars line, like so:</p>
<p><code>node1_hostname={{ hostvars['192.168.0.162']['node1_hostname'] }}</code></p>
<p>I have a <code>set_fact</code> variable from a previous play that I'd like to expand in the host section of the hostvars declaration, instead of the IP address. Can hostvars take expansion inside those brackets and single quotes?</p>
<p>Thanks.</p>

<ansible><linux><configuration-management><yaml><variables>
Ansible - Variable expansion inside hostvars declaration?
synth45
# Ansible - Variable expansion inside hostvars declaration?

(Asked by: username_0 on 2021-03-27)

I have a hardcoded hostvars line, like so:

`node1_hostname={{ hostvars['192.168.0.162']['node1_hostname'] }}`

I have a `set_fact` variable from a previous play that I'd like to expand in the host section of the hostvars declaration, instead of the IP address. Can hostvars take expansion inside those brackets and single quotes?

Thanks.

**Tags:** ansible, linux, configuration-management, yaml, variables


---

## 1 answers

### Answer by username_1 on 2021-03-27. Score: 3

No, you cannot nest jinja2 expansion i.e. the following won't work:
# Warning ! Those will fail !
some_var: "{{ some_dict[{{ some_dynamic_key }}] }}"
other_var: "{{ {{ dynamicaly_prefixed }}_var }}"
# Did I forget to say the above examples will fail ?

But you can use your var without any problem, just as in almost any other programming language dealing with list and indexes. There is actually an ansible FAQ entry on this particular subject
some_inventory_hostname: 192.168.0.162
node1_hostname: "{{ hostvars[some_inventory_hostname].node1_hostname }}"

> **Comments:**
> **username_0**: Okay, understood. I just tested it and it's working with a normal variable declare at the top of the play.   So, expansion of a variable defined via set_fact inside of hostvar does not work, yet declaring the variable the the root of the play works? Why is that? It seems that one kind of expansion works but not the other.   Non-working expansion (with set_fact doing the declare): https://i.imgur.com/RgXAg4s.png  Working expansion (variable declare at the top of play): https://i.imgur.com/RgXAg4s.png  
> **username_1**: You should define "It's not working" (error message, what you get vs what you expect...). In this case, I'm almost sure your problem is not related to using set_fact or not but to mandatory quotes in ansible when a value is a jinja2 exepression and starts with double moustaches: https://docs.ansible.com/ansible/3/user_guide/playbooks_variables.html#when-to-quote-variables-a-yaml-gotcha. I also suggest you save yourself some headaches by adopting the yaml only syntax for tasks rather than the old ini style.  
> **username_1**: In case my comment was not clear enough: https://gist.github.com/zeitounator/8c5834bf149a514441f3cdf8cd683f05  

---

