Reputation: 75
Hi Guys would anyone be able to advise how I can format a variable within square brackets and single quotes.
Im using inventory groups but wish to have a single var that I can change and be referenced ie
_devices:
- webservers
and I would like the var _devices to be used within the following
`groups[' ']`
so I just have a single entry to change and not the whole play. Struggling to escape "{{ _devices }}"
with [' ']
ie
groups['"{{_ devices }}"']
{% for host in groups['webservers'] %}
{{ hostvars[host][‘ansible_host’] }}
{% endfor %}
Upvotes: 0
Views: 3882
Reputation: 33231
You are conflating a string literal with a variable reference; anytime one finds themselves trying to use nested jinja2 mustaches, it's a bug
Your talk of []
means you are expecting devices
to in fact be a list of group names, in which case you'll want the | extract
filter to apply [groups[g_name] for g_name in devices]
(regrettably jinja2 does not understand list comprehensions like that). Then, because each extract
run will itself be a list[str]
, meaning the output will be list[list[str]]
, but you want just list[str]
, you'll apply the |flatten
to fold them up to one list
- debug:
msg: >-
all hosts in devices are: {{ devices | map("extract", groups) | flatten | list }}
vars:
devices:
- webservers
Upvotes: 1