Skip to content
Snippets Groups Projects
Commit 205a2986 authored by poulpe's avatar poulpe
Browse files

First commit

parent d36615a2
No related branches found
No related tags found
No related merge requests found
!docs/*.pdf
docs/*
---
- 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
#!/bin/bash
ls -al
\ No newline at end of file
---
- 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
File added
#!/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
auto {{ ifname }}
iface {{ ifname }} inet static
address {{ ifip }}
netmask {{ ifcidr }}
post-up ip route add default via {{ ifgw }}
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 %}
H1
H2
R1
R1
\ No newline at end of file
targets 0 → 100644
[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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment