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
|
|
|
|
|
|
|
|
- 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
|
|
|
|
|
|
|
|
- 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
|
|
|
|
2019-11-02 19:39:47 +00:00
|
|
|
- name: Register if config/private key already exists on target host
|
2019-01-07 21:49:06 +00:00
|
|
|
stat:
|
2019-11-02 19:39:47 +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-11-02 19:39:47 +00:00
|
|
|
- wg-config
|
2018-08-12 20:21:45 +00:00
|
|
|
|
2020-01-26 22:30:16 +00:00
|
|
|
- name: Get wg subcommands
|
|
|
|
command: "wg --help"
|
|
|
|
register: wg_subcommands
|
2020-02-16 14:36:47 +00:00
|
|
|
changed_when: false
|
2020-01-26 22:30:16 +00:00
|
|
|
|
|
|
|
- name: Set default value for wg_syncconf variable (assume wg syncconf subcommand not available)
|
|
|
|
set_fact:
|
|
|
|
wg_syncconf: false
|
|
|
|
|
|
|
|
- name: Check if wg syncconf subcommand is available
|
|
|
|
set_fact:
|
|
|
|
wg_syncconf: true
|
|
|
|
when: wg_subcommands.stdout | regex_search('syncconf:')
|
|
|
|
|
|
|
|
- name: Show syncconf subcommand status
|
|
|
|
debug:
|
|
|
|
var: wg_syncconf
|
|
|
|
|
2019-11-02 19:39:47 +00:00
|
|
|
- block:
|
|
|
|
- name: Generate WireGuard private key
|
|
|
|
shell: "wg genkey"
|
|
|
|
register: wg_private_key_result
|
|
|
|
tags:
|
|
|
|
- wg-generate-keys
|
|
|
|
|
|
|
|
- name: Set private key fact
|
|
|
|
set_fact:
|
|
|
|
private_key: "{{ wg_private_key_result.stdout }}"
|
|
|
|
tags:
|
|
|
|
- wg-generate-keys
|
|
|
|
when: not config_file_stat.stat.exists
|
|
|
|
|
|
|
|
- block:
|
|
|
|
- name: Read WireGuard config file
|
|
|
|
slurp:
|
|
|
|
src: "{{ wireguard_remote_directory }}/{{ wireguard_interface }}.conf"
|
|
|
|
register: wg_config
|
|
|
|
tags:
|
|
|
|
- wg-config
|
|
|
|
|
|
|
|
- name: Set private key fact
|
|
|
|
set_fact:
|
|
|
|
private_key: "{{ wg_config['content'] | b64decode | regex_findall('PrivateKey = (.*)') | first }}"
|
|
|
|
tags:
|
|
|
|
- wg-config
|
|
|
|
when: config_file_stat.stat.exists
|
|
|
|
|
|
|
|
- name: Derive WireGuard public key
|
|
|
|
shell: "echo '{{ private_key }}' | wg pubkey" # noqa 306
|
2018-08-12 20:21:45 +00:00
|
|
|
register: wg_public_key_result
|
2019-11-02 19:39:47 +00:00
|
|
|
changed_when: false
|
2018-08-12 20:21:45 +00:00
|
|
|
tags:
|
2019-11-02 19:39:47 +00:00
|
|
|
- wg-config
|
2018-08-12 20:21:45 +00:00
|
|
|
|
|
|
|
- name: Set public key fact
|
|
|
|
set_fact:
|
2019-11-02 19:39:47 +00:00
|
|
|
public_key: "{{ wg_public_key_result.stdout }}"
|
2018-08-12 20:21:45 +00:00
|
|
|
tags:
|
2019-11-02 19:39:47 +00:00
|
|
|
- wg-config
|
2018-08-12 20:21:45 +00:00
|
|
|
|
|
|
|
- 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
|
|
|
|
|
2020-04-04 19:30:45 +00:00
|
|
|
- block:
|
|
|
|
- name: Create private key for unmanaged hosts
|
|
|
|
shell: |
|
|
|
|
set -o errexit
|
|
|
|
set -o pipefail
|
|
|
|
wg genkey | tee {{ wireguard_remote_directory }}/{{ item.host }}-privatekey
|
|
|
|
exit 0
|
|
|
|
args:
|
2020-07-12 21:55:01 +00:00
|
|
|
executable: /bin/bash
|
2020-04-04 19:30:45 +00:00
|
|
|
creates: "{{ wireguard_remote_directory }}/{{ item.host }}-privatekey"
|
|
|
|
register: uh_private_key
|
|
|
|
with_items: "{{ wireguard_unmanaged_hosts | default([]) }}"
|
|
|
|
|
|
|
|
- name: Validate permissions of unmanaged hosts' private keys
|
|
|
|
file:
|
|
|
|
path: "{{ wireguard_remote_directory }}/{{ item.host }}-privatekey"
|
|
|
|
mode: '0400'
|
|
|
|
with_items: "{{ wireguard_unmanaged_hosts | default([]) }}"
|
|
|
|
|
|
|
|
- name: Recover existing private key for unmanaged hosts
|
|
|
|
shell: "cat {{ wireguard_remote_directory }}/{{ item.host }}-privatekey"
|
|
|
|
register: uh_private_key
|
|
|
|
changed_when: false
|
|
|
|
with_items: "{{ wireguard_unmanaged_hosts | default([]) }}"
|
|
|
|
|
|
|
|
- name: Derive WireGuard public key for unmanaged hosts
|
|
|
|
shell: "cat {{ wireguard_remote_directory }}/{{ item.host }}-privatekey | wg pubkey | tee {{ wireguard_remote_directory }}/{{ item.host }}-pubkey"
|
|
|
|
args:
|
|
|
|
creates: "{{ wireguard_remote_directory }}/{{ item.host }}-pubkey"
|
2020-04-04 19:35:01 +00:00
|
|
|
executable: "/bin/bash"
|
2020-04-04 19:31:51 +00:00
|
|
|
register: uh_public_key
|
2020-04-04 19:30:45 +00:00
|
|
|
with_items: "{{ wireguard_unmanaged_hosts | default([]) }}"
|
|
|
|
|
|
|
|
- name: Recover existing public key for unmanaged hosts
|
|
|
|
shell: "cat {{ wireguard_remote_directory }}/{{ item.host }}-pubkey"
|
2020-04-04 19:31:51 +00:00
|
|
|
register: uh_public_key
|
2020-04-04 19:30:45 +00:00
|
|
|
changed_when: false
|
|
|
|
with_items: "{{ wireguard_unmanaged_hosts | default([]) }}"
|
|
|
|
when: wireguard_unmanaged_hosts is defined and wireguard_unmanaged_hosts
|
2020-02-16 14:36:47 +00:00
|
|
|
|
2018-08-12 20:21:45 +00:00
|
|
|
- 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:
|
2020-01-26 22:30:16 +00:00
|
|
|
- reconfigure wireguard
|
2018-08-12 20:21:45 +00:00
|
|
|
|
2020-02-16 14:36:47 +00:00
|
|
|
- name: Generate WireGuard configuration file for unmanaged systems
|
|
|
|
template:
|
|
|
|
src: wg-unmanaged.conf.j2
|
|
|
|
dest: "{{ wireguard_remote_directory }}/{{ item.item.host }}.conf"
|
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
mode: 0600
|
2020-04-04 19:30:45 +00:00
|
|
|
with_items: "{{ uh_private_key.results }}"
|
2020-02-16 14:36:47 +00:00
|
|
|
|
2019-11-02 19:39:47 +00:00
|
|
|
- name: Check if reload-module-on-update is set
|
|
|
|
stat:
|
|
|
|
path: "{{ wireguard_remote_directory }}/.reload-module-on-update"
|
|
|
|
register: reload_module_on_update
|
|
|
|
tags:
|
|
|
|
- wg-config
|
|
|
|
|
|
|
|
- name: Set WireGuard reload-module-on-update
|
|
|
|
file:
|
|
|
|
dest: "{{ wireguard_remote_directory }}/.reload-module-on-update"
|
|
|
|
state: touch
|
|
|
|
when: not reload_module_on_update.stat.exists
|
|
|
|
tags:
|
|
|
|
- wg-config
|
|
|
|
|
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
|