2018-08-12 20:21:45 +00:00
|
|
|
---
|
|
|
|
- name: Gather instance facts
|
|
|
|
setup:
|
|
|
|
|
2019-02-06 19:56:06 +00:00
|
|
|
- include_tasks: "setup-{{ ansible_distribution|lower }}.yml"
|
2018-08-12 20:21:45 +00:00
|
|
|
|
|
|
|
- name: Install WireGuard
|
|
|
|
package:
|
2019-06-04 19:51:25 +00:00
|
|
|
name: "{{ packages }}"
|
2018-08-12 20:21:45 +00:00
|
|
|
state: present
|
2019-06-04 19:51:25 +00:00
|
|
|
vars:
|
|
|
|
packages:
|
2018-08-12 20:21:45 +00:00
|
|
|
- wireguard-dkms
|
|
|
|
- wireguard-tools
|
|
|
|
tags:
|
|
|
|
- wg-install
|
2019-01-07 21:56:20 +00:00
|
|
|
- skip_ansible_lint
|
2018-08-12 20:21:45 +00:00
|
|
|
|
|
|
|
- name: Enable WireGuard kernel module
|
|
|
|
modprobe:
|
|
|
|
name: wireguard
|
|
|
|
state: present
|
|
|
|
register: wireguard_module_enabled
|
|
|
|
until: wireguard_module_enabled is succeeded
|
|
|
|
retries: 10
|
|
|
|
delay: 10
|
|
|
|
failed_when: wireguard_module_enabled is failure
|
|
|
|
tags:
|
|
|
|
- wg-install
|
|
|
|
|
2019-10-18 16:38:45 +00:00
|
|
|
- name: Create WireGuard public key directory locally
|
2018-08-12 20:21:45 +00:00
|
|
|
file:
|
2019-01-07 21:49:06 +00:00
|
|
|
dest: "{{ wireguard_cert_directory }}"
|
2018-08-12 20:21:45 +00:00
|
|
|
state: directory
|
2019-01-07 21:49:06 +00:00
|
|
|
owner: "{{ wireguard_cert_owner }}"
|
|
|
|
group: "{{ wireguard_cert_group }}"
|
2019-10-18 16:38:45 +00:00
|
|
|
mode: 0755
|
2018-08-12 20:21:45 +00:00
|
|
|
run_once: true
|
|
|
|
delegate_to: localhost
|
|
|
|
tags:
|
|
|
|
wg-generate-keys
|
|
|
|
|
|
|
|
- name: Set WireGuard IP (without mask)
|
|
|
|
set_fact:
|
2019-01-07 21:49:06 +00:00
|
|
|
wireguard_ip: "{{ wireguard_address.split('/')[0] }}"
|
2018-08-12 20:21:45 +00:00
|
|
|
|
|
|
|
- name: Set path to public key file
|
|
|
|
set_fact:
|
2019-01-07 21:49:06 +00:00
|
|
|
public_key_file_path: "{{ wireguard_cert_directory }}/{{ inventory_hostname }}.public.key"
|
2018-08-12 20:21:45 +00:00
|
|
|
tags:
|
|
|
|
wg-generate-keys
|
|
|
|
|
2019-10-18 16:38:45 +00:00
|
|
|
- name: Register if config/private key already exists on target host
|
2019-01-07 21:49:06 +00:00
|
|
|
stat:
|
2019-10-18 16:38:45 +00:00
|
|
|
path: "{{ wireguard_remote_directory }}/{{ wireguard_interface }}.conf"
|
|
|
|
register: config_file_stat
|
2018-08-12 20:21:45 +00:00
|
|
|
tags:
|
|
|
|
- wg-generate-keys
|
|
|
|
|
2019-10-18 16:38:45 +00:00
|
|
|
- block:
|
|
|
|
- name: Generate WireGuard private key
|
|
|
|
shell: "wg genkey"
|
|
|
|
register: wg_private_key_result
|
|
|
|
tags:
|
|
|
|
- wg-generate-keys
|
|
|
|
- skip_ansible_lint
|
|
|
|
|
|
|
|
- name: Generate WireGuard public key
|
|
|
|
shell: "echo '{{ wg_private_key }}' | wg pubkey"
|
|
|
|
register: wg_public_key_result
|
|
|
|
tags:
|
|
|
|
- wg-generate-keys
|
|
|
|
|
|
|
|
- name: Set public key fact
|
|
|
|
set_fact:
|
|
|
|
wg_public_key: "{{ wg_public_key_result.results[0].stdout }}"
|
|
|
|
tags:
|
|
|
|
- wg-generate-keys
|
|
|
|
|
|
|
|
- name: Store hosts public key locally
|
|
|
|
template:
|
|
|
|
src: "wg-publickey.j2"
|
|
|
|
dest: "{{ public_key_file_path }}"
|
|
|
|
owner: "{{ wireguard_cert_owner }}"
|
|
|
|
group: "{{ wireguard_cert_group }}"
|
|
|
|
mode: 0644
|
|
|
|
delegate_to: localhost
|
|
|
|
tags:
|
|
|
|
- wg-generate-keys
|
|
|
|
when: not config_file_stat.stat.exists
|
|
|
|
|
|
|
|
- name: Read WireGuard config file
|
|
|
|
slurp:
|
|
|
|
src: "{{ wireguard_remote_directory }}/{{ wireguard_interface }}.conf"
|
|
|
|
register: wg_config
|
2018-08-12 20:21:45 +00:00
|
|
|
|
|
|
|
- name: Read private key
|
|
|
|
set_fact:
|
2019-10-18 16:38:45 +00:00
|
|
|
private_key: "{{ wg_config['content'] | b64decode | regex_findall('PrivateKey = (.*)') | first }}"
|
2018-08-12 20:21:45 +00:00
|
|
|
tags:
|
|
|
|
wg-config
|
|
|
|
|
|
|
|
- name: Read public key
|
|
|
|
set_fact:
|
2019-01-07 21:49:06 +00:00
|
|
|
public_key: "{{ lookup('file', public_key_file_path) }}"
|
2018-08-12 20:21:45 +00:00
|
|
|
tags:
|
|
|
|
wg-config
|
|
|
|
|
|
|
|
- name: Create WireGuard configuration directory
|
|
|
|
file:
|
2019-01-07 21:49:06 +00:00
|
|
|
dest: "{{ wireguard_remote_directory }}"
|
2018-08-12 20:21:45 +00:00
|
|
|
state: directory
|
|
|
|
mode: 0700
|
|
|
|
tags:
|
|
|
|
- wg-config
|
|
|
|
|
|
|
|
- name: Generate WireGuard configuration file
|
|
|
|
template:
|
|
|
|
src: wg.conf.j2
|
2019-01-07 21:49:06 +00:00
|
|
|
dest: "{{ wireguard_remote_directory }}/{{ wireguard_interface }}.conf"
|
2018-08-12 20:21:45 +00:00
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
mode: 0600
|
|
|
|
tags:
|
|
|
|
- wg-config
|
|
|
|
notify:
|
|
|
|
- restart wireguard
|
2019-10-18 16:43:07 +00:00
|
|
|
|
|
|
|
- name: check if reload-module-on-update file exists
|
|
|
|
stat:
|
|
|
|
path: "{{ wireguard_remote_directory }}/.reload-module-on-update"
|
|
|
|
register: reload_module_on_update
|
|
|
|
|
|
|
|
- name: WireGuard reload-module-on-update
|
|
|
|
file:
|
|
|
|
dest: "{{ wireguard_remote_directory }}/.reload-module-on-update"
|
|
|
|
state: touch
|
|
|
|
when: not reload_module_on_update.stat.exists
|
|
|
|
|
2018-08-12 20:21:45 +00:00
|
|
|
- name: Start and enable WireGuard service
|
|
|
|
service:
|
2019-01-07 21:49:06 +00:00
|
|
|
name: "wg-quick@{{ wireguard_interface }}"
|
2018-08-12 20:21:45 +00:00
|
|
|
state: started
|
|
|
|
enabled: yes
|
2019-10-18 16:40:47 +00:00
|
|
|
|
|
|
|
- name: Look for local private key
|
|
|
|
find:
|
|
|
|
paths: "{{ wireguard_cert_directory }}"
|
|
|
|
patterns: "*.private.key"
|
|
|
|
register: local_private_key_to_delete
|
|
|
|
delegate_to: localhost
|
|
|
|
run_once: true
|
|
|
|
|
|
|
|
- name: Delete local private key
|
|
|
|
file:
|
|
|
|
path: "{{ item.path }}"
|
|
|
|
state: absent
|
|
|
|
with_items: "{{ local_private_key_to_delete.files }}"
|
|
|
|
delegate_to: localhost
|
|
|
|
run_once: true
|