From 6517b8cdba13ef2f0c3e10a430ea07582e1ec312 Mon Sep 17 00:00:00 2001 From: githubixx Date: Tue, 17 Jul 2018 00:26:00 +0200 Subject: [PATCH] initial implementation - part 1 --- README.md | 39 +++++++++++- defaults/main.yml | 26 ++++++++ handlers/main.yml | 1 + meta/main.yml | 14 +++++ tasks/main.yml | 120 +++++++++++++++++++++++++++++++++++++ templates/wg-privatekey.j2 | 1 + templates/wg-publickey.j2 | 1 + tests/inventory | 2 + tests/test.yml | 5 ++ vars/main.yml | 1 + 10 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 defaults/main.yml create mode 100644 handlers/main.yml create mode 100644 meta/main.yml create mode 100644 tasks/main.yml create mode 100644 templates/wg-privatekey.j2 create mode 100644 templates/wg-publickey.j2 create mode 100644 tests/inventory create mode 100644 tests/test.yml create mode 100644 vars/main.yml diff --git a/README.md b/README.md index 39fa4c2..225dd44 100644 --- a/README.md +++ b/README.md @@ -1 +1,38 @@ -# ansible-role-wireguard \ No newline at end of file +Role Name +========= + +A brief description of the role goes here. + +Requirements +------------ + +Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required. + +Role Variables +-------------- + +A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well. + +Dependencies +------------ + +A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles. + +Example Playbook +---------------- + +Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: + + - hosts: servers + roles: + - { role: username.rolename, x: 42 } + +License +------- + +BSD + +Author Information +------------------ + +An optional section for the role authors to include contact information, or a website (HTML is not allowed). diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..245ddc3 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,26 @@ +--- +# The LOCAL directory where the Wireguard certificate is stored after it was +# generated. By default this will expand to user's LOCAL ${HOME} +# (the user that run's "ansible-playbook" command) plus +# "/wireguard/certs". That means if the user's ${HOME} directory is e.g. +# "/home/da_user" then "wireguard_cert_directory" will have a value of +# "/home/da_user/wireguard/certs". If you change this make sure that +# the parent directory is writable by the user that runs "ansible-playbook" +# command. +wireguard_cert_directory: "{{ '~/wireguard/certs' | expanduser }}" + +# Directory to store Wireguard configuration on the remote hosts +wireguard_remote_directory: "/etc/wireguard" + +# The port Wireguard will listen on. +wireguard_port: "51820" + +# The interface name that wireguard should use. +wireguard_interface: "wg0" + +wireguard_server_conf: | + [Interface] + PrivateKey = {{wg_server_privatekey }} + Address = {{wireguard_ip}} + ListenPort = {{wireguard_port}} + SaveConfig = true diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..ed97d53 --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1 @@ +--- diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..7ce2f35 --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,14 @@ +galaxy_info: + author: Robert Wimmer + description: Installs Wireguard incl. systemd integration + license: GPLv3 + min_ansible_version: 2.5 + platforms: + - name: Ubuntu + versions: + - bionic + galaxy_tags: + - networking + - security + - linux + - vpn diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..2209815 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,120 @@ +--- +- name: Determine kernel release + shell: "uname -r" + register: kernel_release + +- name: Install required packages + package: + name: "{{item}}" + state: present + with_items: + - software-properties-common + - linux-headers-{{kernel_release}} + +- name : Add WireGuard repository + apt_repository: + repo: "ppa:wireguard/wireguard" + state: present + update_cache: yes + +- name: Install WireGuard + package: + name: "{{item}}" + state: present + with_items: + - wireguard-dkms + - wireguard-tools + +- 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 + +- name: Create WireGuard certificates directory + file: + dest: "{{wireguard_cert_directory}}" + state: directory + mode: 0700 + run_once: true + delegate_to: localhost + +- name: Register if private key already exists + local_action: + module: stat + path: "{{wireguard_cert_directory}}/{{inventory_hostname}}.private.key" + register: private_key_file + tags: + - wg-generate-keys + +- name: Generate WireGuard private key + shell: "wg genkey" + register: wg_private_key_result + with_inventory_hostnames: + - vpn + when: private_key_file.stat.exists == False + tags: + - wg-generate-keys + +- name: Set private key fact + set_fact: + wg_private_key: "{{wg_private_key_result.results[0].stdout}}" + when: private_key_file.stat.exists == False + tags: + - wg-generate-keys + +- name: Generate WireGuard public key + shell: "echo '{{wg_private_key}}' | wg pubkey" + register: wg_public_key_result + when: private_key_file.stat.exists == False + with_inventory_hostnames: + - vpn + tags: + - wg-generate-keys + +- name: Set public key fact + set_fact: + wg_public_key: "{{wg_public_key_result.results[0].stdout}}" + when: private_key_file.stat.exists == False + tags: + - wg-generate-keys + +- name: Store hosts private key locally + local_action: + module: template + src: "wg-privatekey.j2" + dest: "{{wireguard_cert_directory}}/{{inventory_hostname}}.private.key" + mode: 0600 + when: private_key_file.stat.exists == False + tags: + - wg-generate-keys + +- name: Store hosts public key locally + local_action: + module: template + src: "wg-publickey.j2" + dest: "{{wireguard_cert_directory}}/{{inventory_hostname}}.public.key" + mode: 0600 + when: private_key_file.stat.exists == False + tags: + - wg-generate-keys + + #- name: Generate WireGuard configuration file + # template: + # src: wireguard.conf.j2 + # dest: /etc/wireguard/wg0.conf + # owner: root + # group: root + # mode: 0600 + # force: no + # + # + #- name: Start and enable WireGuard service + # service: + # name: wg-quick@wg0 + # state: started + # enabled: yes diff --git a/templates/wg-privatekey.j2 b/templates/wg-privatekey.j2 new file mode 100644 index 0000000..84b77fa --- /dev/null +++ b/templates/wg-privatekey.j2 @@ -0,0 +1 @@ +{{hostvars[inventory_hostname]['wg_private_key']}} diff --git a/templates/wg-publickey.j2 b/templates/wg-publickey.j2 new file mode 100644 index 0000000..ca2953a --- /dev/null +++ b/templates/wg-publickey.j2 @@ -0,0 +1 @@ +{{hostvars[inventory_hostname]['wg_public_key']}} diff --git a/tests/inventory b/tests/inventory new file mode 100644 index 0000000..878877b --- /dev/null +++ b/tests/inventory @@ -0,0 +1,2 @@ +localhost + diff --git a/tests/test.yml b/tests/test.yml new file mode 100644 index 0000000..a31e13a --- /dev/null +++ b/tests/test.yml @@ -0,0 +1,5 @@ +--- +- hosts: localhost + remote_user: root + roles: + - . \ No newline at end of file diff --git a/vars/main.yml b/vars/main.yml new file mode 100644 index 0000000..ed97d53 --- /dev/null +++ b/vars/main.yml @@ -0,0 +1 @@ +---