Ansible loop through group vars in template

Question:

I’m struggling with a pattern pulling inventory vars in Ansible templates, please help. 🙂

I’m setting up a monitoring server, and I want to be able to automatically provision the servers using Ansible. I’m struggling with loops in the template to allow me to this.

My semi-working soluition so far is in the playbook that calls the template task I have:

monitoringserver.yml

template.yml

But this isn’t ideal as I can’t define different IP address for different servers to be monitoring. How have other people done this? I’m sure it must be trivial but my brain’s struggling with the syntax.

edit: To clarify the resulting template looks something like this:

What I would like is the values web1/web2/db1/db2 to be different depending on whether I’m using a production inventory file or a development inventory file.

Answer:

Ideally you would be using different inventory files for production and staging, which would allow you to keep the same {{ inventory_hostname }} value, but target different machines.

You can also loop through different groups…

hosts:

playbook:

template:

Leave a Reply