Installs Tailscale from the upstream APT repository with the keyring pinned by sha256, enables tailscaled and, with tailscale_up (default true), runs tailscale up with tailscale_auth_key while the node is not Running. It can also update to the APT candidate version and remove or purge. The role asserts Ubuntu and follows the host's release, and requires a non-empty auth key before any change whenever tailscale up will run. tailscale_userspace_networking now sets --tun=userspace-networking in the FLAGS of /etc/default/tailscaled, where tailscaled reads it, instead of passing it to tailscale up. The molecule scenario installs the real package on noble and resolute without joining a tailnet, and covers the update and purge paths and the refusal cases. Closes #396 Assisted-by: Claude Code:claude-opus-5-5
130 lines
5.1 KiB
YAML
130 lines
5.1 KiB
YAML
---
|
|
# --tun=userspace-networking is a tailscaled flag. The package's systemd unit passes $FLAGS from
|
|
# /etc/default/tailscaled (a conffile that also holds PORT), so the role edits only that one flag
|
|
# in the FLAGS line and keeps everything else.
|
|
- name: Check for the tailscaled defaults file
|
|
ansible.builtin.stat:
|
|
path: /etc/default/tailscaled
|
|
register: __tailscale_defaults_stat
|
|
check_mode: false
|
|
tags: [tailscale, configure]
|
|
|
|
- name: Read the tailscaled defaults file
|
|
become: true
|
|
ansible.builtin.slurp:
|
|
src: /etc/default/tailscaled
|
|
register: __tailscale_defaults
|
|
when: __tailscale_defaults_stat.stat.exists
|
|
tags: [tailscale, configure]
|
|
|
|
- name: Work out the tailscaled flags
|
|
ansible.builtin.set_fact:
|
|
__tailscale_flags: >-
|
|
{{ __tailscale_current_flags | reject('equalto', '--tun=userspace-networking') | list
|
|
+ (['--tun=userspace-networking'] if tailscale_userspace_networking | bool else []) }}
|
|
vars:
|
|
__tailscale_flags_line: >-
|
|
{{ (__tailscale_defaults.content | default('') | b64decode).splitlines()
|
|
| select('match', '^FLAGS=') | list | last | default('FLAGS=', true) }}
|
|
__tailscale_current_flags: >-
|
|
{{ __tailscale_flags_line | regex_replace('^FLAGS=', '') | trim
|
|
| regex_replace('^"(.*)"$', '\1') | regex_replace("^'(.*)'$", '\1') | split }}
|
|
tags: [tailscale, configure]
|
|
|
|
- name: Set the tailscaled flags
|
|
become: true
|
|
ansible.builtin.lineinfile:
|
|
path: /etc/default/tailscaled
|
|
regexp: "^FLAGS="
|
|
line: FLAGS="{{ __tailscale_flags | join(' ') }}"
|
|
notify: Restart tailscaled
|
|
# The package ships the file; under --check on a host without the package it does not exist yet.
|
|
when: not ansible_check_mode or __tailscale_defaults_stat.stat.exists
|
|
tags: [tailscale, configure]
|
|
|
|
# Reads the flags just written when it starts tailscaled, so a daemon that failed to start without
|
|
# a TUN device comes up here.
|
|
- name: Enable and start tailscaled service
|
|
become: true
|
|
ansible.builtin.systemd:
|
|
name: tailscaled
|
|
enabled: true
|
|
state: started
|
|
# Under --check on a host without the package there is no unit to start yet.
|
|
when: >-
|
|
not ansible_check_mode
|
|
or (__tailscale_apt_source | default({}) is not changed and __tailscale_package_install | default({}) is not changed)
|
|
tags: [tailscale, configure]
|
|
|
|
- name: Check current Tailscale backend status
|
|
become: true
|
|
ansible.builtin.command: tailscale status --json
|
|
register: __tailscale_status
|
|
changed_when: false
|
|
# Failure with empty stdout (daemon not up yet) yields NoState below; a logged-out node reports
|
|
# its own non-Running state. Any state other than Running makes the next block run tailscale up;
|
|
# a real fault surfaces there.
|
|
failed_when: false
|
|
when: tailscale_up | bool
|
|
tags: [tailscale, configure]
|
|
|
|
# No Tailscale SSH flag and no ACL tag: the node gets a plain tailnet IP.
|
|
# The auth key is handed over as --auth-key=file:<path> instead of on argv, where it would be
|
|
# readable in ps and /proc/<pid>/cmdline; tailscale up does not read TS_AUTHKEY. The 0600
|
|
# root-owned temporary file is always removed, also on failure.
|
|
# Skipped under --check: tempfile does not support check mode, so the later steps would have no
|
|
# path to work on.
|
|
- name: Bring Tailscale node up
|
|
when:
|
|
- tailscale_up | bool
|
|
- (__tailscale_status.stdout | default('{}', true) | from_json).BackendState | default('NoState') != 'Running'
|
|
- not ansible_check_mode
|
|
tags: [tailscale, configure]
|
|
block:
|
|
# validate.yml asserts this already from the state before any change; the state is read again
|
|
# here, so the key is checked against the state tailscale up actually acts on.
|
|
- name: Require an auth key for tailscale up
|
|
ansible.builtin.assert:
|
|
that:
|
|
- tailscale_auth_key is defined
|
|
- tailscale_auth_key is string
|
|
- tailscale_auth_key | trim | length > 0
|
|
fail_msg: >-
|
|
tailscale_auth_key must be a non-empty string: tailscale_up is true and the node is not Running,
|
|
so the role runs tailscale up. Set the key, normally from a vaulted variable, or set tailscale_up to false.
|
|
quiet: true
|
|
|
|
- name: Create temporary file for the Tailscale auth key
|
|
become: true
|
|
ansible.builtin.tempfile:
|
|
state: file
|
|
prefix: tsauthkey.
|
|
register: __tailscale_authkey_file
|
|
changed_when: false
|
|
|
|
- name: Write Tailscale auth key to the temporary file
|
|
become: true
|
|
no_log: true
|
|
diff: false
|
|
ansible.builtin.copy:
|
|
content: "{{ tailscale_auth_key }}"
|
|
dest: "{{ __tailscale_authkey_file.path }}"
|
|
owner: root
|
|
group: root
|
|
mode: "0600"
|
|
changed_when: false
|
|
|
|
- name: Run tailscale up
|
|
become: true
|
|
no_log: true
|
|
ansible.builtin.command: tailscale up --auth-key=file:{{ __tailscale_authkey_file.path }}
|
|
changed_when: true
|
|
always:
|
|
- name: Remove temporary Tailscale auth key file
|
|
become: true
|
|
ansible.builtin.file:
|
|
path: "{{ __tailscale_authkey_file.path }}"
|
|
state: absent
|
|
when: __tailscale_authkey_file.path is defined
|
|
changed_when: false
|