Compare commits

...

6 Commits

8 changed files with 56 additions and 75 deletions

View File

@ -2,20 +2,28 @@
wg_interface: wg0
wg_port: 51820
#wg_server_public_interface: eth0
wg_server_address: 192.168.99.1/32
wg_server_address: 192.168.98.1/32
#wg_server_private_key: "{{ wg_server_private_key }}"
wg_peers:
- name: fabio_test
publicKey: "byR/8T9AZK2t1cxDCLVzdLXsxcUPRXA06CnfI8gwQyY="
ip: "192.168.99.4/32"
- name: fabio
ip: "192.168.98.11/32"
allowedIPs :
- "192.168.99.0/24"
- "10.22.0.0.0/16"
- "192.168.98.0/24"
- "10.22.0.0/16"
- name: lucio
publicKey: "IifwTYaBMoL3IhAHHplyuVMCir7PHNT57cP57RvEIwg="
ip: "192.168.99.3/32"
ip: "192.168.98.12/32"
allowedIPs :
- "192.168.99.0/24"
- "10.22.0.0.0/16"
- "192.168.98.0/24"
- "10.22.0.0/16"
- name: roberto
ip: "192.168.98.13/32"
allowedIPs :
- "192.168.98.0/24"
- "10.22.0.0/16"
- name: giorgio
ip: "192.168.98.14/32"
allowedIPs :
- "192.168.98.0/24"
- "10.22.0.0/16"

View File

@ -18,7 +18,8 @@ wg_peers:
allowedIPs :
- "192.168.99.0/24"
- "10.22.0.0/16"
- name: second_missing_key_test
- name: forced_missing_key_test
forceRegeneration: True
ip: "192.168.99.6/32"
allowedIPs :
- "192.168.99.0/24"

View File

@ -0,0 +1,20 @@
---
- name: Check if config already exists
stat:
path: "/etc/wireguard/{{ wg_interface }}.conf"
register: interface_configuration
- name: Parse configuration
ansible.utils.cli_parse:
command: "cat /etc/wireguard/{{ wg_interface }}.conf "
parser:
name: ansible.netcommon.native
template_path: templates/wg_config_template.yaml
set_fact: existing_wg_config
when: interface_configuration.stat.exists
- name: Updating client configs info
set_fact:
wg_peers: "{{ wg_peers | community.general.lists_mergeby([{'name':item.key,'publicKey':item.value.publicKey}] ,'name')}}"
when: item.key in (wg_peers | map(attribute='name'))
loop: "{{ existing_wg_config | ansible.builtin.dict2items }}"

View File

@ -14,7 +14,7 @@
- name : Gather needed key generation
set_fact:
generated_keys: "{{ generated_keys + [{'name': item.name}] }}"
when : item.publicKey is not defined
when : item.publicKey is not defined or (item.forceRegeneration is defined and item.forceRegeneration)
loop: "{{ wg_peers }}"
- name : Generate private keys

View File

@ -1,5 +1,6 @@
---
- include_tasks: install_wireguard.yaml
- include_tasks: gather_current_config.yaml
- include_tasks: generate_server_keys.yaml
- include_tasks: generate_client_configs.yaml
- include_tasks: configure_server.yaml

View File

@ -1,62 +0,0 @@
---
- name: Install Wireguard Server
apt:
pkg:
- wireguard
state: latest
update_cache: true
- name: Create directory for wg keys
ansible.builtin.file:
path: /etc/wireguard/keys
state: directory
mode: '0755'
- name: Creating server privatekey and publickey
shell: wg genkey | tee privatekey | wg pubkey > publickey
args:
chdir: /etc/wireguard/keys
- name: Get Private Key [privatekey => var_privatekey]
shell: cat privatekey
register: var_privatekey
args:
chdir: /etc/wireguard/keys
#- name: Add WireGuard interface
# command: ip link add dev wg0 type wireguard
- name: Updating configuration
template:
src: wireguard_server.jinja
dest: /etc/wireguard/wg0.conf
#- name: Activating link
# command: ip link set up dev wg0
- name: Starting wg service
systemd:
state: started
name: wg-quick@wg0
enabled: yes
- name: Getting public key
shell: cat publickey
register: var_publickey
args:
chdir: /etc/wireguard/keys
- name: Check server public IP
shell: curl https://ipinfo.io/ip
register: var_server_ip
- name: Printing public key
debug:
msg: "Server {{ ansible_hostname }} reachable @{{var_server_ip}}. Public key is {{ var_publickey }}"

View File

@ -0,0 +1,13 @@
---
- example: "# Peer ID : fabio_test"
getval: '# Peer ID : (?P<peer_id>\S+)'
result:
"{{ peer_id }}":
name: "{{ peer_id }}"
shared: true
- example: "PublicKey = 9a3tgXpF5CAOffbHZdW1QBEJgSLFnBDVbD1JaMPgzkM="
getval: 'PublicKey = (?P<publicKey>\S+)'
result:
"{{ peer_id }}":
publicKey: "{{ publicKey }}"

View File

@ -16,7 +16,7 @@ PostDown = iptables -t nat -D POSTROUTING ! -o {{wg_interface}} -m mark --mark 0
{% for peer in wg_peers %}
# {{ peer.name }}
# Peer ID : {{ peer.name }}
[Peer]
PublicKey = {{ peer.publicKey }}
AllowedIPs = {{ peer.ip }}