- 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
386 lines
14 KiB
YAML
386 lines
14 KiB
YAML
---
|
|
# 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
|
|
|
|
# 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:
|
|
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
|
|
|
|
# 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:
|
|
that:
|
|
- tailscale_test_updated.stdout == tailscale_test_candidate
|
|
- tailscale_test_unit.status.ActiveState == 'active'
|
|
quiet: true
|
|
|
|
# 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:
|
|
- 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 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 directory
|
|
ansible.builtin.stat:
|
|
path: "{{ tailscale_test_state_dir }}"
|
|
register: tailscale_test_state
|
|
|
|
- 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: 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_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'
|
|
quiet: true
|
|
vars:
|
|
tailscale_test_defaults_lines: "{{ (tailscale_test_defaults.content | b64decode).splitlines() }}"
|