diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..5ca921568a7dc2745cecc94a18e5caa681bf0a60 --- /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 0000000000000000000000000000000000000000..32f3a23307cbd532cdce39e3a018c19b56fd76e0 --- /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 0000000000000000000000000000000000000000..142f8fc98352e35da8cc34abd52c07554bc9cbc7 --- /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 0000000000000000000000000000000000000000..32f3a23307cbd532cdce39e3a018c19b56fd76e0 --- /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 Binary files /dev/null and b/ansible-simple.gns3project differ diff --git a/gns3_get_config.py b/gns3_get_config.py new file mode 100644 index 0000000000000000000000000000000000000000..a6adfb4c1482f622d00903884faf1e193e75e4fd --- /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 0000000000000000000000000000000000000000..bd875a2ba3ff4f7999b8b6ded61b56355c25812b --- /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 0000000000000000000000000000000000000000..0bf0c015d50dedd8a7fad7407b275668703640e2 --- /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 0000000000000000000000000000000000000000..0262001774b8717f77d761167680741d8968b069 --- /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 0000000000000000000000000000000000000000..24d217a7a645c2dc426df2edfe271ee64f25becc --- /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