fix(tailscale): harden removal, restarts and the auth key check
Build / build (pull_request) Successful in 29s
Lint / lint (pull_request) Successful in 2m25s
Molecule / molecule (pull_request) Failing after 3m23s

- Removal passes on a host without a tailscaled unit, and the
  best-effort logout reports a change only when it succeeded.
- Under --check the service tasks run only when the unit exists, also
  with --tags configure on a host without the package.
- The auth key is required before any change only when the run includes
  the configure tasks; --tags remove, install and update need none. The
  assert in the up block stays as the hard guard.
- When the flags change or tailscaled is not active, the role clears its
  failure state and restarts it at once, so a daemon the package started
  without a TUN device does not stay in the start limit.
- The health tag is back on the configure include.
- The scenario tests removal without purge (state and defaults kept, a
  second removal passes) and a purge with a state directory the package
  does not delete itself, and waits for tailscaled to be active.

Assisted-by: Claude Code:claude-opus-5-5
This commit is contained in:
Defame1297 committed 2026-11-26 20:47:49 +00:00
1 parent 80c76b16ee
commit 29165a610f
6 files changed
+190 -29

No files matched your search

+8 -5
View File
@@ -12,7 +12,7 @@ The role targets Ubuntu hosts, not Proxmox nodes, so it does not cover Debian tr
- Use it by its fully qualified name, `mortis.kamino.tailscale`, in a play that gathers facts: the Ubuntu assert and the default of `tailscale_apt_release` read them.
- Role dependencies: none.
- `tailscale_auth_key` has no default. With `tailscale_up: true` it must be a non-empty string whenever the node is not Running (a new node, a logged-out one, or any run with `tailscale_remove`); this is asserted before any change. Source it from a vaulted variable.
- `tailscale_auth_key` has no default. With `tailscale_up: true` it must be a non-empty string whenever the node is not Running (a new node, a logged-out one, or any run with `tailscale_remove`) and the run includes the configure tasks; this is asserted before any change. See Tags for which runs need it. Source it from a vaulted variable.
- A container needs `/dev/net/tun` passed through from its host, or `tailscale_userspace_networking: true`.
- The role owns no firewall rule: Tailscale makes outbound connections (UDP 41641 by default) and needs no inbound one.
- `become` is used for every task that changes the host.
@@ -39,7 +39,9 @@ The role targets Ubuntu hosts, not Proxmox nodes, so it does not cover Debian tr
## Tags
The input validation (`tasks/validate.yml`) carries `always`, so it runs whatever tags are selected. `tailscale` selects every other task; `install`, `configure`, `update` and `remove` select their task files.
The input validation (`tasks/validate.yml`) carries `always`, so it runs whatever tags are selected. `tailscale` selects every other task; `install`, `configure`, `update` and `remove` select their task files. `health` is only on the include of the configure tasks, whose own tasks carry `tailscale` and `configure`, so `--tags health` runs none of them.
The auth key is needed only by runs that include the configure tasks: no tag selection, `--tags tailscale` or `--tags configure` (and `tagged`), unless `--skip-tags` names `tailscale`, `configure` or `health`. `--tags remove`, `--tags install` and `--tags update` run without it.
## Example play
@@ -56,7 +58,7 @@ Removal and update are explicit: `-e tailscale_remove=true` (add `tailscale_remo
## Tests
The `default` molecule scenario runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). It installs the real package from pkgs.tailscale.com with `tailscale_up: false` and `tailscale_userspace_networking: true`. `tailscale up` (joining a tailnet) is not exercised: the scenario has no auth key and no tailnet. Verify checks the keyring checksum against the published hash, the APT source line, the package installed from that repository, `tailscaled` enabled and active, the `FLAGS` line in `/etc/default/tailscaled` with the package's `PORT` line kept, the running daemon's command line carrying `--tun=userspace-networking`, and a backend state that is not Running. It then downgrades the package to an older version from the repository and runs the role with `tailscale_update` (the candidate is installed again), and runs it with `tailscale_remove` and `tailscale_remove_data` (a marker in the state directory and a line added to the defaults file are gone, and the role installed and configured Tailscale again). It runs `check` before the converge (a fresh host) and after it (a configured host). Negative tests cover a host without Ubuntu facts, a `tailscale_up` that is not a boolean, and `tailscale_up: true` without an auth key or with an empty one. Switching `tailscale_userspace_networking` back to false is not run: `tailscaled` cannot start without `/dev/net/tun` in the container.
The `default` molecule scenario runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). It installs the real package from pkgs.tailscale.com with `tailscale_up: false` and `tailscale_userspace_networking: true`. `tailscale up` (joining a tailnet) is not exercised: the scenario has no auth key and no tailnet. Verify checks the keyring checksum against the published hash, the APT source line, the package installed from that repository, `tailscaled` enabled and active, the `FLAGS` line in `/etc/default/tailscaled` with the package's `PORT` line kept, the running daemon's command line carrying `--tun=userspace-networking`, and a backend state that is not Running. It then downgrades the package to an older version from the repository and runs the role with `tailscale_update` (the candidate is installed again), Removal is run twice. First the remove tasks alone, as `--tags remove` runs them, without `tailscale_remove_data`: the package and the source list go, while a marker in the state directory and a line added to the defaults file stay. A second removal, with no `tailscaled` unit left, passes, and the role then installs Tailscale again with the state kept. Second, the role with `tailscale_remove` and `tailscale_remove_data` and a `tailscale_state_dir` other than `/var/lib/tailscale`, which the package's own purge deletes: that directory is gone, the defaults file is the package's fresh copy, and the role installed and configured Tailscale again. Verify waits for `tailscaled` to be active after each restart. It runs `check` before the converge (a fresh host) and after it (a configured host). Negative tests cover a host without Ubuntu facts, a `tailscale_up` that is not a boolean, and `tailscale_up: true` without an auth key or with an empty one. Switching `tailscale_userspace_networking` back to false is not run: `tailscaled` cannot start without `/dev/net/tun` in the container.
## Notes
@@ -65,6 +67,7 @@ The `default` molecule scenario runs in CI only, on two platforms: `${MOLECULE_I
- The auth key is passed as `--auth-key=file:<path>` from a root-owned 0600 temporary file, so it never appears in argv. The file is always removed, and the write and `tailscale up` tasks use `no_log`.
- `tailscale up` runs only when `tailscale status --json` reports a state other than `Running` (an unreadable status counts as not running).
- No Tailscale SSH flag and no ACL tag are set: the node gets a plain tailnet IP.
- `tailscale_userspace_networking` edits only the `--tun=userspace-networking` token of the `FLAGS` line; other flags and the rest of `/etc/default/tailscaled` (a conffile of the package, which also holds `PORT`) are kept. A change restarts `tailscaled`. The flag belongs to `tailscaled`, not to `tailscale up`.
- The `Restart tailscaled` handler runs after a package install or update and after a change of the flags.
- `tailscale_userspace_networking` edits only the `--tun=userspace-networking` token of the `FLAGS` line; other flags and the rest of `/etc/default/tailscaled` (a conffile of the package, which also holds `PORT`) are kept. A change, or a `tailscaled` that is not active, restarts it at once after clearing its failure state (`systemctl reset-failed`): the package starts it with the flags it ships, which fails without a TUN device and can run into systemd's start limit. The flag belongs to `tailscaled`, not to `tailscale up`.
- The `Restart tailscaled` handler runs after a package install or update.
- Removal tolerates a host with no `tailscaled` unit, so a repeated `--tags remove` passes.
- `tailscale logout` during removal is best effort. A removal without `tailscale_remove_data` keeps the state directory and the defaults file.
+117 -12
View File
@@ -95,11 +95,15 @@
- "'https://pkgs.tailscale.com/stable/ubuntu ' ~ ansible_facts['distribution_release'] in tailscale_test_policy.stdout"
quiet: true
- name: Ask systemd about tailscaled
# tailscaled may still be starting after a restart.
- name: Wait for tailscaled
ansible.builtin.systemd:
name: tailscaled
register: tailscale_test_unit
check_mode: true
until: tailscale_test_unit.status.ActiveState == 'active'
retries: 10
delay: 3
- name: Tailscaled is enabled and running
ansible.builtin.assert:
@@ -206,11 +210,15 @@
register: tailscale_test_updated
changed_when: false
- name: Ask systemd about tailscaled after the update
# tailscaled may still be starting after a restart.
- name: Wait for tailscaled after the update
ansible.builtin.systemd:
name: tailscaled
register: tailscale_test_unit
check_mode: true
until: tailscale_test_unit.status.ActiveState == 'active'
retries: 10
delay: 3
- name: The candidate is installed and tailscaled runs
ansible.builtin.assert:
@@ -219,10 +227,10 @@
- tailscale_test_unit.status.ActiveState == 'active'
quiet: true
# tailscale_remove with tailscale_remove_data purges the package and the state directory, then the
# role installs it again. A marker in the state directory and a line added to the defaults file (a
# conffile, so only a purge replaces it) show that both went.
- name: Verify removal and purge
# A removal without tailscale_remove_data, through the role's remove tasks alone (as --tags remove
# runs them): the package goes, while the state directory and the defaults file (a conffile) stay.
# A second removal finds no tailscaled unit and must still pass. The role then installs it again.
- name: Verify removal without purge
hosts: all
gather_facts: true
tasks:
@@ -237,17 +245,112 @@
path: /etc/default/tailscaled
line: "# molecule-marker"
- name: Run the remove tasks
ansible.builtin.include_role:
name: mortis.kamino.tailscale
tasks_from: remove
vars:
tailscale_remove: true
- name: Ask dpkg for the package state
ansible.builtin.command:
argv: [dpkg-query, --show, --showformat, "${db:Status-Status}", tailscale]
register: tailscale_test_dpkg
changed_when: false
failed_when: false
- name: Stat the state marker and the source list
ansible.builtin.stat:
path: "{{ item }}"
loop:
- /var/lib/tailscale/molecule-marker
- /etc/apt/sources.list.d/tailscale.list
register: tailscale_test_kept
- name: Read the defaults file
ansible.builtin.slurp:
src: /etc/default/tailscaled
register: tailscale_test_defaults
- name: The package and the source list are gone, the state and the defaults file are kept
ansible.builtin.assert:
that:
- tailscale_test_dpkg.stdout != 'installed'
- tailscale_test_kept.results | map(attribute='stat.exists') | list == [true, false]
- "'# molecule-marker' in (tailscale_test_defaults.content | b64decode).splitlines()"
quiet: true
- name: Run the remove tasks again, with no tailscaled unit left
ansible.builtin.include_role:
name: mortis.kamino.tailscale
tasks_from: remove
vars:
tailscale_remove: true
- name: Install again
ansible.builtin.include_role:
name: mortis.kamino.tailscale
- name: Wait for tailscaled
ansible.builtin.systemd:
name: tailscaled
register: tailscale_test_unit
check_mode: true
until: tailscale_test_unit.status.ActiveState == 'active'
retries: 10
delay: 3
- name: Stat the state marker again
ansible.builtin.stat:
path: /var/lib/tailscale/molecule-marker
register: tailscale_test_marker
- name: The reinstall kept the state
ansible.builtin.assert:
that:
- tailscale_test_marker.stat.exists
- tailscale_test_unit.status.UnitFileState == 'enabled'
quiet: true
# tailscale_remove with tailscale_remove_data purges the package and deletes tailscale_state_dir,
# then the role installs it again. The package's own purge deletes /var/lib/tailscale, so the state
# directory here is another one: only the role removes it. The marker line in the defaults file (a
# conffile, so only a purge replaces it) shows the purge.
- name: Verify removal with purge
hosts: all
gather_facts: true
vars:
tailscale_test_state_dir: /srv/tailscale-molecule-state
tasks:
- name: Create the state directory
ansible.builtin.file:
path: "{{ tailscale_test_state_dir }}"
state: directory
mode: "0700"
- name: Put a marker in the state directory
ansible.builtin.copy:
content: marker
dest: "{{ tailscale_test_state_dir }}/molecule-marker"
mode: "0600"
- name: Add a marker line to the defaults file
ansible.builtin.lineinfile:
path: /etc/default/tailscaled
line: "# molecule-marker"
- name: Run the role with removal and data removal
ansible.builtin.include_role:
name: mortis.kamino.tailscale
vars:
tailscale_remove: true
tailscale_remove_data: true
tailscale_state_dir: "{{ tailscale_test_state_dir }}"
- name: Stat the state marker
- name: Stat the state directory
ansible.builtin.stat:
path: /var/lib/tailscale/molecule-marker
register: tailscale_test_marker
path: "{{ tailscale_test_state_dir }}"
register: tailscale_test_state
- name: Read the defaults file
ansible.builtin.slurp:
@@ -260,21 +363,23 @@
register: tailscale_test_dpkg
changed_when: false
- name: Ask systemd about tailscaled
- name: Wait for tailscaled
ansible.builtin.systemd:
name: tailscaled
register: tailscale_test_unit
check_mode: true
until: tailscale_test_unit.status.ActiveState == 'active'
retries: 10
delay: 3
- name: The data went, and the role installed and configured tailscale again
ansible.builtin.assert:
that:
- not tailscale_test_marker.stat.exists
- not tailscale_test_state.stat.exists
- "'# molecule-marker' not in tailscale_test_defaults_lines"
- "'FLAGS=\"--tun=userspace-networking\"' in tailscale_test_defaults_lines"
- tailscale_test_dpkg.stdout == 'installed'
- tailscale_test_unit.status.UnitFileState == 'enabled'
- tailscale_test_unit.status.ActiveState == 'active'
quiet: true
vars:
tailscale_test_defaults_lines: "{{ (tailscale_test_defaults.content | b64decode).splitlines() }}"
+45 -7
View File
@@ -31,29 +31,67 @@
| regex_replace('^"(.*)"$', '\1') | regex_replace("^'(.*)'$", '\1') | split }}
tags: [tailscale, configure]
# Under --check the unit exists only when the package is installed and this run did not just
# (simulate) installing it; the defaults file ships with the package.
- name: Work out whether the tailscaled unit can be managed
ansible.builtin.set_fact:
__tailscale_unit_present: >-
{{ not ansible_check_mode
or (__tailscale_defaults_stat.stat.exists
and __tailscale_apt_source | default({}) is not changed
and __tailscale_package_install | default({}) is not changed) }}
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
register: __tailscale_flags_file
# 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: Read the state of tailscaled
become: true
ansible.builtin.systemd:
name: tailscaled
register: __tailscale_unit
check_mode: true
when: __tailscale_unit_present | bool
tags: [tailscale, configure]
# The package starts tailscaled with the flags it ships. Without a TUN device that start fails and
# Restart=on-failure can run into the start limit, which refuses a later plain start. So when the
# flags changed or the unit is not active, clear the failure state and restart now, not at the end
# of the play.
- name: Restart tailscaled with the current flags
when:
- __tailscale_unit_present | bool
- __tailscale_flags_file is changed or __tailscale_unit.status.ActiveState | default('') != 'active'
tags: [tailscale, configure]
block:
- name: Clear the failure state of tailscaled
become: true
ansible.builtin.command: systemctl reset-failed tailscaled # noqa: command-instead-of-module
changed_when: false
# Not run under --check: the restart below only reports what it would do there.
when: not ansible_check_mode
- name: Restart tailscaled
become: true
ansible.builtin.systemd:
name: tailscaled
state: restarted
- 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)
when: __tailscale_unit_present | bool
tags: [tailscale, configure]
- name: Check current Tailscale backend status
+1 -1
View File
@@ -16,7 +16,7 @@
- name: Configure Tailscale
ansible.builtin.include_tasks: configure.yml
tags: [tailscale, configure]
tags: [tailscale, configure, health]
- name: Ensure Tailscale is up-to-date
ansible.builtin.include_tasks: update.yml
+7 -1
View File
@@ -4,7 +4,8 @@
- name: Log Tailscale out of the tailnet
become: true
ansible.builtin.command: tailscale logout
changed_when: true
register: __tailscale_logout
changed_when: __tailscale_logout.rc == 0
# 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
@@ -16,6 +17,11 @@
name: tailscaled
state: stopped
enabled: false
register: __tailscale_stop
# A host without the package has no unit; there is nothing to stop then.
failed_when:
- __tailscale_stop is failed
- "'Could not find the requested service' not in __tailscale_stop.msg | default('')"
tags: [tailscale, remove]
- name: Remove Tailscale package
+12 -3
View File
@@ -12,6 +12,15 @@
The role reads gathered facts, so run it in a play that gathers them.
quiet: true
# tailscale up runs in the configure tasks (tags tailscale and configure), so the key is needed
# only when the run selects them and skips neither.
- name: Work out whether this run brings the node up
ansible.builtin.set_fact:
__tailscale_runs_up: >-
{{ tailscale_up | bool
and (ansible_run_tags | intersect(['all', 'tagged', 'tailscale', 'configure']) | length > 0)
and (ansible_skip_tags | intersect(['tailscale', 'configure', 'health']) | length == 0) }}
# 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
@@ -21,10 +30,10 @@
changed_when: false
failed_when: false
check_mode: false
when: tailscale_up | bool
when: __tailscale_runs_up | bool
# tailscale up runs when the node is not Running, and a removal logs it out first, so then the
# key is needed.
# key is needed. The configure tasks check it again against the state tailscale up acts on.
- name: Require an auth key when tailscale up will run
ansible.builtin.assert:
that:
@@ -37,7 +46,7 @@
vaulted variable, or set tailscale_up to false.
quiet: true
when:
- tailscale_up | bool
- __tailscale_runs_up | bool
- >-
tailscale_remove | bool
or (__tailscale_status_before.stdout | default('{}', true) | from_json).BackendState | default('NoState') != 'Running'