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