serverctl/infrastructure/create-resources/ansible/roles/wireguard/mesh/tasks/main.yml

90 lines
2.4 KiB
YAML
Raw Permalink Normal View History

2022-02-24 13:57:19 +01:00
---
2022-02-24 21:36:07 +01:00
- name: install wireguard
2022-02-24 13:57:19 +01:00
apt:
2022-02-24 21:36:07 +01:00
name: wireguard
2022-02-24 13:57:19 +01:00
state: present
become: yes
2022-02-24 21:36:07 +01:00
when: ansible_distribution == 'Debian' or ansible_distribution == "Ubuntu"
2022-02-24 13:57:19 +01:00
- name: install wireguard
2022-02-24 21:36:07 +01:00
pacman:
name: wireguard-tools
2022-02-24 13:57:19 +01:00
state: present
become: yes
2022-02-24 21:36:07 +01:00
when: ansible_distribution == "Archlinux"
2022-02-24 13:57:19 +01:00
- name: generate wireguard keypair
2022-02-24 21:36:07 +01:00
shell: wg genkey | tee /etc/wireguard/serverctl-privatekey | wg pubkey | tee /etc/wireguard/serverctl-publickey
2022-02-24 13:57:19 +01:00
args:
2022-02-24 21:36:07 +01:00
creates: /etc/wireguard/serverctl-privatekey
2022-02-24 13:57:19 +01:00
become: yes
- name: register private key
2022-02-24 21:36:07 +01:00
shell: cat /etc/wireguard/serverctl-privatekey
2022-02-24 13:57:19 +01:00
register: wireguard_private_key
changed_when: false
become: yes
- name: register public key
2022-02-24 21:36:07 +01:00
shell: cat /etc/wireguard/serverctl-publickey
2022-02-24 13:57:19 +01:00
register: wireguard_public_key
changed_when: false
become: yes
- name: generate preshared keypair
2022-02-24 21:36:07 +01:00
shell: "wg genpsk > /etc/wireguard/serverctl-psk-{{item}}"
2022-02-24 13:57:19 +01:00
args:
2022-02-24 21:36:07 +01:00
creates: "/etc/wireguard/serverctl-psk-{{item}}"
2022-02-24 13:57:19 +01:00
when: inventory_hostname < item
2022-02-24 21:36:07 +01:00
with_items: "{{groups['serverctl_super_cluster']}}"
2022-02-24 13:57:19 +01:00
become: yes
- name: register preshared key
2022-02-24 21:36:07 +01:00
shell: "cat /etc/wireguard/serverctl-psk-{{item}}"
2022-02-24 13:57:19 +01:00
register: wireguard_preshared_key
changed_when: false
when: inventory_hostname < item
2022-02-24 21:36:07 +01:00
with_items: "{{groups['serverctl_super_cluster']}}"
2022-02-24 13:57:19 +01:00
become: yes
- name: message preshared keys
set_fact: "wireguard_preshared_keys={{wireguard_preshared_keys|default({}) | combine({item.item: item.stdout})}}"
when: item.skipped is not defined
with_items: "{{wireguard_preshared_key.results}}"
become: yes
2022-02-24 21:36:07 +01:00
#- name: print hostvars
# ansible.builtin.debug:
# msg: "{{hostvars[item]}}"
# with_items: "{{groups['serverctl_super_cluster']}}"
2022-02-24 14:03:42 +01:00
- name: Setup wg0 device
2022-02-24 13:57:19 +01:00
template:
src: 'systemd.netdev'
2022-02-24 21:36:07 +01:00
dest: '{{systemd_network_dir}}/99-serverctl-wg0.netdev'
2022-02-24 13:57:19 +01:00
owner: root
group: systemd-network
mode: 0640
become: yes
notify: systemd network restart
2022-02-24 14:03:42 +01:00
- name: Setup wg0 network
2022-02-24 13:57:19 +01:00
template:
src: 'systemd.network'
2022-02-24 21:36:07 +01:00
dest: "{{systemd_network_dir}}/99-serverctl-wg0.network"
2022-02-24 13:57:19 +01:00
owner: root
group: systemd-network
mode: 0640
become: yes
notify: systemd network restart
#- name: Start and enalbe wireguard on book
# systemd:
# name: wg-quick@wgserverctl0
# enabled: yes
# state: started
#- debug: msg="{{item.1}} - {{ (wireguard_base_ipv4|ipaddr(item.0 + 1)) }}"
# with_indexed_items: "{{groups.serverctl_mesh_nodes}}"