# Loops in Variable Files

**URL:** <https://forum.ansible.com/t/loops-in-variable-files/17635>\
**Category:** Ansible Project\
**Tags:** galaxy-ng\
**Created:** [February 27, 2015, 11:39am UTC](https://forum.ansible.com/t/loops-in-variable-files/17635 "2015-02-27T11:39:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kelvin\_Nicholson](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kelvin\_Nicholson](https://forum.ansible.com/u/Kelvin_Nicholson)\
**Post date:** [February 27, 2015, 11:39am UTC](https://forum.ansible.com/t/loops-in-variable-files/17635/1 "2015-02-27T11:39:39Z")

</div>

Hi,

I’m wishing to use various Roles from Galaxy, and they often require configuration via variables.

My question is: is it possible to dynamically build these files?

For instance, let’s say I want to configure a few backend servers for HAProxy. The variables typically look like this:

`  
haproxy\_backends:

- name: ‘be-mysupersite’  
description: ‘mysupersite is really cool’  
servers:
- name: “be-mysupersite-01”  
ip: “123.123.123.124”  
port: 8080
- name: “be-mysupersite-02”  
ip: “123.123.123.125”  
port: 8080
- name: “be-mysupersite-03”  
ip: “123.123.123.126”  
port: 8080  
`

What I’m hoping to do is build this with loops, for example, something like this:

`

haproxy\_backends:

- name: ‘be-mysupersite’  
description: ‘mysupersite is really cool’  
servers:

- name: “{{ item.name }}”  
ip: “{{ item.ip }}”  
port: 8080  
with\_items:

- { name: ‘be-mysupersite-01’, ip: ‘123.123.123.124’ }

- { name: ‘be-mysupersite-02’, ip: ‘123.123.123.125’ }

- { name: ‘be-mysupersite-03’, ip: ‘123.123.123.126’ }

`

The config file ends up with something like {# item.name #} instead of the actual name.

Can anybody point me in the right direction?

Much appreciated,

Kelvin

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [March 4, 2015, 2:56pm UTC](https://forum.ansible.com/t/loops-in-variable-files/17635/2 "2015-03-04T14:56:27Z")

</div>

var files do not handle ansible directives, but yaml allows for some references

\_default\_server:

haproxy\_backends:  
&nbsp;&nbsp;- name: 'be-mysupersite'  
&nbsp;&nbsp;&nbsp;&nbsp;description: 'mysupersite is really cool'  
&nbsp;&nbsp;&nbsp;&nbsp;servers:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: "be-mysupersite-01"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ip: "123.123.123.124"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;port: 8080  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: "be-mysupersite-02"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ip: "123.123.123.125"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;port: 8080  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: "be-mysupersite-03"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ip: "123.123.123.126"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;port: 8080
