How to reference host variable in Ansible template?
I have the following Ansible project structure:
โโโ demo.yml
โโโ hosts
โโโ group_vars
โ โโโ all
โโโ roles
โโโ common
โ โโโ tasks
โ โ โโโ main.yml
โ โโโ templates
โ โโโ init.j2
Inside the "hosts" I have:
[primary]
server1
[secondary]
server2
Casting /common/templates/init.j2 I want to be able to reference the [primary] group variable . As Ansible uses Jinja2 for its module template . I was directed to this Jinja2 doc .
I tried:
print("{{ group['primary'] }}")
But it will return:
['server1']
Right now I can only get it in a loop:
{% for host in groups['primary'] %}
print("{{ host }}")
{% endfor %}
It will return what I want:
server1
But how do you get this result without using a loop?
+3
source to share