patternyamlMinor
Ansible - Variable expansion inside hostvars declaration?
Viewed 0 times
expansiondeclarationhostvarsvariableansibleinside
Problem
I have a hardcoded hostvars line, like so:
I have a
Thanks.
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.
Solution
No, you cannot nest jinja2 expansion i.e. the following won't work:
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
# 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 }}"
Context
StackExchange DevOps Q#13618, answer score: 3
Revisions (0)
No revisions yet.