Reputation: 987
I have the following Ansible project structure:
├── demo.yml
├── hosts
├── group_vars
│ └── all
└── roles
├── common
│ ├── tasks
│ │ └── main.yml
│ └── templates
│ └── init.j2
Inside 'hosts', I have:
[primary]
server1
[secondary]
server2
In roles/common/templates/init.j2, I want to be able to refer to the [primary] group variable. Since Ansible uses Jinja2 for its template module. I was directed to this Jinja2 doc.
I tried:
print("{{ group['primary'] }}")
But it will return:
['server1']
Right now I can only get it within a loop:
{% for host in groups['primary'] %}
print("{{ host }}")
{% endfor %}
It will return what I want:
server1
But how do I get this result without using a loop?
Upvotes: 4
Views: 7394
Reputation: 4578
Try this...
groups['primary'][0]
or just print group and you should be able to see how the data is store.
Hope this helps!
Upvotes: 4