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

@@ -0,0 +1,8 @@
---
# tailscale_up false and userspace networking come from molecule.yml: the node installs and runs
# tailscaled but never joins a tailnet.
- name: Converge
hosts: all
gather_facts: true
roles:
- role: mortis.kamino.tailscale
@@ -0,0 +1,72 @@
---
# Runs on the repository's own runner (label kamino-molecule), in the molecule-ubuntu2404
# (MOLECULE_IMAGE) and molecule-ubuntu2604 (MOLECULE_IMAGE_2604) images, pinned in
# .gitea/workflows/molecule.yml. Needs outbound network: the role installs the real package from
# pkgs.tailscale.com. The node never joins a tailnet (tailscale_up false), and tailscaled runs with
# userspace networking because a container may have no /dev/net/tun. The containers run systemd as
# PID 1 with a writable cgroup; no privileged mode.
driver:
name: docker
platforms:
- name: ${KAMINO_RUN_ID:-local}-tailscale-noble
labels:
net.rkdr.mortis.molecule-run: "${KAMINO_RUN_ID:-local}"
image: ${MOLECULE_IMAGE}
pre_build_image: true
pull: true
command: /sbin/init
cgroupns_mode: host
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:rw
tmpfs:
- /run
- /run/lock
capabilities:
- NET_ADMIN
security_opts:
- apparmor=unconfined
- name: ${KAMINO_RUN_ID:-local}-tailscale-resolute
labels:
net.rkdr.mortis.molecule-run: "${KAMINO_RUN_ID:-local}"
image: ${MOLECULE_IMAGE_2604}
pre_build_image: true
pull: true
command: /sbin/init
cgroupns_mode: host
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:rw
tmpfs:
- /run
- /run/lock
capabilities:
- NET_ADMIN
security_opts:
- apparmor=unconfined
provisioner:
name: ansible
# One source for every play, so the converge and the plays in verify.yml start from the same
# valid input.
inventory:
group_vars:
all:
tailscale_up: false
tailscale_userspace_networking: true
verifier:
name: ansible
scenario:
# molecule's default sequence minus both destroys (scripts/run_molecule_scenarios.sh removes the
# containers as soon as this scenario ends; after a local run, run `molecule destroy` yourself),
# plus a check before the converge (fresh host) and one after it (configured host).
test_sequence:
- dependency
- cleanup
- syntax
- create
- prepare
- check
- converge
- check
- idempotence
- side_effect
- verify
- cleanup
@@ -0,0 +1,21 @@
---
# One failing run: includes the role with the inputs the caller sets on the include, and asserts
# that the expected message, not something else, stopped it. Input: tailscale_test_case (name,
# expect).
- name: "Run the role, case {{ tailscale_test_case.name }}"
block:
- name: "Include the role, case {{ tailscale_test_case.name }}"
ansible.builtin.include_role:
name: mortis.kamino.tailscale
# Only reached when the include returned. The message must not contain the expected
# text, so the rescue below cannot mistake it for the failure under test.
- name: "Fail because the include returned, case {{ tailscale_test_case.name }}"
ansible.builtin.fail:
msg: The role accepted the input
rescue:
- name: "The expected failure stopped the run, case {{ tailscale_test_case.name }}"
ansible.builtin.assert:
that: tailscale_test_case.expect in (ansible_failed_result.msg | default(''))
fail_msg: "Expected '{{ tailscale_test_case.expect }}', got: {{ ansible_failed_result.msg | default('') }}"
quiet: true
+280
View File
@@ -0,0 +1,280 @@
---
# Runs first, before any play gathers facts: the role refuses a host it cannot identify as
# Ubuntu, before any change.
- name: Refuse a host without Ubuntu facts
hosts: all
gather_facts: false
tasks:
- name: Run the role without gathered facts
ansible.builtin.include_tasks: tasks/failed_run.yml
vars:
tailscale_test_case:
name: no-facts
expect: tailscale supports Ubuntu only
# Negative tests: each validation stops the role before any change. The node is not Running (it
# never joined a tailnet), so tailscale_up true needs an auth key.
- name: Refuse invalid input
hosts: all
gather_facts: true
tasks:
- name: Refuse tailscale_up that is not a boolean
ansible.builtin.include_tasks: tasks/failed_run.yml
vars:
tailscale_up: maybe
tailscale_test_case:
name: up-not-bool
expect: "argument 'tailscale_up' is of type str and we were unable to convert to bool"
- name: Refuse tailscale_up without an auth key
ansible.builtin.include_tasks: tasks/failed_run.yml
vars:
tailscale_up: true
tailscale_test_case:
name: up-without-key
expect: tailscale_auth_key must be a non-empty string
- name: Refuse tailscale_up with an empty auth key
ansible.builtin.include_tasks: tasks/failed_run.yml
vars:
tailscale_up: true
tailscale_auth_key: ""
tailscale_test_case:
name: up-empty-key
expect: tailscale_auth_key must be a non-empty string
- name: Verify the install
hosts: all
gather_facts: true
tasks:
- name: Stat the keyring
ansible.builtin.stat:
path: /usr/share/keyrings/tailscale-archive-keyring.gpg
checksum_algorithm: sha256
register: tailscale_test_keyring
# The hash of Tailscale's package signing key as published, written out here rather than read
# from the role defaults, so a changed default cannot pass by itself.
- name: The keyring is the pinned key, world-readable for apt
ansible.builtin.assert:
that:
- tailscale_test_keyring.stat.checksum == '3e03dacf222698c60b8e2f990b809ca1b3e104de127767864284e6c228f1fb39'
- tailscale_test_keyring.stat.mode == '0644'
quiet: true
- name: Read the APT source list
ansible.builtin.slurp:
src: /etc/apt/sources.list.d/tailscale.list
register: tailscale_test_source
- name: The source line names the keyring and the host's release
ansible.builtin.assert:
that:
- >-
('deb [signed-by=/usr/share/keyrings/tailscale-archive-keyring.gpg] https://pkgs.tailscale.com/stable/ubuntu '
~ ansible_facts['distribution_release'] ~ ' main')
in (tailscale_test_source.content | b64decode).splitlines()
quiet: true
- name: Ask apt where tailscale comes from
ansible.builtin.command: apt-cache policy tailscale
register: tailscale_test_policy
changed_when: false
- 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
- name: The package is installed from pkgs.tailscale.com
ansible.builtin.assert:
that:
- tailscale_test_dpkg.stdout == 'installed'
- "'Installed: (none)' not in tailscale_test_policy.stdout"
- "'https://pkgs.tailscale.com/stable/ubuntu ' ~ ansible_facts['distribution_release'] in tailscale_test_policy.stdout"
quiet: true
- name: Ask systemd about tailscaled
ansible.builtin.systemd:
name: tailscaled
register: tailscale_test_unit
check_mode: true
- name: Tailscaled is enabled and running
ansible.builtin.assert:
that:
- tailscale_test_unit.status.UnitFileState == 'enabled'
- tailscale_test_unit.status.ActiveState == 'active'
quiet: true
- name: Read the tailscaled defaults file
ansible.builtin.slurp:
src: /etc/default/tailscaled
register: tailscale_test_defaults
- name: FLAGS carries userspace networking and the package's PORT line is kept
ansible.builtin.assert:
that:
- tailscale_test_defaults_lines | select('match', '^FLAGS=') | list == ['FLAGS="--tun=userspace-networking"']
- "'PORT=\"41641\"' in tailscale_test_defaults_lines"
quiet: true
vars:
tailscale_test_defaults_lines: "{{ (tailscale_test_defaults.content | b64decode).splitlines() }}"
# The effect on the service, not only the file: the running daemon got the flag.
- name: Read the main PID of tailscaled
ansible.builtin.command: systemctl show --property=MainPID --value tailscaled # noqa: command-instead-of-module
register: tailscale_test_pid
changed_when: false
- name: Read the command line of the running daemon
ansible.builtin.slurp:
src: /proc/{{ tailscale_test_pid.stdout }}/cmdline
register: tailscale_test_cmdline
- name: The running daemon uses userspace networking
ansible.builtin.assert:
that:
- "'--tun=userspace-networking' in (tailscale_test_cmdline.content | b64decode)"
quiet: true
- name: Ask tailscaled for its backend state
ansible.builtin.command: tailscale status --json
register: tailscale_test_status
changed_when: false
failed_when: false
# tailscale_up is false: the daemon answers, and the node did not join a tailnet.
- name: The daemon answers and the node is not up
ansible.builtin.assert:
that:
- (tailscale_test_status.stdout | from_json).BackendState is defined
- (tailscale_test_status.stdout | from_json).BackendState != 'Running'
quiet: true
# The role pins the update to the APT candidate: downgrade to an older version from the same
# repository, then run with tailscale_update.
- name: Verify the update path
hosts: all
gather_facts: true
tasks:
- name: List the versions the repository offers
ansible.builtin.command: apt-cache madison tailscale
register: tailscale_test_madison
changed_when: false
- name: Read the candidate version
ansible.builtin.command: apt-cache policy tailscale
register: tailscale_test_policy
changed_when: false
- name: Pick the candidate and the newest older version
ansible.builtin.set_fact:
tailscale_test_candidate: "{{ tailscale_test_candidate_v }}"
tailscale_test_older: "{{ tailscale_test_versions | reject('equalto', tailscale_test_candidate_v) | first }}"
vars:
tailscale_test_candidate_v: "{{ tailscale_test_policy.stdout | regex_search('Candidate:\\s+(\\S+)', '\\1') | first }}"
tailscale_test_versions: "{{ tailscale_test_madison.stdout_lines | map('split', '|') | map(attribute=1) | map('trim') | list }}"
- name: Downgrade tailscale
ansible.builtin.apt:
name: "tailscale={{ tailscale_test_older }}"
allow_downgrade: true
state: present
- name: Read the installed version after the downgrade
ansible.builtin.command:
argv: [dpkg-query, --show, --showformat, "${Version}", tailscale]
register: tailscale_test_downgraded
changed_when: false
- name: The downgrade took effect
ansible.builtin.assert:
that: tailscale_test_downgraded.stdout == tailscale_test_older
quiet: true
- name: Run the role with tailscale_update
ansible.builtin.include_role:
name: mortis.kamino.tailscale
vars:
tailscale_update: true
- name: Read the installed version after the update
ansible.builtin.command:
argv: [dpkg-query, --show, --showformat, "${Version}", tailscale]
register: tailscale_test_updated
changed_when: false
- name: Ask systemd about tailscaled after the update
ansible.builtin.systemd:
name: tailscaled
register: tailscale_test_unit
check_mode: true
- name: The candidate is installed and tailscaled runs
ansible.builtin.assert:
that:
- tailscale_test_updated.stdout == tailscale_test_candidate
- 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
hosts: all
gather_facts: true
tasks:
- name: Put a marker in the state directory
ansible.builtin.copy:
content: marker
dest: /var/lib/tailscale/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
- name: Stat the state marker
ansible.builtin.stat:
path: /var/lib/tailscale/molecule-marker
register: tailscale_test_marker
- name: Read the defaults file
ansible.builtin.slurp:
src: /etc/default/tailscaled
register: tailscale_test_defaults
- 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
- name: Ask systemd about tailscaled
ansible.builtin.systemd:
name: tailscaled
register: tailscale_test_unit
check_mode: true
- name: The data went, and the role installed and configured tailscale again
ansible.builtin.assert:
that:
- not tailscale_test_marker.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() }}"