Ansible loop variable

209 views
Skip to first unread message

Kiran Kumar

unread,
Apr 25, 2017, 7:19:44 PM4/25/17
to Ansible Project

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 
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

  - 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.

Kai Stian Olstad

unread,
Apr 26, 2017, 3:15:13 AM4/26/17
to ansible...@googlegroups.com
On 26.04.2017 01:19, Kiran Kumar wrote:
> 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:
> 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
>
>
> - 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

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


> 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:
docker_inv:
- host: docker1
ip: 1.2.3.4
nm: 255.255.255.1
gw: 1.2.3.1
- host: docker2
ip: 1.2.3.5
nm: 255.255.255.2
gw: 1.2.3.1

- name: Generating docker network Files
template: src=templates/docker_interface
dest=/etc/sysconfig/network-scripts/ifcfg-{{ item.host }}
with_items: '{{ docker_inv }}'

template
--------
DEVICE={{ item.host }}
BONDING_OPTS="miimon=100 mode=1"
ONPARENT=yes
BOOTPROTO=static
IPADDR={{ item.ip }}
NETMASK={{ item.nm }}
GATEWAY={{ item.gw }}

--
Kai Stian Olstad

Kiran Kumar

unread,
Apr 26, 2017, 4:50:07 PM4/26/17
to Ansible Project, ansible-pr...@olstad.com

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.


- name: Get number of Containers (Number of logical drives excluding the OS)
shell: echo `/sbin/hpssacli ctrl all show config | grep logical | wc -l` -1 | bc
register: container_count


 - 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={{ container_count.stdout }} stride=1


template

DEVICE=docker{{ item }}
BONDING_OPTS="miimon=100 mode=1"
ONPARENT=yes
BOOTPROTO=static
IPADDR={{ ipaddress }}
NETMASK={{ netmask }}
GATEWAY={{ gateway }}

Can you help me with this one
Reply all
Reply to author
Forward
0 new messages