Ajout du playbook Ansible pour Wireguard, du script bash, et des

fichiers de configuration.
This commit is contained in:
Guillaume Emorine
2024-11-14 15:01:59 +01:00
parent 5a11bfbd7d
commit 761184054f
9 changed files with 222 additions and 5 deletions

View File

@@ -0,0 +1,7 @@
# Ansible pour Wireguard
## Description du playbook :
Ce *playbook* se charge d'installer les outils Wireguard et le paquet Wireguard sur chacune des machines, avant de copier le script pour générer les fichiers de configuration sur la machine **ap31-mon**, puis exécute le script, avant de récupérer chacun des trois fichiers de configuration pour les copier sur **ap31-ans**.
Ensuite, le *playbook* se charge de copier les fichiers de configuration à chaque machine de manière individuelle sans possibilité d'erreur, avant d'activer le service Wireguard au démarrage, et de le relancer.

6
ansible/wireguard/hosts Normal file
View File

@@ -0,0 +1,6 @@
[wg]
ap31-prod
ap31-test
ap31-mon
[wg_srv]
ap31-mon

View File

@@ -0,0 +1,80 @@
#!/bin/bash
set -u
set -e
AddressAwg=10.0.0.1/32 # Adresse VPN Wireguard MON
EndpointA=172.16.0.102 # Adresse extremite MON
PortA=51820 # Port ecoute extremite MON
AddressBwg=10.0.0.2/32 # Adresse VPN Wireguard PROD
EndpointB=172.16.0.100 # Adresse extremite PROD
PortB=51820 # Port ecoute extremite PROD
AddressCwg=10.0.0.3/32 # Adresse VPN Wireguard TEST
EndpointC=172.16.0.101 # Adresse extremite TEST
PortC=51820 # Port ecoute extremite TEST
umask 077 ;
wg genkey > endpoint-a.key
wg pubkey < endpoint-a.key > endpoint-a.pub
wg genkey > endpoint-b.key
wg pubkey < endpoint-b.key > endpoint-b.pub
wg genkey > endpoint-c.key
wg pubkey < endpoint-c.key > endpoint-c.pub
PKA=$(cat endpoint-a.key)
pKA=$(cat endpoint-a.pub)
PKB=$(cat endpoint-b.key)
pKB=$(cat endpoint-b.pub)
PKC=$(cat endpoint-c.key)
pKC=$(cat endpoint-c.pub)
cat <<FINI > wg0-mon.conf
# local settings for ap31-mon
[Interface]
PrivateKey = $PKA
Address = $AddressAwg
ListenPort = $PortA
# remote settings for ap31-prod
[Peer]
PublicKey = $pKB
Endpoint = ${EndpointB}:$PortB
AllowedIPs = $AddressBwg
# remote settings for ap31-test
[Peer]
PublicKey= $pKC
Endpoint = ${EndpointC}:$PortC
AllowedIPs = $AddressCwg
FINI
cat <<FINI > wg0-prod.conf
# local settings for ap31-prod
[Interface]
PrivateKey = $PKB
Address = $AddressBwg
ListenPort = $PortB
# remote settings for ap31-mon
[Peer]
PublicKey = $pKA
Endpoint = ${EndpointA}:$PortA
AllowedIPs = $AddressAwg
FINI
cat <<FINI > wg0-test.conf
# local settings for ap31-test
[Interface]
PrivateKey = $PKC
Address = $AddressCwg
ListenPort = $PortC
# remote settings for ap31-mon
[Peer]
PublicKey = $pKA
Endpoint = ${EndpointA}:$PortA
AllowedIPs = $AddressAwg
FINI

74
ansible/wireguard/wg.yml Normal file
View File

@@ -0,0 +1,74 @@
# wg.yml
---
- hosts: wg
become: true
tasks:
- name: 1. Installe le paquet Wireguard.
apt:
name: wireguard
state: present
- name: 2. Installe les outils Wireguard.
apt:
name: wireguard-tools
state: present
- hosts: wg_srv
become: true
tasks:
- name: 3. Envoie le script Wireguard.
copy:
src: mkwgconf-p2p.sh
dest: mkwgconf-p2p.sh
- name: 4. Exécute le script Wireguard.
command: bash mkwgconf-p2p.sh
- name: 5. Copie le fichier de configuration Wireguard C sur cette machine.
ansible.builtin.fetch:
src: wg0-mon.conf
dest: wg0-mon.conf
flat: yes
- name: 6. Récupère le fichier de conf A.
ansible.builtin.fetch:
src: wg0-prod.conf
dest: wg0-prod.conf
flat: yes
- name: 7. Récupère le fichier de conf B.
ansible.builtin.fetch:
src: wg0-test.conf
dest: wg0-test.conf
flat: yes
- hosts: wg
become: true
tasks:
- name: 8. Envoie le fichier de conf A vers la machine prod.
copy:
src: wg0-prod.conf
dest: /etc/wireguard/wg0.conf
when: ansible_hostname == "ap31-prod"
- name: 9. Envoie le fichier de conf B vers la machine test.
copy:
src: wg0-test.conf
dest: /etc/wireguard/wg0.conf
when: ansible_hostname == "ap31-test"
- name: 10. Envoie le fichier de conf C vers la machine mon.
copy:
src: wg0-mon.conf
dest: /etc/wireguard/wg0.conf
when: ansible_hostname == "ap31-mon"
- name: 11. Active le service Wireguard.
ansible.builtin.service:
service: wg-quick@wg0.service
enabled: true
- name: 12. Lance le service Wireguard.
ansible.builtin.service:
service: wg-quick@wg0.service
state: restarted

View File

@@ -0,0 +1,17 @@
# local settings for ap31-mon
[Interface]
PrivateKey = qNrA3qizZyJ7iPfXQepwA6/g5kkabijifGUW61OO4G4=
Address = 10.0.0.1/32
ListenPort = 51820
# remote settings for ap31-prod
[Peer]
PublicKey = mnWYL52q8bzvvary6s81ROHV1hohu3nlQN0YQkTZTzg=
Endpoint = 172.16.0.100:51820
AllowedIPs = 10.0.0.2/32
# remote settings for ap31-test
[Peer]
PublicKey= Amfx3tgY9WyjhVH8gQJWZpbQVNYwpIt3OFZokMqq43M=
Endpoint = 172.16.0.101:51820
AllowedIPs = 10.0.0.3/32

View File

@@ -0,0 +1,11 @@
# local settings for ap31-prod
[Interface]
PrivateKey = ENtiBbOg56P0DQJhoYe61mNLF06Vtv601rbAAvyKz2M=
Address = 10.0.0.2/32
ListenPort = 51820
# remote settings for ap31-mon
[Peer]
PublicKey = LEkM9UVt21zq7PS8sX+SBihuXr5k9bjAbwSN0dChmAk=
Endpoint = 172.16.0.102:51820
AllowedIPs = 10.0.0.1/32

View File

@@ -0,0 +1,11 @@
# local settings for ap31-test
[Interface]
PrivateKey = 6Hq3I9MdSB/O8xiXxf1ekysWR2GQgYBss9VNDngbZE4=
Address = 10.0.0.3/32
ListenPort = 51820
# remote settings for ap31-mon
[Peer]
PublicKey = LEkM9UVt21zq7PS8sX+SBihuXr5k9bjAbwSN0dChmAk=
Endpoint = 172.16.0.102:51820
AllowedIPs = 10.0.0.1/32