There is much to say. First, try to avoid lineinfile like the plague. This is really the last decision. lineinfile makes it difficult to record serial and idempotent players.
Now, since you are trying to populate RH style interface files, this is pretty easy to do.
Organize your variables
The first thing to do is create the right structure for your variables. You will want to focus on your interfaces so that you do loopable. interface1 , interface2 ... interfaceN not scalable, as you mentioned.
Here is a suggestion:
interfaces_ipv4: - name: eth0 bootproto: static ipaddress: 192.168.211.249 netmask: 255.255.255.0 gateway: 192.168.211.2 dns: 192.168.211.2 - name: eth2 bootproto: static ipaddress: 10.0.0.100 netmask: 255.0.0.0
Write your template
Now that you have the data, you need a template to create the OS configuration file.
BOOTPROTO={{item.bootproto}} IPADDR={{item.ipaddress}} NETMASK={{item.netmask}} {% if item.gateway is defined %} GATEWAY={{item.gateway}} {% endif %} PEERDNS=no DNS1={{item.dns}} ONBOOT={{item.onboot|default('no')}}
I have included two options: you can skip the output of the line when it is not installed ( {% if ... %} construct) or provide default values (for example, {{item.onboot|default('no')}} )
Your mileage may expire, depending on whether you want to use the default value or skip with the if construct.
Create task
Finally, here is the task that will create the interface configuration files for each interface:
- name: Push template template: src=/path/to/the/above/template.j2 dest=/etc/sysconfig/network-scripts/ifcfg-{{item.name}}.cfg with_items: - "{{ interfaces_ipv4 }}"
That should do it all.
Of course, the best way to use this task is to add it to some "network" role and call it from the textbook.
Good luck.
source share