After a removal without purge the role has disabled tailscaled, so the reinstall's postinst does not start it. systemd then unloads the inactive, unreferenced unit, and `systemctl reset-failed tailscaled` exits 1 with "Unit tailscaled.service not loaded.", failing the configure step on both noble and resolute. An unloaded unit has no failure state to clear, and the restart that follows loads it, so that one error is not a failure. Assisted-by: Claude Code:claude-opus-5-5
175 lines
6.9 KiB
YAML
175 lines
6.9 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]
|
|
|
|
# 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(' ') }}"
|
|
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]
|
|
|
|
- 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
|
|
register: __tailscale_reset_failed
|
|
changed_when: false
|
|
# systemd unloads an inactive unit nothing references (a disabled one the package did not
|
|
# start, e.g. after a removal without purge); it then has no failure state to clear, and the
|
|
# restart below loads it.
|
|
failed_when:
|
|
- __tailscale_reset_failed.rc != 0
|
|
- "'not loaded' not in __tailscale_reset_failed.stderr | default('')"
|
|
# 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
|
|
when: __tailscale_unit_present | bool
|
|
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
|