2018-09-18 04:00:12 +02:00
|
|
|
---
|
2019-04-07 16:26:03 +02:00
|
|
|
- name: Install tinc
|
|
|
|
|
apt:
|
|
|
|
|
name: tinc
|
|
|
|
|
state: latest
|
|
|
|
|
|
2018-09-26 19:24:36 +02:00
|
|
|
- name: Ensure tinc netname directory exists
|
2018-09-18 04:00:12 +02:00
|
|
|
file:
|
|
|
|
|
path: /etc/tinc/{{ netname }}/hosts
|
|
|
|
|
recurse: True
|
|
|
|
|
state: directory
|
|
|
|
|
|
2018-09-26 19:24:36 +02:00
|
|
|
- name: Create /etc/tinc/nets.boot file from template
|
2018-09-25 17:07:38 +02:00
|
|
|
template:
|
|
|
|
|
src: nets.boot.j2
|
|
|
|
|
dest: /etc/tinc/nets.boot
|
|
|
|
|
notify:
|
|
|
|
|
- restart tinc
|
|
|
|
|
|
2018-09-26 19:24:36 +02:00
|
|
|
- name: Ensure tinc.conf contains connection to all other nodes
|
2018-09-18 04:00:12 +02:00
|
|
|
template:
|
|
|
|
|
src: tinc.conf.j2
|
|
|
|
|
dest: /etc/tinc/{{ netname }}/tinc.conf
|
|
|
|
|
notify:
|
|
|
|
|
- restart tinc
|
|
|
|
|
- reload tinc
|
|
|
|
|
|
2018-09-26 19:24:36 +02:00
|
|
|
- name: Create tinc-up file
|
2018-09-18 04:00:12 +02:00
|
|
|
template:
|
|
|
|
|
src: tinc-up.j2
|
|
|
|
|
dest: /etc/tinc/{{ netname }}/tinc-up
|
|
|
|
|
mode: 0755
|
|
|
|
|
notify:
|
|
|
|
|
- restart tinc
|
|
|
|
|
|
2018-09-26 19:24:36 +02:00
|
|
|
- name: Create tinc-down file
|
2018-09-18 04:00:12 +02:00
|
|
|
template:
|
|
|
|
|
src: tinc-down.j2
|
|
|
|
|
dest: /etc/tinc/{{ netname }}/tinc-down
|
|
|
|
|
mode: 0755
|
|
|
|
|
notify:
|
|
|
|
|
- restart tinc
|
|
|
|
|
|
2018-09-26 19:24:36 +02:00
|
|
|
- name: Ensure tinc hosts file binds to scaleway dns address
|
2018-09-18 04:00:12 +02:00
|
|
|
block:
|
2018-09-26 19:24:36 +02:00
|
|
|
- name: Gather Scaleway instance ID
|
|
|
|
|
shell: "/usr/local/bin/scw-metadata ID"
|
2018-09-18 04:00:12 +02:00
|
|
|
register: scw_id
|
2018-09-26 19:24:36 +02:00
|
|
|
changed_when: no
|
2018-09-18 04:00:12 +02:00
|
|
|
|
|
|
|
|
- lineinfile:
|
|
|
|
|
dest: /etc/tinc/{{ netname }}/hosts/{{ inventory_hostname }}
|
|
|
|
|
line: "Address = {{ scw_id.stdout }}.{{ scw_private_domain }}"
|
|
|
|
|
create: yes
|
|
|
|
|
notify:
|
|
|
|
|
- restart tinc
|
|
|
|
|
when: tinc_ignore_scaleway_dns | default(False) | bool == False
|
|
|
|
|
|
2018-09-26 19:24:36 +02:00
|
|
|
- name: Ensure tinc hosts file binds to physical ip address
|
2018-09-18 04:00:12 +02:00
|
|
|
lineinfile:
|
|
|
|
|
dest: /etc/tinc/{{ netname }}/hosts/{{ inventory_hostname }}
|
|
|
|
|
line: |-
|
|
|
|
|
Address = {{ ansible_eth0.ipv4.address }}
|
|
|
|
|
create: yes
|
|
|
|
|
notify:
|
|
|
|
|
- restart tinc
|
|
|
|
|
when: tinc_ignore_scaleway_dns | default(False) | bool == True
|
|
|
|
|
|
2018-09-26 19:24:36 +02:00
|
|
|
- name: Ensure subnet ip address is properly set in tinc host file
|
2018-09-18 04:00:12 +02:00
|
|
|
lineinfile:
|
|
|
|
|
dest: /etc/tinc/{{ netname }}/hosts/{{ inventory_hostname }}
|
|
|
|
|
line: "Subnet = {{ vpn_ip }}/{{ vpn_subnet_cidr_netmask }}"
|
|
|
|
|
create: yes
|
|
|
|
|
notify:
|
|
|
|
|
- restart tinc
|
|
|
|
|
|
2018-09-26 19:24:36 +02:00
|
|
|
- name: Check whether /etc/tinc/netname/hosts/inventory_hostname contains "-----END RSA PUBLIC KEY-----"
|
2018-09-18 04:00:12 +02:00
|
|
|
command: awk '/^-----END RSA PUBLIC KEY-----$/' /etc/tinc/{{ netname }}/hosts/{{ inventory_hostname }}
|
|
|
|
|
changed_when: "public_key.stdout != '-----END RSA PUBLIC KEY-----'"
|
|
|
|
|
register: public_key
|
|
|
|
|
|
|
|
|
|
# this is necessary because the public key will not be generated (non-interactively) if the private key already exists
|
2018-09-26 19:24:36 +02:00
|
|
|
- name: Delete private key and regenerate keypair if public key is absent from tinc hosts file
|
2018-09-18 04:00:12 +02:00
|
|
|
file:
|
|
|
|
|
path: /etc/tinc/{{ netname }}/rsa_key.priv
|
|
|
|
|
state: absent
|
|
|
|
|
when: public_key.changed
|
|
|
|
|
|
2018-09-26 19:24:36 +02:00
|
|
|
- name: Create tinc private key (and append public key to tincd hosts file)
|
2018-09-18 04:00:12 +02:00
|
|
|
shell: tincd -n {{ netname }} -K4096
|
|
|
|
|
args:
|
|
|
|
|
creates: /etc/tinc/{{ netname }}/rsa_key.priv
|
|
|
|
|
notify:
|
|
|
|
|
- restart tinc
|
|
|
|
|
|
2018-09-26 19:24:36 +02:00
|
|
|
- name: Fetch tinc hosts file after key creation
|
2018-09-18 04:00:12 +02:00
|
|
|
fetch:
|
|
|
|
|
src: /etc/tinc/{{ netname }}/hosts/{{ inventory_hostname }}
|
|
|
|
|
dest: fetch/{{ inventory_hostname }}
|
|
|
|
|
flat: yes
|
|
|
|
|
notify:
|
|
|
|
|
- reload tinc
|
|
|
|
|
|
2018-09-26 19:24:36 +02:00
|
|
|
- name: Sync the fetched tinc hosts files on each host
|
2018-09-18 04:00:12 +02:00
|
|
|
synchronize:
|
|
|
|
|
src: fetch/
|
|
|
|
|
dest: /etc/tinc/{{ netname }}/hosts/
|
|
|
|
|
use_ssh_args: yes
|
|
|
|
|
notify:
|
|
|
|
|
- reload tinc
|
|
|
|
|
|
|
|
|
|
- meta: flush_handlers
|
|
|
|
|
|
2018-09-26 19:24:36 +02:00
|
|
|
- name: Start tinc on boot
|
2018-09-18 04:00:12 +02:00
|
|
|
systemd:
|
2018-10-18 22:45:01 +02:00
|
|
|
name: tinc@{{ netname }}
|
2018-09-18 04:00:12 +02:00
|
|
|
enabled: yes
|
|
|
|
|
state: started
|
|
|
|
|
|
2018-09-26 19:24:36 +02:00
|
|
|
- name: Ensure tun0 exists
|
2018-09-18 04:00:12 +02:00
|
|
|
shell: "ip a s"
|
|
|
|
|
register: result
|
|
|
|
|
until: result.stdout.find("tun0") != -1
|
|
|
|
|
retries: 200
|
|
|
|
|
delay: 10
|
|
|
|
|
changed_when: False
|
|
|
|
|
|
2018-09-26 19:24:36 +02:00
|
|
|
- name: Add nodes to /etc/hosts (ansible_inventory resolves to vpn_ip)
|
2018-09-18 04:00:12 +02:00
|
|
|
lineinfile: dest=/etc/hosts regexp='.*{{ item }}$' line="{{ hostvars[item].vpn_ip }} {{item}}" state=present
|
|
|
|
|
when: hostvars[item].vpn_ip is defined
|
|
|
|
|
with_items: "{{ play_hosts }}"
|