# Ideas on how to set-up dynamic ifcfg files

**URL:** <https://forum.ansible.com/t/ideas-on-how-to-set-up-dynamic-ifcfg-files/13845>\
**Category:** Ansible Project\
**Tags:** ee\
**Created:** [November 5, 2013, 5:11am UTC](https://forum.ansible.com/t/ideas-on-how-to-set-up-dynamic-ifcfg-files/13845 "2013-11-05T05:11:08Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gonzalo\_Servat](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Gonzalo\_Servat](https://forum.ansible.com/u/Gonzalo_Servat)\
**Post date:** [November 5, 2013, 5:11am UTC](https://forum.ansible.com/t/ideas-on-how-to-set-up-dynamic-ifcfg-files/13845/1 "2013-11-05T05:11:08Z")

</div>

Hi All,

I’ve just started looking into Ansible and I was wondering how the more experienced users would go about doing this.

I want Ansible to configure the ifcfg-XXX and ifroute-XXX files for me using a template populated by host\_vars. Now, the tricky bit is that the order of interfaces may vary from server to server, and even within the server (i.e. may change for whatever reason over time). If possible, I’d like to set up the interfaces based on MAC address instead of NIC name. To illustrate what I mean:

host\_vars/hostname:

nics:  
main:  
config:  
mac: AA:BB:CC:DD:EE:F0  
ip: 1.2.3.4  
netmask: …  
etc  
routes:

- route1
- route2
- …  
backup:  
config:  
mac: AA:BB:CC:DD:EE:F1  
ip: 2.3.4.5  
netmask: …  
routes:
- route3
- route4
- …

With ansible, I basically want to loop over the ‘nics’ dict and set-up the relevant ifcfg- files. The part will have to be looked up through the facters (i.e. loop macaddress\_\* facters once to map mac addresses to nic names then use the mac address in the YAML above to find the ethXX NIC name, which will be the file we write to – i.e. /etc/sysconfig/network/ifcfg-).

Is this reasonable? Can it be done without hacking up something horrible? 🙂 I haven’t got the hang of things yet but I suspect perhaps I may need to write a custom plugin and/or module?

Any suggestions/feedback would be appreciated.

Thanks!  
Gonzalo

---

<div class="post-metadata">

**Author:** ![Gonzalo\_Servat](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Gonzalo\_Servat](https://forum.ansible.com/u/Gonzalo_Servat)\
**Post date:** [November 12, 2013, 4:24am UTC](https://forum.ansible.com/t/ideas-on-how-to-set-up-dynamic-ifcfg-files/13845/2 "2013-11-12T04:24:58Z")

</div>

Anyone have any ideas on how I can achieve this? Looking for pointers on whether I need to write a plugin, module, or if it’s even possible to do this.

I basically want to use the variables set in the host\_vars file for each host, together with facts information, to build the necessary ifcfg-XXXX and ifroute-XXXX files. The challenging bit is the fact that I don’t know ahead of time what the name of the NICs are (needs to work it out from facts). The logic is:

- Loop over the NICs in the ‘ansible\_interfaces’ facts

- For each NIC, check if the NIC config has been defined (based on the MAC address)

- If it has, create /etc/sysconfig/network/ifcfg- from template (populated with host\_vars data) and /etc/sysconfig/network/ifroute- (also populated from host\_vars data)

Any pointers on how to achieve this in Ansible?

---

<div class="post-metadata">

**Author:** ![tannerjc](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/tannerjc/32/985_2.png) [@tannerjc](https://forum.ansible.com/u/tannerjc)\
**Post date:** [November 12, 2013, 4:28am UTC](https://forum.ansible.com/t/ideas-on-how-to-set-up-dynamic-ifcfg-files/13845/3 "2013-11-12T04:28:13Z")

</div>

The first question I suppose is if these machines will be routable prior to the mentioned configuration?

"Now, the tricky bit is that the order of interfaces may vary from server to server, and even within the server"

Are your machines el5 based? This behavior is no longer true in el6 with the default udev configuration.

---

<div class="post-metadata">

**Author:** ![tannerjc](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/tannerjc/32/985_2.png) [@tannerjc](https://forum.ansible.com/u/tannerjc)\
**Post date:** [November 12, 2013, 2:00pm UTC](https://forum.ansible.com/t/ideas-on-how-to-set-up-dynamic-ifcfg-files/13845/4 "2013-11-12T14:00:55Z")

</div>

"Yes, machines are routable. They come up with one good nic and I want to go in with Ansible and set up the ifcfg and ifroute files for all interfaces.

All machines are either SLES 10 or 11."

I would suggest starting with building a role that takes in a few inputs:

\* nic number  
\* nic mac address  
\* routes

The role should then create the necessary config files from templates. I suggest a role because you do a lot of little tasks to achieve the goal instead of trying to have everything happening in one task in a top level playbook.

---

<div class="post-metadata">

**Author:** ![Kesten\_Broughton2](https://avatars.discourse-cdn.com/v4/letter/k/ecc23a/32.png) [@Kesten\_Broughton2](https://forum.ansible.com/u/Kesten_Broughton2)\
**Post date:** [November 12, 2013, 5:02pm UTC](https://forum.ansible.com/t/ideas-on-how-to-set-up-dynamic-ifcfg-files/13845/5 "2013-11-12T17:02:21Z")

</div>

I have a very rough start on this, in attempts to switch from dhcp to static ips and set other critical networking files prior to ssh being abailable on hosts created on vsphere esx servers.  
My plan is to add it to the excellent ansible provisioning github when it is ready: [https://github.com/ansible-provisioning](https://github.com/ansible-provisioning).  
To do work without an ssh connection, I write bare python modules with the pysphere library, then incorporate calls from vsphere or direct calls to my python modules via the ansible shell module.

I keep a jinja2 template in my roles/centos\_common/templates folder

The workflow would be

1. Run a task that gathers facts on the desired hosts (group by for example)
2. Run a task the templates the desired template files
3. Notify handler to restart network service

This gist contains ansible client files, python modules, jinja2 templates. [https://gist.github.com/darKoram/7329543](https://gist.github.com/darKoram/7329543)

The template file to look at is  
etc\_sysconfig\_network-scripts\_ifcfg-eth0

Which is executed with any other files given via variable “paths” by update\_network\_configs.py  
If you already have a connection via ssh, you can throw away most everything and just use the template.

If you want to use with\_fileglob or with\_items in your ansible tasks you can use jinja filters to convert “_" to “/” adding an extra ‘/’ at the start.  
template: src=“/{{item | replace(”_”,“/”) …  
with\_items:

- template\_file1
- template\_file2

kesten

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 12, 2013, 5:42pm UTC](https://forum.ansible.com/t/ideas-on-how-to-set-up-dynamic-ifcfg-files/13845/6 "2013-11-12T17:42:21Z")

</div>

There is no reason to add modules to ansible-provisioning, modules can be submitted to core. We have already pulled over several…

– Michael

---

<div class="post-metadata">

**Author:** ![Gonzalo\_Servat](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Gonzalo\_Servat](https://forum.ansible.com/u/Gonzalo_Servat)\
**Post date:** [November 22, 2013, 3:21am UTC](https://forum.ansible.com/t/ideas-on-how-to-set-up-dynamic-ifcfg-files/13845/7 "2013-11-22T03:21:28Z")

</div>

Hi James,

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 22, 2013, 9:12pm UTC](https://forum.ansible.com/t/ideas-on-how-to-set-up-dynamic-ifcfg-files/13845/8 "2013-11-22T21:12:25Z")

</div>

I’d start by reading the template information: [http://jinja.pocoo.org/docs/](http://jinja.pocoo.org/docs/)

---

<div class="post-metadata">

**Author:** ![Gonzalo\_Servat](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Gonzalo\_Servat](https://forum.ansible.com/u/Gonzalo_Servat)\
**Post date:** [November 23, 2013, 12:42pm UTC](https://forum.ansible.com/t/ideas-on-how-to-set-up-dynamic-ifcfg-files/13845/9 "2013-11-23T12:42:16Z")

</div>

I don’t have an issue with the template itself. I was asking how I can iterate within the playbook over some (not all) of the network interfaces in the system (the ones I have defined a config for), since roles/includes + with\_items is not recommended.

- GS

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 23, 2013, 3:39pm UTC](https://forum.ansible.com/t/ideas-on-how-to-set-up-dynamic-ifcfg-files/13845/10 "2013-11-23T15:39:13Z")

</div>

For “How do I iterate within a playbook” I recommend reading the looping information in the documentation.
