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
|
|
|
|
|
|
|
|
- name: Create WireGuard certificates directory
|
|
|
|
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 }}"
|
2018-08-12 20:21:45 +00:00
|
|
|
mode: 0700
|
|
|
|
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 private key file
|
|
|
|
set_fact:
|
2019-01-07 21:49:06 +00:00
|
|
|
private_key_file_path: "{{ wireguard_cert_directory }}/{{ inventory_hostname }}.private.key"
|
2018-08-12 20:21:45 +00:00
|
|
|
tags:
|
|
|
|
wg-generate-keys
|
|
|
|
|
|
|
|
- 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
|
|
|
|
|
|
|
|
- name: Register if private key already exists
|
2019-01-07 21:49:06 +00:00
|
|
|
stat:
|
|
|
|
path: "{{ private_key_file_path }}"
|
2018-08-12 20:21:45 +00:00
|
|
|
register: private_key_file_stat
|
2019-01-07 21:49:06 +00:00
|
|
|
delegate_to: localhost
|
2018-08-12 20:21:45 +00:00
|
|
|
tags:
|
|
|
|
- wg-generate-keys
|
|
|
|
|
|
|
|
- name: Generate WireGuard private key
|
|
|
|
shell: "wg genkey"
|
|
|
|
register: wg_private_key_result
|
2019-01-07 21:49:06 +00:00
|
|
|
when: not private_key_file_stat.stat.exists
|
2018-08-12 20:21:45 +00:00
|
|
|
tags:
|
|
|
|
- wg-generate-keys
|
2019-01-07 21:56:20 +00:00
|
|
|
- skip_ansible_lint
|
2018-08-12 20:21:45 +00:00
|
|
|
|
|
|
|
- name: Set private key fact
|
|
|
|
set_fact:
|
2019-09-26 20:36:36 +00:00
|
|
|
wg_private_key: "{{ wg_private_key_result.stdout }}"
|
2019-01-07 21:49:06 +00:00
|
|
|
when: not private_key_file_stat.stat.exists
|
2018-08-12 20:21:45 +00:00
|
|
|
tags:
|
|
|
|
- wg-generate-keys
|
|
|
|
|
|
|
|
- name: Generate WireGuard public key
|
2019-01-07 21:49:06 +00:00
|
|
|
shell: "echo '{{ wg_private_key }}' | wg pubkey"
|
2018-08-12 20:21:45 +00:00
|
|
|
register: wg_public_key_result
|
2019-01-07 21:49:06 +00:00
|
|
|
when: not private_key_file_stat.stat.exists
|
2018-08-12 20:21:45 +00:00
|
|
|
tags:
|
|
|
|
- wg-generate-keys
|
|
|
|
|
|
|
|
- name: Set public key fact
|
|
|
|
set_fact:
|
2019-09-26 20:36:36 +00:00
|
|
|
wg_public_key: "{{ wg_public_key_result.stdout }}"
|
2019-01-07 21:49:06 +00:00
|
|
|
when: not private_key_file_stat.stat.exists
|
2018-08-12 20:21:45 +00:00
|
|
|
tags:
|
|
|
|
- wg-generate-keys
|
|
|
|
|
|
|
|
- name: Store hosts private key locally
|
2019-01-07 21:49:06 +00:00
|
|
|
template:
|
2018-08-12 20:21:45 +00:00
|
|
|
src: "wg-privatekey.j2"
|
2019-01-07 21:49:06 +00:00
|
|
|
dest: "{{ private_key_file_path }}"
|
|
|
|
owner: "{{ wireguard_cert_owner }}"
|
|
|
|
group: "{{ wireguard_cert_group }}"
|
2018-08-12 20:21:45 +00:00
|
|
|
mode: 0644
|
2019-01-07 21:49:06 +00:00
|
|
|
when: not private_key_file_stat.stat.exists
|
|
|
|
delegate_to: localhost
|
2018-08-12 20:21:45 +00:00
|
|
|
tags:
|
|
|
|
- wg-generate-keys
|
|
|
|
|
|
|
|
- name: Store hosts public key locally
|
2019-01-07 21:49:06 +00:00
|
|
|
template:
|
2018-08-12 20:21:45 +00:00
|
|
|
src: "wg-publickey.j2"
|
2019-01-07 21:49:06 +00:00
|
|
|
dest: "{{ public_key_file_path }}"
|
|
|
|
owner: "{{ wireguard_cert_owner }}"
|
|
|
|
group: "{{ wireguard_cert_group }}"
|
2018-08-12 20:21:45 +00:00
|
|
|
mode: 0644
|
2019-01-07 21:49:06 +00:00
|
|
|
when: not private_key_file_stat.stat.exists
|
|
|
|
delegate_to: localhost
|
2018-08-12 20:21:45 +00:00
|
|
|
tags:
|
|
|
|
- wg-generate-keys
|
|
|
|
|
|
|
|
- name: Read private key
|
|
|
|
set_fact:
|
2019-01-07 21:49:06 +00:00
|
|
|
private_key: "{{ lookup('file', private_key_file_path) }}"
|
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
|
|
|
|
|
|
|
|
- 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
|