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

+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'