feat(tailscale): add the tailscale role

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
This commit is contained in:
Defame1297 committed 2026-11-26 19:08:25 +00:00
1 parent 31634c4eef
commit 80c76b16ee
18 files changed
+943 -3

No files matched your search

+129
View File
@@ -0,0 +1,129 @@
---
# --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
+41
View File
@@ -0,0 +1,41 @@
---
- name: Ensure APT keyrings directory exists
become: true
ansible.builtin.file:
path: "{{ tailscale_keyring_path | dirname }}"
state: directory
mode: "0755"
tags: [tailscale, install]
- name: Install Tailscale APT keyring
become: true
ansible.builtin.get_url:
url: "{{ tailscale_keyring_url }}"
dest: "{{ tailscale_keyring_path }}"
checksum: "sha256:{{ tailscale_keyring_checksum }}"
mode: "0644"
tags: [tailscale, install]
# Same content as upstream's <release>.tailscale-keyring.list.
- name: Install Tailscale APT source list
become: true
ansible.builtin.copy:
dest: "{{ tailscale_apt_list_path }}"
content: |
# Tailscale packages for {{ tailscale_apt_distribution }} {{ tailscale_apt_release }}
deb [signed-by={{ tailscale_keyring_path }}] {{ tailscale_repo_url }} {{ tailscale_apt_release }} main
mode: "0644"
register: __tailscale_apt_source
tags: [tailscale, install]
- name: Install Tailscale package
become: true
ansible.builtin.apt:
name: "{{ tailscale_package }}"
state: present
update_cache: true
register: __tailscale_package_install
notify: Restart tailscaled
# Under --check a new source line is only simulated, so apt would find no tailscale package.
when: not ansible_check_mode or __tailscale_apt_source is not changed
tags: [tailscale, install]
+30
View File
@@ -0,0 +1,30 @@
---
# Validation first, under always, so a bad input fails before any change whatever tags the run
# selects.
- name: Validate the role inputs
ansible.builtin.import_tasks: validate.yml
tags: always
- name: Remove Tailscale (explicit only)
ansible.builtin.include_tasks: remove.yml
when: tailscale_remove | bool
tags: [tailscale, remove]
- name: Install Tailscale
ansible.builtin.include_tasks: install.yml
tags: [tailscale, install]
- name: Configure Tailscale
ansible.builtin.include_tasks: configure.yml
tags: [tailscale, configure]
- name: Ensure Tailscale is up-to-date
ansible.builtin.include_tasks: update.yml
when:
- tailscale_update | bool
# Under --check a new source line is only simulated, so apt would have no candidate.
- not ansible_check_mode or __tailscale_apt_source | default({}) is not changed
tags: [tailscale, update]
- name: Flush handlers
ansible.builtin.meta: flush_handlers
+49
View File
@@ -0,0 +1,49 @@
---
# Run with tailscale_remove=true (and tailscale_remove_data=true to purge), tag: remove. main.yml
# installs and configures Tailscale again afterwards.
- name: Log Tailscale out of the tailnet
become: true
ansible.builtin.command: tailscale logout
changed_when: true
# Best effort: already logged out, tailscaled down or binary missing must not block the removal. A
# failed logout leaves a stale device in the tailnet admin console.
failed_when: false
tags: [tailscale, remove]
- name: Stop and disable tailscaled service
become: true
ansible.builtin.systemd:
name: tailscaled
state: stopped
enabled: false
tags: [tailscale, remove]
- name: Remove Tailscale package
become: true
ansible.builtin.apt:
name: "{{ tailscale_package }}"
state: absent
purge: "{{ tailscale_remove_data | bool }}"
tags: [tailscale, remove]
- name: Remove Tailscale APT source list
become: true
ansible.builtin.file:
path: "{{ tailscale_apt_list_path }}"
state: absent
tags: [tailscale, remove]
- name: Remove Tailscale APT keyring
become: true
ansible.builtin.file:
path: "{{ tailscale_keyring_path }}"
state: absent
tags: [tailscale, remove]
- name: Remove Tailscale state directory
become: true
ansible.builtin.file:
path: "{{ tailscale_state_dir }}"
state: absent
when: tailscale_remove_data | bool
tags: [tailscale, remove]
+39
View File
@@ -0,0 +1,39 @@
---
# Run with tailscale_update=true (tag: update).
- name: Refresh APT cache before checking for Tailscale updates
become: true
ansible.builtin.apt:
update_cache: true
tags: [tailscale, update]
- name: Determine latest available Tailscale candidate version
ansible.builtin.command: apt-cache policy {{ tailscale_package | quote }}
register: __tailscale_apt_policy
changed_when: false
# Read-only, so it also runs in the --check preview, which needs its stdout.
check_mode: false
tags: [tailscale, update]
- name: Extract latest Tailscale candidate version
ansible.builtin.set_fact:
__tailscale_candidate_version: >-
{{ __tailscale_apt_policy.stdout | regex_search('Candidate:\s+(\S+)', '\1') | default([''], true) | first }}
tags: [tailscale, update]
- name: Fail if Tailscale has no installable candidate
ansible.builtin.fail:
msg: >-
No installable candidate version found for {{ tailscale_package }} (apt-cache policy reported
'{{ __tailscale_candidate_version }}'). Check that the Tailscale APT repository is reachable.
when: __tailscale_candidate_version in ['', '(none)']
tags: [tailscale, update]
# Pinned to the resolved candidate version (never state=latest), so the task stays idempotent and
# passes ansible-lint's package-latest rule.
- name: Update Tailscale package to latest candidate version
become: true
ansible.builtin.apt:
name: "{{ tailscale_package }}={{ __tailscale_candidate_version }}"
state: present
notify: Restart tailscaled
tags: [tailscale, update]
+43
View File
@@ -0,0 +1,43 @@
---
# Input checks. Imported first in main.yml with tags: always, so a bad input fails before any
# change. The argument spec (types) runs before this file.
# The repository suite and the source list follow the host's release, read from gathered facts.
- name: Require Ubuntu
ansible.builtin.assert:
that:
- ansible_facts['distribution'] | default('') == 'Ubuntu'
fail_msg: >-
tailscale supports Ubuntu only (found: {{ ansible_facts['distribution'] | default('no gathered facts') }}).
The role reads gathered facts, so run it in a play that gathers them.
quiet: true
# Read-only, so it also runs under --check. A missing binary or a daemon that is not up counts as
# not Running.
- name: Read the Tailscale backend state before any change
become: true
ansible.builtin.command: tailscale status --json
register: __tailscale_status_before
changed_when: false
failed_when: false
check_mode: false
when: tailscale_up | bool
# tailscale up runs when the node is not Running, and a removal logs it out first, so then the
# key is needed.
- name: Require an auth key when tailscale up will run
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
(or tailscale_remove is true), so the role runs tailscale up. Set the key, normally from a
vaulted variable, or set tailscale_up to false.
quiet: true
when:
- tailscale_up | bool
- >-
tailscale_remove | bool
or (__tailscale_status_before.stdout | default('{}', true) | from_json).BackendState | default('NoState') != 'Running'