From 6eb4f866213f39de86be23a9c36dea6b6bb8e907 Mon Sep 17 00:00:00 2001
From: "alexandr.benzonan" <alexandra.benzonana@etu.hesge.ch>
Date: Tue, 11 Apr 2023 15:32:29 +0200
Subject: [PATCH] clean part 3 using variables

---
 playbook_2.yaml   | 16 +++-------------
 vars2.yml         | 16 ++++++++++++++++
 vault.yml         | 23 ++++++++++++-----------
 wireguard_conf.j2 | 25 ++++++-------------------
 4 files changed, 37 insertions(+), 43 deletions(-)
 create mode 100644 vars2.yml

diff --git a/playbook_2.yaml b/playbook_2.yaml
index 41f7413..6bcb86d 100644
--- a/playbook_2.yaml
+++ b/playbook_2.yaml
@@ -35,19 +35,9 @@
 
 - name: configure wireguard
   hosts: hosts
-  vars_files: vault.yml
-  vars:
-    h1_port: 53610
-    h2_port: 58970
-    h2_public_key: "NG7C20yfj0r5KbhKqC4hVNtBoOpvFxhQ9Do+KcPxcmk="
-    h2_ip: "10.0.0.2"
-    h1_ip: "10.0.0.1"
-    h1_public_key: "8pbE4PtVZk283L5Mrmn1XE04xXe1p1jPWAqUowQnBmE="
-    h2_AllowedIPs: "10.0.0.1/32"
-    h1_AllowedIPs: "10.0.0.2/32"
-    h1_endpoint: "3.0.0.3"
-    h2_endpoint: "1.0.0.3"
-    ifname: wg0
+  vars_files: 
+    - vars2.yml
+    - vault.yml
   tasks:
     - name: config wireguard
       template:
diff --git a/vars2.yml b/vars2.yml
new file mode 100644
index 0000000..fba953b
--- /dev/null
+++ b/vars2.yml
@@ -0,0 +1,16 @@
+all:  
+  ifname: wg0
+  H1:
+    port: 53610
+    other_port: 58970
+    ip: "10.0.0.1"
+    AllowedIPs: "10.0.0.2/32"
+    other_public_key: "NG7C20yfj0r5KbhKqC4hVNtBoOpvFxhQ9Do+KcPxcmk="
+    endpoint: "3.0.0.3"
+  H2:
+    port: 58970
+    other_port: 53610
+    ip: "10.0.0.2"
+    other_public_key: "8pbE4PtVZk283L5Mrmn1XE04xXe1p1jPWAqUowQnBmE="
+    AllowedIPs: "10.0.0.1/32"
+    endpoint: "1.0.0.3"
\ No newline at end of file
diff --git a/vault.yml b/vault.yml
index 8e34216..f6ec662 100644
--- a/vault.yml
+++ b/vault.yml
@@ -1,12 +1,13 @@
 $ANSIBLE_VAULT;1.1;AES256
-30376437623835666561646530366363363366323331353237393935623365623766666437313366
-6139393331316437663165366133653637363131613938340a343865366364666431636335323261
-38633933623064316630376337353565326665373434363661326430633964383466303462633165
-3939326435633761320a393536326365313964666163383963313535326231303063393964656530
-64346565613664393932396235643139653138343734373664663939616533383661373266326263
-33333861626330663231393666333538353964373639376661353065313761316665366637633533
-39323439613231363038653162333765366639643232333437326361623661303538326536636464
-37303439663166663536333435363866306637396238343939303563383132376233343532643064
-64613661303464366131366332386566633330626433313238383463346430306362376666323835
-65653232313834373961653662373061373535663930656331656635663630386134663864313163
-366339613664626231656531313830653837
+39333264633735383236613566343736653131373862343939333835346632313736616332623461
+6463626532633265366631353932666536313832373836660a303436316462613531393030333761
+63653138376362356335373161323533323138363363396336636162373539343333353635313063
+6531656662343461300a353265383739333461616365613262333461393232653739343739653465
+65303564303034316236653865623939643134373631633532343037656466313637326330303533
+62613930316231333531613438316532343030653132326632643864623432616366396438396133
+39376264643632663530646565383938326130613662616265666331316662323133373137323936
+38616136363434663536613030303461373663653736303565663632363430326634383264326464
+32666239303662643839666239343031306661303333626561333330306633333166663436366463
+32626135326235663035386165326531353534353739613663656437653230346363636165643634
+34346330613035623734386665306234386431633762303430373734613164373563316536626136
+33346133316232303130
diff --git a/wireguard_conf.j2 b/wireguard_conf.j2
index d51f28c..c8984be 100644
--- a/wireguard_conf.j2
+++ b/wireguard_conf.j2
@@ -1,22 +1,9 @@
 [Interface]
-{% if inventory_hostname == "H1" %}
-Address = {{h1_ip}}
-ListenPort = {{h1_port}}
-PrivateKey = {{all.h1_private_key}}
-{% else %}
-Address = {{h2_ip}}
-ListenPort = {{h2_port}}
-PrivateKey = {{all.h2_private_key}}
-{% endif %}
+Address = {{all[inventory_hostname].ip}}
+ListenPort = {{all[inventory_hostname].port}}
+PrivateKey = {{pk[inventory_hostname].private_key}}
 
 [Peer]
-{% if inventory_hostname == "H1" %}
-PublicKey = {{h2_public_key}}
-AllowedIPs = {{h1_AllowedIPs}}
-Endpoint = {{h1_endpoint}}:{{h2_port}}
-{% else %}
-PublicKey = {{h1_public_key}}
-AllowedIPs = {{h2_AllowedIPs}}
-Endpoint = {{h2_endpoint}}:{{h1_port}}
-{% endif %}
-
+PublicKey = {{all[inventory_hostname].other_public_key}}
+AllowedIPs = {{all[inventory_hostname].AllowedIPs}}
+Endpoint = {{all[inventory_hostname].endpoint}}:{{all[inventory_hostname].other_port}}
\ No newline at end of file
-- 
GitLab