Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible loop over variables

i am using ansible to update configuration file of newly added NIC for that i have defined some variables in 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'

similarly repeated for second interface.

Even Though this method works for 2 NIC,this is too difficult to manage ,that is for each new NIC added i need to modify playbook and update corresponding variable in /tmp/ip.yml.

Is there a way to add variables to /tmp/ip.yml and may be using some separator parse it to playbook with out modifying playbook each time for plugging in new NIC.

like image 954
Kevin Parker Avatar asked Oct 28 '14 10:10

Kevin Parker


1 Answers

There is a lot to say here. First, try to avoid lineinfile like plague. It is really a last-resort solution. lineinfile makes it hard to write consistent and idempotents playbooks.

Now, since you're trying to populate RH style interface files, it is quite easy to do.

Organize your variables

The first thing to do is to have a proper structure for your variables. You'll want to loop over your interfaces so you have to make stuff 'loopable'. Having interface1, interface2 ... interfaceN is 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 your data, you need a template to create your OS config 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 included two variations : you can skip outputting a line when it's not set ({% if ... %} construct) or provide default values (for instance {{item.onboot|default('no')}}).

Your mileage may vay, depending if you want to use a default or to skip with the if construct.

Create a task

Finally, here is a task that will create 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 }}"

This should do it all.

Of course, best way to use this task is to add it to some "network" role, and call it from a playbook.

Good luck.

like image 138
leucos Avatar answered Oct 24 '22 10:10

leucos