Strong loop over variables

I use the newly added network board configuration file for updating, for this I have defined some variables in a separate yml file

/tmp/ip.yml

#first interface interface1: eth1 bootproto1: static ipaddress1: 192.168.211.249 netmask1: 255.255.255.0 gateway: 192.168.211.2 DNS1: 192.168.211.2 #second interface interface2: eth2 bootproto2: static ipaddress2: 10.0.0.100 netmask2: 255.0.0.0 

Playbook

 - include_vars: /tmp/ip.yml - name: configuring interface lineinfile: state=present create=yes dest=/etc/sysconfig/network-scripts/ifcfg-{{interface1}} regexp="{{ item.regexp }}" line="{{ item.line }}" with_items: - { regexp: '^BOOTPROTO=.*', line: 'BOOTPROTO={{interface1}}' } - { regexp: '^IPADDR=.*', line: 'IPADDR={{ipaddress1}' } - { regexp: '^NETMASK=.*', line: 'NETMASK={{netmask1}}' } - { regexp: '^GATEWAY=.*', line: 'GATEWAY={{gateway}}' } - { regexp: '^PEERDNS=.*', line: 'PEERDNS=no' } - { regexp: '^DNS1=.*', line: 'DNS1={{DNS1}}' } - { regexp: '^ONBOOT=.*', line: 'ONBOOT={{onboot}}' } when: bootproto1 == 'static' - name: configuring for DHCP lineinfile: state=present create=yes dest=/etc/sysconfig/network-scripts/ifcfg-{{interface1}} regexp="{{ item.regexp }}" line="{{ item.line }}" with_items: - { regexp: '^BOOTPROTO=.*',line: 'BOOTPROTO={{bootproto1}}' } - {regexp: '^PEERDNS=.*',line: 'PEERDNS=yes' } - { regexp: '^ONBOOT=.*', line: 'ONBOOT={{onboot}}' } when: bootproto1 == 'dhcp' 

repeats similarly for the second interface.

Even if this method works for 2 network adapters, it is too difficult to manage, that is, for each newly added network adapter, I need to change the playbook and update the corresponding variable in /tmp/ip.yml .

Is there a way to add variables to /tmp/ip.yml and can use some kind of separator to make it out on the playbook with a change in the playlist every time to connect a new network card.

+5
source share
1 answer

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.

+20
source

Source: https://habr.com/ru/post/1205683/