- hosts: localhost vars: meta: application_name: Headscale playbook_version: 0.2 app_domain: "{{ uberspace_default_web_domain }}" app_port: 8080 _home: "/home/{{ uberspace_asteroid_name }}" _root: "{{ _home }}/.headscale" tasks: # Required by `uberspace app install`; skipped during installation. - name: _Return resolved playbook vars debug: var: vars tags: [playbook-vars] - name: Validate installation options assert: that: - app_domain is match('^[a-zA-Z0-9][a-zA-Z0-9.-]*[a-zA-Z0-9]$') - app_port | string is match('^[0-9]+$') - app_port | int >= 1024 - app_port | int <= 65535 fail_msg: "Use a domain and a port from 1024 to 65535." - name: Inspect installation and service paths stat: path: "{{ item }}" loop: - "{{ _root }}" - "{{ _home }}/.config/systemd/user/headscale.service" register: existing - name: Refuse to overwrite an existing installation or service assert: that: not item.stat.exists fail_msg: "{{ item.item }} already exists. This playbook only supports fresh installations." loop: "{{ existing.results }}" loop_control: label: "{{ item.item }}" - name: Inspect the document root find: paths: "/var/www/virtual/{{ uberspace_asteroid_name }}/html" hidden: true file_type: any excludes: [nocontent.html] register: contents - name: Require an empty document root assert: that: contents.matched == 0 fail_msg: "Use a fresh Asteroid with an empty document root." - name: Inspect existing web routes command: argv: [uberspace, web, backend, list, --json] register: routes changed_when: false check_mode: false - name: Refuse to replace another application's route assert: that: item.path != '/' or item.destination == 'APACHE' fail_msg: "{{ app_domain }}/ already serves another backend." loop: "{{ (routes.stdout | from_json) | selectattr('domain', 'equalto', app_domain) | list }}" loop_control: label: "{{ item.path }}" - name: Require an unused HTTP port wait_for: host: 127.0.0.1 port: "{{ app_port }}" state: stopped timeout: 1 when: not ansible_check_mode - name: List registered web domains command: argv: [uberspace, web, domain, list, --json] register: domains changed_when: false check_mode: false - name: Require the supported binary architecture assert: that: ansible_facts['architecture'] == 'x86_64' - name: Install Headscale when: not ansible_check_mode block: - name: Find the latest stable release uri: url: https://api.github.com/repos/juanfont/headscale/releases/latest return_content: true register: release - name: Validate the release assert: that: - not release.json.draft - not release.json.prerelease - release.json.tag_name is match('^v[0-9]+\.[0-9]+\.[0-9]+$') - name: Select the release binary set_fact: _binary: "headscale_{{ release.json.tag_name | regex_replace('^v', '') }}_linux_amd64" - name: Create private application directories file: path: "{{ item }}" state: directory mode: '0700' loop: - "{{ _root }}" - "{{ _home }}/.config/systemd/user" - name: Download the binary and checksums get_url: url: "https://github.com/juanfont/headscale/releases/download/{{ release.json.tag_name }}/{{ item }}" dest: "{{ _root }}/{{ item }}" mode: '0600' loop: ["{{ _binary }}", checksums.txt] - name: Verify the binary checksum command: argv: [sha256sum, --check, --ignore-missing, checksums.txt] chdir: "{{ _root }}" changed_when: false - name: Make the verified binary executable file: path: "{{ _root }}/{{ _binary }}" mode: '0700' - name: Link the release binary file: src: "{{ _binary }}" dest: "{{ _root }}/headscale" state: link - name: Fetch this release's example configuration uri: url: "https://raw.githubusercontent.com/juanfont/headscale/{{ release.json.tag_name }}/config-example.yaml" return_content: true register: example - name: Adapt the release configuration vars: overrides: server_url: "https://{{ app_domain }}" listen_addr: "0.0.0.0:{{ app_port }}" metrics_listen_addr: "" grpc_listen_addr: "127.0.0.1:0" tls_cert_path: "" tls_key_path: "" unix_socket: "{{ _root }}/headscale.sock" unix_socket_permission: '0600' noise: private_key_path: "{{ _root }}/noise_private.key" database: sqlite: path: "{{ _root }}/db.sqlite" dns: base_domain: "tailnet.{{ app_domain }}" override_local_dns: false disable_check_updates: true copy: dest: "{{ _root }}/config.yaml" mode: '0600' content: "{{ (example.content | from_yaml) | combine(overrides, recursive=true) | to_nice_yaml }}" - name: Validate the configuration command: argv: ["{{ _root }}/headscale", --config, "{{ _root }}/config.yaml", configtest] changed_when: false - name: Create the Headscale service copy: dest: "{{ _home }}/.config/systemd/user/headscale.service" mode: '0644' content: | [Unit] Description=Headscale [Service] WorkingDirectory=%h/.headscale ExecStart=%h/.headscale/headscale --config %h/.headscale/config.yaml serve Restart=on-failure RestartSec=5 UMask=0077 [Install] WantedBy=default.target - name: Enable and start Headscale systemd: name: headscale scope: user daemon_reload: true enabled: true state: started - name: Check the local service uri: url: "http://127.0.0.1:{{ app_port }}/health" register: health until: health.status == 200 retries: 24 delay: 5 - name: Register the domain command: argv: [uberspace, web, domain, add, "{{ app_domain }}", --wait] when: app_domain not in (domains.stdout | from_json | map(attribute='name') | list) - name: Route the domain to Headscale command: argv: [uberspace, web, backend, add, "{{ app_domain }}/", port, "{{ app_port | string }}", --force, --wait] - name: Inspect existing web headers command: argv: [uberspace, web, header, list, --json] register: headers changed_when: false check_mode: false - name: Enable HSTS command: argv: [uberspace, web, header, add, "{{ app_domain }}/", Strict-Transport-Security, max-age=15552000] when: - not ansible_check_mode - headers.stdout | default('[]') | from_json | selectattr('domain', 'equalto', app_domain) | selectattr('name', 'equalto', 'Strict-Transport-Security') | list | length == 0 - name: Check public availability uri: url: "https://{{ app_domain }}/health" register: site until: site.status == 200 retries: 12 delay: 5 - name: _Report debug: msg: | Headscale: https://{{ app_domain }} Version: {{ release.json.tag_name }} Manage users and nodes with ~/.headscale/headscale over SSH. The embedded DERP server is disabled; clients use external relays when necessary. Guide: https://u8lab.uberspace.de/guide_headscale/