I want to be able to fully manage my /etc/network/interfaces.d/ configuration files using Ansible.
I already use ansible for a lot of feature, including apache files, database, and logs files, but I can't find a way to properly add / update / remove network interface configuration files.
There are a few different project on my server using different interfaces, and I want my ansible to be able to work on any server i could deploy my project.
I already found a way to create a new file using the next free interface like this :
- name: calc next free interface
set_fact:
nextFreeIf: "
{%- set ifacePrefix = vars.ansible_default_ipv4.alias -%}
{%- set ifaceNum = { 'cnt': 0 } -%}
{%- macro increment(dct, key, inc=1)-%}
{%- if dct.update({key: dct[key] + inc}) -%}
{%- endif -%}
{%- endmacro -%}
{%- for iface in ansible_interfaces|sort -%}
{%- if iface| regex_search('^' ~ vars.ansible_default_ipv4.alias) -%}
{{ increment(ifaceNum, 'cnt') }}
{%- endif -%}
{%- endfor -%}
{{ifacePrefix}}:{{ifaceNum.cnt}}"
tags: network
- name: "copy network interface configuration"
template:
src: "files/etc/network/interfaces.d/my-configuration.conf"
dest: "/etc/network/interfaces.d/my-configuration.conf"
owner: root
group: root
force: true
notify: 'restart failover interface'
tags: network
Now I need to find a way to check if my configuration file is already present so i don't recreate a new configuration file every time I run ansible. But if it is present, there is still a problem :
network configuration file will look like this
auto {{ interface }}
iface {{ interface }} inet static
address {{ ip }}
netmask 255.255.255.255
Since I don't know which interface is used by my project, I need to check for every available interfaces if it matches the actual file, and update using the next free interface if not.
I can't find a way to do it using Ansible!!
I hope you can help me.