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