diff --git a/interfaces.yml b/interfaces.yml new file mode 100755 index 00000000..5c7107a7 --- /dev/null +++ b/interfaces.yml @@ -0,0 +1,70 @@ +#!/usr/bin/env ansible-playbook +--- +# Get ifname of configured vlan for all servers +- hosts: server + tasks: + - shell: "grep {{ item }} /sys/class/net/*/ifalias | sed \"s|/sys/class/net/||\" | sed \"s|/ifalias:.*||\"" + check_mode: no + register: ifaces + loop: + - srv + - ens + - adm + - borne + - switch + - fil + +- hosts: boeing.adm.crans.org,cochon.adm.crans.org,tracker.adm.crans.org,voyager.adm.crans.org,lutim.adm.crans.org,gateau.adm.crans.org,owncloud-srv.adm.crans.org,charybde.adm.crans.org,cas-srv.adm.crans.org,fyre.adm.crans.org,silice.adm.crans.org + vars: + vlan: + - name: srv + id: 0 + metric: 100 + gateway: 185.230.79.254 + dns: 185.230.79.152 185.230.79.4 + dns_search: crans.org + ifnames: "{{ ifaces | json_query('results[?item==`srv`].stdout') }}" + + - name: ens + id: 1 + metric: 300 + gateway: 138.231.136.254 + dns: 138.231.136.152 138.231.136.4 + dns_search: crans.org + ifnames: "{{ ifaces | json_query('results[?item==`ens`].stdout') }}" + + - name: adm + id: 2 + dns: 10.231.136.152 10.231.136.4 + dns_search: adm.crans.org + ifnames: "{{ ifaces | json_query('results[?item==`adm`].stdout') }}" + + - name: borne + id: 3 + dns: 10.231.148.52 10.231.148.4 + dns_search: borne.crans.org + ifnames: "{{ ifaces | json_query('results[?item==`borne`].stdout') }}" + + - name: switch + id: 4 + dns: 10.231.100.152 10.231.100.4 + dns_search: switch.crans.org + ifnames: "{{ ifaces | json_query('results[?item==`switch`].stdout') }}" + + - name: fil + id: 21 + metric: 400 + gateway: 10.54.0.254 + dns: 10.54.0.152 10.54.0.4 + dns_search: fil.crans.org + ifnames: "{{ ifaces | json_query('results[?item==`fil`].stdout') }}" + + - name: adh + id: 23 + metric: 200 + gateway: 185.230.78.254 + dns: 185.230.78.152 185.230.78.4 + dns_search: crans.org + ifnames: "{{ ifaces | json_query('results[?item==`adh`].stdout') }}" + roles: + - interfaces diff --git a/roles/interfaces/tasks/main.yml b/roles/interfaces/tasks/main.yml new file mode 100644 index 00000000..886b45d3 --- /dev/null +++ b/roles/interfaces/tasks/main.yml @@ -0,0 +1,23 @@ +--- +- name: Install vlan support + apt: + update_cache: true + name: vlan + state: present + register: apt_result + retries: 3 + until: apt_result is succeeded + +- name: Deploy default interfaces config + template: + src: network/interfaces.j2 + dest: /etc/network/interfaces + mode: 0644 + +- name: Deploy interfaces config + template: + src: "network/interfaces.d/ifalias.j2" + dest: "/etc/network/interfaces.d/{{ '%02d' | format(item.id) }}-{{ item.name }}" + mode: 0644 + when: (item.ifnames | length > 0) and item.ifnames[0] != '' + loop: "{{ vlan }}" diff --git a/roles/interfaces/templates/network/interfaces.d/ifalias.j2 b/roles/interfaces/templates/network/interfaces.d/ifalias.j2 new file mode 100644 index 00000000..daf6a938 --- /dev/null +++ b/roles/interfaces/templates/network/interfaces.d/ifalias.j2 @@ -0,0 +1,36 @@ +{{ ansible_header | comment }} + +{% set ifconfig = hostvars[inventory_hostname]['ansible_' + item.ifnames[0]] %} +auto {{ item.ifnames[0] }} +iface {{ item.ifnames[0] }} inet static + address {{ ifconfig.ipv4.address }} + network {{ ifconfig.ipv4.network }} + netmask {{ ifconfig.ipv4.netmask }} + broadcast {{ ifconfig.ipv4.broadcast }} +{% if item.gateway is defined %} + gateway {{ item.gateway }} +{% endif %} +{% if item.metric is defined %} + metric {{ item.metric }} +{% endif %} + mtu 1496 + dns-nameservers {{ item.dns }} + dns-search {{ item.dns_search }} + up /sbin/ip link set $IFACE alias {{ item.name }} +{% if ansible_local.interfaces.sup_if_4 is defined %} +{% if item.ifnames[0] in ansible_local.interfaces.sup_if_4 %} +{% for line in ansible_local.interfaces.sup_if_4[item.ifnames[0]] %} + {{ line }} +{% endfor %} +{% endif %} +{% endif %} + +iface {{ item.ifnames[0] }} inet6 static + address {{ ifconfig.ipv6[0].address }}/{{ ifconfig.ipv6[0].prefix }} +{% if ansible_local.interfaces.sup_if_6 is defined %} +{% if item.ifnames[0] in ansible_local.interfaces.sup_if_6 %} +{% for line in ansible_local.interfaces.sup_if_6[item.ifnames[0]] %} + {{ line }} +{% endfor %} +{% endif %} +{% endif %} diff --git a/roles/interfaces/templates/network/interfaces.j2 b/roles/interfaces/templates/network/interfaces.j2 new file mode 100644 index 00000000..0c339966 --- /dev/null +++ b/roles/interfaces/templates/network/interfaces.j2 @@ -0,0 +1,10 @@ +{{ ansible_header | comment }} + +# This file describes the network interfaces available on your system +# and how to activate them. For more information, see interfaces(5). + +source /etc/network/interfaces.d/* + +# The loopback network interface +auto lo +iface lo inet loopback