From fa86ce78aa04dad5058b79c9fa9eb24e85ea531b Mon Sep 17 00:00:00 2001
From: Karl Grube <karl@hudlergrube.com>
Date: Fri, 3 Nov 2023 14:27:06 +0100
Subject: [PATCH] interface configuration with ansible

---
 handlers/main.yml       |  4 +---
 tasks/interfaces.yml    | 18 ++++++++++++++++++
 tasks/main.yml          |  3 +++
 templates/interfaces.j2 | 21 +++++++++++++++++++++
 4 files changed, 43 insertions(+), 3 deletions(-)
 create mode 100644 tasks/interfaces.yml
 create mode 100644 templates/interfaces.j2

diff --git a/handlers/main.yml b/handlers/main.yml
index 3bf4ce1..ab0608b 100644
--- a/handlers/main.yml
+++ b/handlers/main.yml
@@ -2,7 +2,5 @@
 
 - name: restart nftables
   command: 'nft -f /etc/nftables.conf'
-- name: ifup all
-  command: "ifreload -a"
-- name: reload networking
+- name: ifreload
   command: "ifreload -a"
diff --git a/tasks/interfaces.yml b/tasks/interfaces.yml
new file mode 100644
index 0000000..83d2d6c
--- /dev/null
+++ b/tasks/interfaces.yml
@@ -0,0 +1,18 @@
+---
+
+- name: prevent proxmox from overwriting interfaces
+  copy:
+    dest: /etc/network/.pve-ignore.interfaces
+    owner: root
+    group: root
+    content: ""
+    force: no
+    mode: 0644
+  tags: network,interfaces
+
+- name: write /etc/network/interfaces
+  template:
+    src: interfaces.j2
+    dest: /etc/network/interfaces
+  notify: ifreload
+  tags: network,interfaces
diff --git a/tasks/main.yml b/tasks/main.yml
index 05a44cd..58055b5 100644
--- a/tasks/main.yml
+++ b/tasks/main.yml
@@ -2,6 +2,9 @@
 
 - import_tasks: all.yml
  
+- import_tasks: interfaces.yml
+  when: my_interfaces is defined
+
 - name: nftables
   import_role:
     name: nftables
diff --git a/templates/interfaces.j2 b/templates/interfaces.j2
new file mode 100644
index 0000000..2483ec9
--- /dev/null
+++ b/templates/interfaces.j2
@@ -0,0 +1,21 @@
+source /etc/network/interfaces.d/*
+
+auto lo
+iface lo inet static
+{% if loopback_ipv4_address is defined %}
+  address {{loopback_ipv4_address}}/32
+{% endif %}
+{% if loopback_ipv6_address is defined %}
+  address {{loopback_ipv6_address}}/128
+{% endif %}
+
+{% for interface in my_interfaces %}
+auto {{(interface|list)[0]}}
+iface {{(interface|list)[0]}}
+  mtu {{interface.mtu|default(9166)}}
+{%   if interface.ips is defined %}
+{%     for ip in interface.ips %}
+  address {{ip}}
+{%     endfor %}
+{%   endif %}
+{% endfor %}
-- 
GitLab