From 0c0c1c3cb8aa66f571410d00ae6e977bb8d3c62a Mon Sep 17 00:00:00 2001
From: "iliya.saroukha" <iliya.saroukhanian@etu.hesge.ch>
Date: Tue, 11 Mar 2025 16:19:56 +0100
Subject: [PATCH] feat: first play finished, gotta start the wireguard thing

---
 playbooks/first.play.yml          | 101 ++++++++++++++++++++++++++++++
 playbooks/inventory.ini           |   7 +++
 playbooks/templates/interfaces.j2 |   8 +++
 playbooks/templates/routes.j2     |   4 ++
 4 files changed, 120 insertions(+)
 create mode 100644 playbooks/first.play.yml
 create mode 100644 playbooks/inventory.ini
 create mode 100644 playbooks/templates/interfaces.j2
 create mode 100644 playbooks/templates/routes.j2

diff --git a/playbooks/first.play.yml b/playbooks/first.play.yml
new file mode 100644
index 0000000..96369b2
--- /dev/null
+++ b/playbooks/first.play.yml
@@ -0,0 +1,101 @@
+---
+- name: Configure network interfaces
+  hosts: all
+  become: true
+  vars:
+    gns3_hosts:
+      H1:
+        interfaces:
+          - name: "eth0"
+            address: "1.0.0.3"
+            netmask: "255.255.255.0"
+            gateway: "1.0.0.1"
+
+      R1:
+        interfaces:
+          - name: eth1
+            address: "1.0.0.1"
+            netmask: "255.255.255.0"
+          - name: eth0
+            address: "2.0.0.1"
+            netmask: "255.255.255.0"
+        routes:
+          - network: "3.0.0.0/24"
+            via: "2.0.0.2"
+
+      R2:
+        interfaces:
+          - name: eth0
+            address: "2.0.0.2"
+            netmask: "255.255.255.0"
+          - name: eth1
+            address: "3.0.0.2"
+            netmask: "255.255.255.0"
+        routes:
+          - network: "1.0.0.0/24"
+            via: "2.0.0.1"
+
+      H2:
+        interfaces:
+          - name: eth0
+            address: "3.0.0.3"
+            netmask: "255.255.255.0"
+            gateway: "3.0.0.2"
+
+  tasks:
+    # - name: "Debug interfaces"
+    #   ansible.builtin.debug:
+    #     msg: "{{ item.name }}"
+    #   loop: "{{ gns3_hosts[inventory_hostname].interfaces }}"
+
+    # - name: "Debug routes"
+    #   ansible.builtin.debug:
+    #     msg: "{{ item.network }}"
+    #   loop: "{{ gns3_hosts[inventory_hostname].routes }}"
+
+    # - name: "Debug YAML"
+    #   ansible.builtin.debug:
+    #     msg: "{{ item.interfaces }}"
+    #   loop: "{{ gns3_hosts[inventory_hostname] | dict2items }}"
+    #   tags: debug_yaml
+
+    - name: "Ensure /etc/network/interfaces.d exists"
+      ansible.builtin.file:
+        path: /etc/network/interfaces.d
+        state: directory
+        mode: '0755'
+
+    - name: "Configure network interfaces"
+      ansible.builtin.template:
+        src: "./templates/interfaces.j2"
+        dest: "/etc/network/interfaces.d/{{ item.name }}"
+        owner: "root"
+        group: "root"
+        mode: "0644"
+      loop: "{{ gns3_hosts[inventory_hostname].interfaces }}"
+      notify: Restart networking
+
+    - name: "Configure routes"
+      ansible.builtin.template:
+        src: "./templates/routes.j2"
+        dest: "/etc/network/interfaces.d/{{ inventory_hostname }}_routes"
+        owner: "root"
+        group: "root"
+        mode: "0644"
+      loop: "{{ gns3_hosts[inventory_hostname].routes }}"
+      when: inventory_hostname != "H1" and inventory_hostname != "H2"
+      notify: Restart networking
+
+    # - name: Verify H1 can ping H2
+    #   command: ping -c 3 3.0.0.3
+    #   register: ping_result
+    #   changed_when: false
+    #   failed_when: ping_result.rc != 0
+    #   when: inventory_hostname == "H1"
+
+  handlers:
+    - name: "Restart networking"
+      ansible.builtin.systemd:
+        name: networking
+        state: restarted
+        enabled: true
diff --git a/playbooks/inventory.ini b/playbooks/inventory.ini
new file mode 100644
index 0000000..03ce03b
--- /dev/null
+++ b/playbooks/inventory.ini
@@ -0,0 +1,7 @@
+[hosts]
+H1
+H2
+
+[routers]
+R1
+R2
diff --git a/playbooks/templates/interfaces.j2 b/playbooks/templates/interfaces.j2
new file mode 100644
index 0000000..30c42c9
--- /dev/null
+++ b/playbooks/templates/interfaces.j2
@@ -0,0 +1,8 @@
+auto {{ item.name }}
+iface {{ item.name }} inet static
+    address {{ item.address }}
+    netmask {{ item.netmask }}
+{% if item.gateway is defined %}
+    gateway {{ item.gateway }}
+{% endif %}
+
diff --git a/playbooks/templates/routes.j2 b/playbooks/templates/routes.j2
new file mode 100644
index 0000000..9ea96c6
--- /dev/null
+++ b/playbooks/templates/routes.j2
@@ -0,0 +1,4 @@
+{% if item.network is defined %}
+    up ip route add {{ item.network }} via {{ item.via }}
+    down ip route del {{ item.network }} via {{ item.via }}
+{% endif %}
-- 
GitLab