# Ansible loop variable

**URL:** <https://forum.ansible.com/t/ansible-loop-variable/24317>\
**Category:** Ansible Project\
**Created:** [April 25, 2017, 11:19pm UTC](https://forum.ansible.com/t/ansible-loop-variable/24317 "2017-04-25T23:19:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kiran\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/k/ac8455/32.png) [@Kiran\_Kumar1](https://forum.ansible.com/u/Kiran_Kumar1)\
**Post date:** [April 25, 2017, 11:19pm UTC](https://forum.ansible.com/t/ansible-loop-variable/24317/1 "2017-04-25T23:19:44Z")

</div>

Hello friends,

Am having a challenge finding a solution to below issue.  
I need to generate multiple network files based on single template and the value of template should change based on the item like IP/NM/GW .

Example

```auto
vars:
  docker1ip: 1.2.3.4
  docker1nm: 255.255.255.1
  docker1gw: 1.2.3.1
  docker2ip: 1.2.3.5
  docker2nm: 255.255.255.2
  docker2gw: 1.2.3.1

```

```auto

```

- name: Generating docker network Files  
template: src=templates/docker\_interface dest=/etc/sysconfig/network-scripts/ifcfg-docker{{ item }}

vars:  
ipaddress: {{ docker{{ item }}ip }}  
netmask: {{ docker{{ item }}nm }}  
gateway: {{ docker{{ item }}gw }}  
with\_sequence: start=1 end=2 stride=1

template

DEVICE=docker{{ item }}  
BONDING\_OPTS=“miimon=100 mode=1”  
ONPARENT=yes  
BOOTPROTO=static  
IPADDR={{ ipaddress }}  
NETMASK={{ netmask }}  
GATEWAY={{ gateway }}

Above task doesnt work but I need a solution it may be a different approach too.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [April 26, 2017, 7:14am UTC](https://forum.ansible.com/t/ansible-loop-variable/24317/2 "2017-04-26T07:14:57Z")

</div>

> Am having a challenge finding a solution to below issue.  
> I need to generate multiple network files based on single template and the  
> value of template should change based on the item like IP/NM/GW .
> 
> Example
> 
> vars:  
> &nbsp;&nbsp;docker1ip: 1.2.3.4  
> &nbsp;&nbsp;docker1nm: 255.255.255.1  
> &nbsp;&nbsp;docker1gw: 1.2.3.1  
> &nbsp;&nbsp;docker2ip: 1.2.3.5  
> &nbsp;&nbsp;docker2nm: 255.255.255.2  
> &nbsp;&nbsp;docker2gw: 1.2.3.1
> 
> &nbsp;&nbsp;- name: Generating docker network Files  
> &nbsp;&nbsp;&nbsp;&nbsp;template: src=templates/docker\_interface  
> dest=/etc/sysconfig/network-scripts/ifcfg-docker{{ item }}  
> &nbsp;&nbsp;&nbsp;&nbsp;vars:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ipaddress: {{ docker{{ item }}ip }}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;netmask: {{ docker{{ item }}nm }}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gateway: {{ docker{{ item }}gw }}  
> &nbsp;&nbsp;&nbsp;&nbsp;with\_sequence: start=1 end=2 stride=1

Variables in variables is not possible like this, you need to do it like this  
[https://docs.ansible.com/ansible/faq.html#when-should-i-use-also-how-to-interpolate-variables-or-dynamic-variable-names](https://docs.ansible.com/ansible/faq.html#when-should-i-use-also-how-to-interpolate-variables-or-dynamic-variable-names)

> template
> 
> DEVICE=docker{{ item }}  
> BONDING\_OPTS="miimon=100 mode=1"  
> ONPARENT=yes  
> BOOTPROTO=static  
> IPADDR={{ ipaddress }}  
> NETMASK={{ netmask }}  
> GATEWAY={{ gateway }}
> 
> Above task doesnt work but I need a solution it may be a different approach  
> too.

Since you are specifying everything why not restructure the variable, one example on how to do it:

vars:  
&nbsp;&nbsp;&nbsp;docker\_inv:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- host: docker1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ip: 1.2.3.4  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nm: 255.255.255.1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gw: 1.2.3.1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- host: docker2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ip: 1.2.3.5  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nm: 255.255.255.2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gw: 1.2.3.1

&nbsp;&nbsp;&nbsp;- name: Generating docker network Files  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;template: src=templates/docker\_interface dest=/etc/sysconfig/network-scripts/ifcfg-{{ item.host }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: '{{ docker\_inv }}'

template

---

<div class="post-metadata">

**Author:** ![Kiran\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/k/ac8455/32.png) [@Kiran\_Kumar1](https://forum.ansible.com/u/Kiran_Kumar1)\
**Post date:** [April 26, 2017, 8:50pm UTC](https://forum.ansible.com/t/ansible-loop-variable/24317/3 "2017-04-26T20:50:07Z")

</div>

HI Kai Stian Olstad ,

That helped me and thanks a ton but that scenario works better if we know the number of docker networks.  
Here is the exact use case where I need to generate multiple docker network files based on the number of container count and push a single template multiple times to match the container count updating the correct network details. I would get docker1ip/nm/gw docker2ip/nm/gw dockernip/nm/gw from external source and just for testing i mentioned them as vars in this playbook.
