From 05d2349f6214d8955d70c5fcedcfc3dfc7bf87c3 Mon Sep 17 00:00:00 2001
From: Benjamin Graillot <graillot@crans.org>
Date: Tue, 21 Apr 2020 16:50:16 +0200
Subject: [PATCH] [interfaces] Configure switch interface

---
 interfaces.yml                                |  6 +++-
 roles/interfaces/tasks/main.yml               |  7 ++++
 .../network/interfaces.d/04-switch.j2         | 34 +++++++++++++++++++
 3 files changed, 46 insertions(+), 1 deletion(-)
 create mode 100644 roles/interfaces/templates/network/interfaces.d/04-switch.j2

diff --git a/interfaces.yml b/interfaces.yml
index 52b9a667..1feb86ca 100755
--- a/interfaces.yml
+++ b/interfaces.yml
@@ -15,6 +15,9 @@
     - shell: grep borne /sys/class/net/*/ifalias | sed "s|/sys/class/net/||" | sed "s|/ifalias:.*||"
       register: borne_iface
       check_mode: no
+    - shell: grep switch /sys/class/net/*/ifalias | sed "s|/sys/class/net/||" | sed "s|/ifalias:.*||"
+      register: switch_iface
+      check_mode: no
     - shell: grep fil /sys/class/net/*/ifalias | sed "s|/sys/class/net/||" | sed "s|/ifalias:.*||"
       register: fil_iface
       check_mode: no
@@ -29,7 +32,8 @@
     - ens_gateway: 138.231.136.254
     - ens_dns: 138.231.136.152 138.231.136.4
     - adm_dns: 10.231.136.152 10.231.136.4
-    - borne_dns: 10.231.148.4
+    - borne_dns: 10.231.148.52 10.231.148.4
+    - switch_dns: 10.231.100.152 10.231.100.4
     - fil_gateway: 10.54.0.254
     - fil_dns: 10.54.0.152 10.54.0.4
     - adh_gateway: 185.230.78.254
diff --git a/roles/interfaces/tasks/main.yml b/roles/interfaces/tasks/main.yml
index 5b41c028..210e3142 100644
--- a/roles/interfaces/tasks/main.yml
+++ b/roles/interfaces/tasks/main.yml
@@ -33,6 +33,13 @@
     mode: 0644
   when: borne_iface.stdout
 
+- name: Deploy switch interface config
+  template:
+    src: network/interfaces.d/04-switch.j2
+    dest: /etc/network/interfaces.d/04-switch
+    mode: 0644
+  when: switch_iface.stdout
+
 - name: Deploy fil interface config
   template:
     src: network/interfaces.d/21-fil.j2
diff --git a/roles/interfaces/templates/network/interfaces.d/04-switch.j2 b/roles/interfaces/templates/network/interfaces.d/04-switch.j2
new file mode 100644
index 00000000..d8cfeb8b
--- /dev/null
+++ b/roles/interfaces/templates/network/interfaces.d/04-switch.j2
@@ -0,0 +1,34 @@
+{{ ansible_header | comment }}
+
+{% set switch = hostvars[inventory_hostname]['ansible_' + switch_iface.stdout] %}
+allow-hotplug {{ switch_iface.stdout }}
+iface {{ switch_iface.stdout }} inet static
+	address {{ switch.ipv4.address }}
+	network {{ switch.ipv4.network }}
+	netmask {{ switch.ipv4.netmask }}
+	broadcast {{ switch.ipv4.broadcast }}
+	mtu 1496
+	dns-nameservers {{ switch_dns }}
+	dns-search switch.crans.org
+	up /sbin/ip link set $IFACE alias switch
+{% if 'interfaces' in ansible_local %}
+{% if 'sup_if_4' in ansible_local.interfaces %}
+{% if switch_iface.stdout in ansible_local.interfaces.sup_if_4 %}
+{% for line in ansible_local.interfaces.sup_if_4[switch_iface.stdout] %}
+	{{ line }}
+{% endfor %}
+{% endif %}
+{% endif %}
+{% endif %}
+
+iface {{ switch_iface.stdout }} inet6 static
+	address {{ switch.ipv6[0].address }}/{{ switch.ipv6[0].prefix }}
+{% if 'interfaces' in ansible_local %}
+{% if 'sup_if_6' in ansible_local.interfaces %}
+{% if switch_iface.stdout in ansible_local.interfaces.sup_if_6 %}
+{% for line in ansible_local.interfaces.sup_if_6[switch_iface.stdout] %}
+	{{ line }}
+{% endfor %}
+{% endif %}
+{% endif %}
+{% endif %}