1
0
Fork 0
This repository has been archived on 2020-08-04. You can view files and clone it, but cannot push or open issues or pull requests.
ansible-role-wireguard/tasks/main.yml

133 lines
3 KiB
YAML
Raw Normal View History

---
- name: Gather instance facts
setup:
- include_tasks: "setup-{{ ansible_distribution|lower }}.yml"
- name: Install WireGuard
package:
name: "{{ packages }}"
state: present
vars:
packages:
- 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:
wireguard_ip: "{{ wireguard_address.split('/')[0] }}"
- name: Register if config/private key already exists on target host
stat:
path: "{{ wireguard_remote_directory }}/{{ wireguard_interface }}.conf"
register: config_file_stat
tags:
- wg-generate-keys
- wg-config
2019-10-18 21:14:30 +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
2019-10-18 17:54:10 +00:00
when: not config_file_stat.stat.exists
2019-10-18 21:14:30 +00:00
- block:
- name: Read WireGuard config file
slurp:
src: "{{ wireguard_remote_directory }}/{{ wireguard_interface }}.conf"
register: wg_config
tags:
- wg-config
2019-10-18 21:14:30 +00:00
- 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
2019-10-18 17:27:55 +00:00
- name: Derive WireGuard public key
shell: "echo '{{ private_key }}' | wg pubkey" # noqa 306
2019-10-18 17:27:55 +00:00
register: wg_public_key_result
changed_when: false
tags:
- wg-config
- name: Set public key fact
set_fact:
2019-10-18 17:27:55 +00:00
public_key: "{{ wg_public_key_result.stdout }}"
tags:
2019-10-18 17:54:10 +00:00
- wg-config
- name: Create WireGuard configuration directory
file:
dest: "{{ wireguard_remote_directory }}"
state: directory
mode: 0700
tags:
- wg-config
- name: Generate WireGuard configuration file
template:
src: wg.conf.j2
dest: "{{ wireguard_remote_directory }}/{{ wireguard_interface }}.conf"
owner: root
group: root
mode: 0600
tags:
- wg-config
notify:
- restart wireguard
2019-10-18 17:38:34 +00:00
- name: Check if reload-module-on-update is set
2019-10-30 05:50:00 +00:00
stat:
path: "{{ wireguard_remote_directory }}/.reload-module-on-update"
register: reload_module_on_update
2019-10-18 17:54:10 +00:00
tags:
- wg-config
2019-10-18 17:38:34 +00:00
- 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
2019-10-18 17:54:10 +00:00
tags:
- wg-config
2019-10-30 05:50:00 +00:00
- name: Start and enable WireGuard service
service:
name: "wg-quick@{{ wireguard_interface }}"
state: started
enabled: yes
2019-10-18 16:40:47 +00:00
2019-10-18 17:27:55 +00:00
- name: Delete local cert directory
2019-10-18 16:40:47 +00:00
file:
2019-10-18 17:27:55 +00:00
path: "{{ wireguard_cert_directory }}"
2019-10-18 16:40:47 +00:00
state: absent
delegate_to: localhost
run_once: true
2019-10-18 17:54:10 +00:00
tags:
- wg-config