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:
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?
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)
"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.
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.
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
Run a task that gathers facts on the desired hosts (group by for example)
Run a task the templates the desired template files
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:
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.