New to Ansible - need to pull hostname and ip from facts for template.

Hey all,

Im trying to pull the hostname and ip of the hosts in a specific host group from the facts gathered when the ansibe playbook runs to gather it facts.

Im trying to use the hostname and ip for both my two target hosts to dynamically populate a haproxy config file using the jinja2 templating support

Ive got the templating working perfectly but i cannot find a way to reference these facts and interate over them within the template.

I know in the template we can do

{% for item in hosts %}
server name {{ host.name }} {{ host.ip }} check
{% endfor %}

How can i get the facts i need into either array or dictionary so i can iterate through them and populate the template.

Owen

You can iterate over the group and access hostvars['server name'].
Assuming your backends are in a group called 'backends'
something like this should work:

{% for host in groups['backends'] %}
{% set h = hostvars[host] %}
   server name {{ h.name }} {{ h.ip }} check
{% endfor %}