a357e5fab1
* merge stateless with no storage of local priv key * Delete locally stored private key * add reload module on update config file * privatekey template is not used anymore * remove all local keys priv and public * use ansible_play_hosts instead of hardcoded vpn grp should use the group in the play calling the role. works fine when hosts bellong to several groups * Clean tasks names * add tag, and cleanup * fix private key creation * Support for mutliple wireguard vpn on same host add inventory exemple in readme * fix typo, add some comment on inventory * add wg-config tag to Check config: allow run with -t - wg-config * Update tasks/main.yml Co-Authored-By: Robert Wimmer <2039811+githubixx@users.noreply.github.com> * remove trailing whitespace * Update templates/wg.conf.j2 Co-Authored-By: Robert Wimmer <2039811+githubixx@users.noreply.github.com> * Update templates/wg.conf.j2 Co-Authored-By: Robert Wimmer <2039811+githubixx@users.noreply.github.com> * changes after githubixx code review * readd new line to separate peers in config
124 lines
2.8 KiB
YAML
124 lines
2.8 KiB
YAML
---
|
|
- 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
|
|
|
|
- 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
|
|
register: wg_public_key_result
|
|
changed_when: false
|
|
tags:
|
|
- wg-config
|
|
|
|
- name: Set public key fact
|
|
set_fact:
|
|
public_key: "{{ wg_public_key_result.stdout }}"
|
|
tags:
|
|
- 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
|
|
|
|
- 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
|
|
|
|
- name: Start and enable WireGuard service
|
|
service:
|
|
name: "wg-quick@{{ wireguard_interface }}"
|
|
state: started
|
|
enabled: yes
|