From 205a2986c0fa478103e01e86c74f28c315d9eb8b Mon Sep 17 00:00:00 2001
From: poulpe <poulpe@localhost.localdomain>
Date: Tue, 28 Mar 2023 11:17:59 +0200
Subject: [PATCH] First commit

---
 .gitignore                 |   2 ++
 Config_H1_Ping_H2.yml      |  60 +++++++++++++++++++++++++++++++++++
 Config_Labo.sh             |   3 ++
 Config_Playbook.yml        |  60 +++++++++++++++++++++++++++++++++++
 ansible-simple.gns3project | Bin 0 -> 2305 bytes
 gns3_get_config.py         |  63 +++++++++++++++++++++++++++++++++++++
 interfaces_H.j2            |   5 +++
 interfaces_R.j2            |  15 +++++++++
 inventory.yml              |   4 +++
 targets                    |  36 +++++++++++++++++++++
 10 files changed, 248 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 Config_H1_Ping_H2.yml
 create mode 100644 Config_Labo.sh
 create mode 100644 Config_Playbook.yml
 create mode 100644 ansible-simple.gns3project
 create mode 100644 gns3_get_config.py
 create mode 100644 interfaces_H.j2
 create mode 100644 interfaces_R.j2
 create mode 100644 inventory.yml
 create mode 100644 targets

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..5ca9215
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+!docs/*.pdf
+docs/*
diff --git a/Config_H1_Ping_H2.yml b/Config_H1_Ping_H2.yml
new file mode 100644
index 0000000..32f3a23
--- /dev/null
+++ b/Config_H1_Ping_H2.yml
@@ -0,0 +1,60 @@
+---
+- name: Configuration et démarrage des interfaces réseau
+  hosts: all
+  become: true
+  tasks:
+    - name: Rename hosts
+      command: hostnamectl set-hostname {{ inventory_hostname }}
+      notify: rename_finish
+  
+  handlers:
+    - name: rename_finish
+      debug:
+        msg: "I can handle dates"
+    
+- name: Setup network for Hosts
+  hosts: hotes
+  become: yes
+  tasks:
+    - name: "Create file interface in interfaces.d"
+      ansible.builtin.template:
+        src: ./interfaces_H.j2
+        dest: /etc/network/interfaces.d/interfaces_playbook.conf
+        mode: 0640
+    - name: Restart interfaces hosts
+      shell: "ifdown --force eth0"
+
+- name: Setup network for Routers
+  hosts: routeurs
+  become: yes
+  tasks:
+    - name: "Create file interface in interfaces.d"
+      ansible.builtin.template:
+        src: ./interfaces_R.j2
+        dest: /etc/network/interfaces.d/interfaces_playbook.conf
+        mode: 0640
+    - name: "Set ip_forwarding to 1"
+      ansible.posix.sysctl:
+        name: net.ipv4.ip_forward
+        value: '1'
+        sysctl_set: true
+        state: present
+    - name: Restart interfaces hosts
+      shell: "ifdown --force eth0 eth1"
+
+
+- name: Configuration et démarrage des interfaces réseau
+  hosts: all
+  become: true
+  tasks:
+    - name: Restart interfaces hosts
+      shell: "ifup -a --ignore-errors"
+      notify: start network
+
+  handlers:
+    - name: start network
+      service:
+        name: networking
+        state: restarted
+        enabled: yes
+...
\ No newline at end of file
diff --git a/Config_Labo.sh b/Config_Labo.sh
new file mode 100644
index 0000000..142f8fc
--- /dev/null
+++ b/Config_Labo.sh
@@ -0,0 +1,3 @@
+#!/bin/bash
+
+ls -al
\ No newline at end of file
diff --git a/Config_Playbook.yml b/Config_Playbook.yml
new file mode 100644
index 0000000..32f3a23
--- /dev/null
+++ b/Config_Playbook.yml
@@ -0,0 +1,60 @@
+---
+- name: Configuration et démarrage des interfaces réseau
+  hosts: all
+  become: true
+  tasks:
+    - name: Rename hosts
+      command: hostnamectl set-hostname {{ inventory_hostname }}
+      notify: rename_finish
+  
+  handlers:
+    - name: rename_finish
+      debug:
+        msg: "I can handle dates"
+    
+- name: Setup network for Hosts
+  hosts: hotes
+  become: yes
+  tasks:
+    - name: "Create file interface in interfaces.d"
+      ansible.builtin.template:
+        src: ./interfaces_H.j2
+        dest: /etc/network/interfaces.d/interfaces_playbook.conf
+        mode: 0640
+    - name: Restart interfaces hosts
+      shell: "ifdown --force eth0"
+
+- name: Setup network for Routers
+  hosts: routeurs
+  become: yes
+  tasks:
+    - name: "Create file interface in interfaces.d"
+      ansible.builtin.template:
+        src: ./interfaces_R.j2
+        dest: /etc/network/interfaces.d/interfaces_playbook.conf
+        mode: 0640
+    - name: "Set ip_forwarding to 1"
+      ansible.posix.sysctl:
+        name: net.ipv4.ip_forward
+        value: '1'
+        sysctl_set: true
+        state: present
+    - name: Restart interfaces hosts
+      shell: "ifdown --force eth0 eth1"
+
+
+- name: Configuration et démarrage des interfaces réseau
+  hosts: all
+  become: true
+  tasks:
+    - name: Restart interfaces hosts
+      shell: "ifup -a --ignore-errors"
+      notify: start network
+
+  handlers:
+    - name: start network
+      service:
+        name: networking
+        state: restarted
+        enabled: yes
+...
\ No newline at end of file
diff --git a/ansible-simple.gns3project b/ansible-simple.gns3project
new file mode 100644
index 0000000000000000000000000000000000000000..457885e70ff62c41a3165807f08a5b6ad338301b
GIT binary patch
literal 2305
zcmWIWW@Zs#;9%fjI8aa$#()HP7#J7|it@8klS}l{^NNk%Mn&h}-XZi)yYc*Mqi3;8
zZeQNc^m|Xl1DiQwZ0q~{Qch~0^t`O_|1VFfXPJ<7>82NInsZqCOjO+G^c#QX)x7U_
zX4l$h>sHJ%m^pXrSF1l|+a`vt4rtPtwMa76H~UqY`-|-2xzoFUE;{#j&&mDjvLOzK
z*C}{6UDCPy^~e<8yzYisH9MQ?bC_~h@A?&A@Yd%0O7|W~zA&~af(He|Zz+rRI5%-Q
z_k5hhGT-g$L#_NzT4w_f=RS6ld?v0KAG1VKsOO``-gC9p6PI1%)?f8H?^JZRbzt1K
zg?-0bD-&}xX0e}b-N{+HIk`4?)>WB+m!Z9nzGj6!S;!W+Q;%!HmaD6@RvqQ_$Wvbz
zweDK3N7ujKrQM0;_0~$e4jy`~VeGEED9-XoZih$uECm_U(@i1QSm$og&?=WKdma$J
zHq-n;iCDbuq+R_V{;y$vd_P`oQtiw+xgVOWyY~Co)#e=D{!J$=wR=(Iv!lEwe=0X~
z{a;z^z_zR0H#2f!<AY+Gj&<y&+q7zgp1CrX|37rsU*F;CsU3TKy*#CUaoSyeKjl;Z
z<u6bC<(C^A^gp>E-yrtL#+K79QmF+WZDa5JE&C>*B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oe28JM4M;BjLy^@L&)iZnf4k<{qKAgMS
zB3fO_Y5Oq=-#Ic;`HT;bT-s8@^5OZXwT0EwzwZ={d~v(u+)}Cc+NHhGaW@Z&?>ffd
zx#1Y!EuLFarFGjPPj#$5!zr%FcRH?CVBU|y{hPmUlASt><!8R~hF?O(-PT{rMEidv
z{bB;Sa{^P>qyh#8hDrvII~kcom=R46WIuqK9<Y{310#rq+z<)yM(70j5!qCbA3^Ft
bcuC_$c4Qw1c(byB#F-eF7>Yoy-~jOer>H7p

literal 0
HcmV?d00001

diff --git a/gns3_get_config.py b/gns3_get_config.py
new file mode 100644
index 0000000..a6adfb4
--- /dev/null
+++ b/gns3_get_config.py
@@ -0,0 +1,63 @@
+#!/usr/bin/env python3
+
+import requests
+from jinja2 import Environment, FileSystemLoader, Template
+import json
+import sys
+from pathlib import Path
+
+# TAILSCALE : sudo tailscale up --login-server https://tailscale.hepiapp.ch
+# https://hepia.infolibre.ch/VRES-2022-2023/labos/preparation.html#installation-du-client-gns3-sous-gnu-linux
+
+PROJECTS_URL='https://gns3.hepiapp.ch/v2/projects'
+
+# Template for jinja2
+tm_config = """
+{% for node in nodes %}
+{% if node['console'] != None %}
+Host {{ node['name'] }}
+    User root
+    Hostname {{ node['console_host'] }}
+    Port {{ node['console']+1 }}
+    StrictHostKeychecking no
+    UserKnownHostsFile /dev/null
+    IdentityFile ~/.ssh/gns3.rsa
+{% endif %}
+{% endfor %}
+"""
+
+# Create file of config ssh
+def create_file(filepath : str = "/home/poulpe/.ssh/config.d/gns3.conf",data : str = "") -> str:
+    with open(filepath,"a") as f:
+        f.write(data)
+        f.write("\n")
+        f.close()
+    return filepath
+
+# Get the config from GNS3
+def get_config(proj_name : str,verbose : bool = False) -> None:
+    data = requests.get(PROJECTS_URL).json()
+    for proj in data:
+        if proj_name == proj["name"]:
+            print(f"Project use : {proj['name']}")
+            url = PROJECTS_URL+"/" + proj["project_id"]+"/nodes"
+            nodes = requests.get(url).json()
+            tm = Template(tm_config)
+            msg = tm.render(nodes=nodes)
+            path = create_file(data=msg)
+    print(f"Config write here : '{path}'")
+
+# Delete the config file if he exist
+def delete_file_if_exist(filepath : str = "/home/poulpe/.ssh/config.d/gns3.conf") -> None:
+    Path("/home/poulpe/.ssh/config.d/").mkdir(parents=True, exist_ok=True)
+    f = open(filepath, "w")
+    f.close()
+
+if __name__ == "__main__":
+    if len(sys.argv) < 2:
+        print("Create config file for SSH connection from GNS3 project name")
+        print(f"{sys.argv[0]} <project_name>")
+        exit(1)
+    name = sys.argv[1]
+    delete_file_if_exist()
+    get_config(name)
\ No newline at end of file
diff --git a/interfaces_H.j2 b/interfaces_H.j2
new file mode 100644
index 0000000..bd875a2
--- /dev/null
+++ b/interfaces_H.j2
@@ -0,0 +1,5 @@
+auto {{ ifname }}
+iface {{ ifname }} inet static
+ address {{ ifip }}
+ netmask {{ ifcidr }}
+ post-up ip route add default via {{ ifgw }}
diff --git a/interfaces_R.j2 b/interfaces_R.j2
new file mode 100644
index 0000000..0bf0c01
--- /dev/null
+++ b/interfaces_R.j2
@@ -0,0 +1,15 @@
+auto {{ ifname }}
+iface {{ ifname }} inet static
+ address {{ ifip }}
+ netmask {{ ifcidr }}
+
+auto {{ ifname2 }}
+iface {{ ifname2 }} inet static
+ address {{ ifip2 }}
+ netmask {{ ifcidr2 }}
+ {% if Rname != "R2" %}
+post-up ip route add 3.0.0.0/24 nexthop via {{ ifgw }}
+ {% endif %}
+ {% if Rname != "R1" %}
+post-up ip route add 1.0.0.0/24 nexthop via {{ ifgw2 }}
+ {% endif %}
diff --git a/inventory.yml b/inventory.yml
new file mode 100644
index 0000000..0262001
--- /dev/null
+++ b/inventory.yml
@@ -0,0 +1,4 @@
+H1
+H2
+R1
+R1
\ No newline at end of file
diff --git a/targets b/targets
new file mode 100644
index 0000000..24d217a
--- /dev/null
+++ b/targets
@@ -0,0 +1,36 @@
+[hotes]
+H1
+H2 ifip="3.0.0.2" ifgw="3.0.0.1"
+
+[hotes:vars]
+ifname="eth0"
+ifip="1.0.0.2"
+ifcidr="255.255.255.0"
+ifgw="1.0.0.1"
+
+[routeurs]
+R1 ifip="1.0.0.1" ifgw="2.0.0.2" Rname="R1"
+R2 ifip2="2.0.0.2" ifgw2="2.0.0.1" Rname="R2"
+
+[routeurs:vars]
+ifname="eth1"
+ifip="3.0.0.1"
+ifcidr="255.255.255.0"
+ifgw="2.0.0.1"
+ifname2="eth0"
+ifip2="2.0.0.1"
+ifcidr2="255.255.255.0"
+ifgw2="1.0.0.2"
+Rname=""
+
+[H1]
+H1
+
+[H2]
+H2
+
+[R1]
+R1
+
+[R2]
+R2
-- 
GitLab