# loop over variables

**URL:** <https://forum.ansible.com/t/loop-over-variables/16623>\
**Category:** Ansible Project\
**Created:** [October 28, 2014, 11:18am UTC](https://forum.ansible.com/t/loop-over-variables/16623 "2014-10-28T11:18:15Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevin\_parker](https://avatars.discourse-cdn.com/v4/letter/k/df788c/32.png) [@kevin\_parker](https://forum.ansible.com/u/kevin_parker)\
**Post date:** [October 28, 2014, 11:18am UTC](https://forum.ansible.com/t/loop-over-variables/16623/1 "2014-10-28T11:18:15Z")

</div>

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

```auto
#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**

```auto
- 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 a NIC is connected.
