New to Ansible - need to pull hostname and ip from facts for template.
222 views
Skip to first unread message
Owen Corcoran
unread,
May 1, 2017, 4:18:24 PM5/1/17
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Ansible Project
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
Dick Davies
unread,
May 2, 2017, 3:21:23 AM5/2/17
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to ansible list
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 %}