Merge branch 'main' into fix/364-ansible-facts
Build / build (pull_request) Successful in 29s
Lint / lint (pull_request) Successful in 2m10s
Molecule / molecule (pull_request) Canceled after 5m20s

This commit is contained in:
Defame1297 committed 2026-11-26 21:44:16 +00:00
commit d1b4501c2b
18 files changed
+1111 -3

No files matched your search

+73
View File
@@ -0,0 +1,73 @@
# tailscale
Installs [Tailscale](https://tailscale.com) from the upstream APT repository with the keyring pinned by sha256, sets the `tailscaled` flags, enables `tailscaled` and brings the node onto the tailnet with an auth key. It can also update to the APT candidate version, and remove or purge. Variables and their types are defined in [`meta/argument_specs.yml`](meta/argument_specs.yml), which is the source of truth.
## Supported platforms
Ubuntu 24.04 (noble) and 26.04 (resolute), the platforms the molecule scenario runs on. The role asserts that the host is Ubuntu before any change, and the repository suite follows the host's release (`tailscale_apt_release`). The host needs outbound access to pkgs.tailscale.com.
The role targets Ubuntu hosts, not Proxmox nodes, so it does not cover Debian trixie: it asserts Ubuntu and is tested only there.
## Requirements and run order
- 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`) 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.
## Role variables
| Variable | Default | Description |
|---|---|---|
| `tailscale_up` | `true` | Run `tailscale up` when the backend state is not Running. False installs and runs `tailscaled` without joining a tailnet. |
| `tailscale_auth_key` | none | Auth key for `tailscale up`. No default; see Requirements. `no_log`. |
| `tailscale_update` | `false` | Install the package's APT candidate version. |
| `tailscale_remove` | `false` | Log out, stop and disable `tailscaled`, remove the package, the source list and the keyring, then install and configure again. |
| `tailscale_remove_data` | `false` | With `tailscale_remove`: purge the package and delete `tailscale_state_dir`. |
| `tailscale_userspace_networking` | `false` | Add `--tun=userspace-networking` to `FLAGS` in `/etc/default/tailscaled`; false removes it again. |
| `tailscale_package` | `tailscale` | APT package name. |
| `tailscale_apt_distribution` | `ubuntu` | Repository distribution directory. |
| `tailscale_apt_release` | the host's release (`ansible_facts['distribution_release']`) | Repository suite and keyring release. |
| `tailscale_repo_url` | `https://pkgs.tailscale.com/stable/<tailscale_apt_distribution>` | APT repository URL in the source list. |
| `tailscale_keyring_url` | `<tailscale_repo_url>/<tailscale_apt_release>.noarmor.gpg` | Keyring download URL. |
| `tailscale_keyring_checksum` | sha256 of the current Tailscale keyring | Expected sha256 of the keyring; a mismatch fails the download. |
| `tailscale_keyring_path` | `/usr/share/keyrings/tailscale-archive-keyring.gpg` | Keyring destination. |
| `tailscale_apt_list_path` | `/etc/apt/sources.list.d/tailscale.list` | APT source list destination. |
| `tailscale_state_dir` | `/var/lib/tailscale` | Deleted when `tailscale_remove_data` is true. |
## 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. `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
```yaml
- name: Join the tailnet
hosts: tailnet_nodes
roles:
- role: mortis.kamino.tailscale
vars:
tailscale_auth_key: "{{ vault_tailscale_auth_key }}"
```
Removal and update are explicit: `-e tailscale_remove=true` (add `tailscale_remove_data=true` to purge) removes and then reinstalls, while `--tags remove` runs only the removal; `-e tailscale_update=true --tags update` updates.
## 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), 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
- Idempotent: yes, with `tailscale_up: false` (the scenario checks it). With `tailscale_up: true`, `tailscale up` runs only while the node is not Running.
- Check mode: `--check` is the supported preview; a play-level `check_mode: true` is not supported. On a host without the package, the package install, the `FLAGS` edit, the service start and the update are skipped under `--check`, because the new source line is only simulated. `tailscale up` is always skipped under `--check`. The auth key is still asserted.
- 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, 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.
+24
View File
@@ -0,0 +1,24 @@
---
# tailscale_auth_key has no default: it is a secret, required whenever tailscale up has to run.
tailscale_up: true
tailscale_update: false
tailscale_remove: false
tailscale_remove_data: false
# Runs tailscaled without a TUN device, for hosts (containers) that have no /dev/net/tun.
tailscale_userspace_networking: false
tailscale_package: tailscale
tailscale_apt_distribution: ubuntu
# The default keeps argument validation from failing on a play without facts, so the Ubuntu assert
# in tasks/validate.yml gives the reason instead.
tailscale_apt_release: "{{ ansible_facts['distribution_release'] | default('') }}"
tailscale_repo_url: "https://pkgs.tailscale.com/stable/{{ tailscale_apt_distribution }}"
tailscale_keyring_url: "{{ tailscale_repo_url }}/{{ tailscale_apt_release }}.noarmor.gpg"
# sha256 of Tailscale's package signing key (2596A99EAAB33821893C0A79458CA832957F5868), the same
# file for noble and resolute. Static on purpose: a rotated key fails the install until a person
# has checked the new one and updated this hash.
tailscale_keyring_checksum: "3e03dacf222698c60b8e2f990b809ca1b3e104de127767864284e6c228f1fb39"
tailscale_keyring_path: /usr/share/keyrings/tailscale-archive-keyring.gpg
tailscale_apt_list_path: /etc/apt/sources.list.d/tailscale.list
tailscale_state_dir: /var/lib/tailscale
+6
View File
@@ -0,0 +1,6 @@
---
- name: Restart tailscaled
become: true
ansible.builtin.systemd:
name: tailscaled
state: restarted
+101
View File
@@ -0,0 +1,101 @@
---
argument_specs:
main:
short_description: Install Tailscale and bring the node up
description:
- >-
Asserts an Ubuntu host, optionally removes Tailscale first, installs the package from the upstream APT repository
with the keyring pinned by sha256, sets the C(tailscaled) flags, enables C(tailscaled), runs C(tailscale up) when
C(tailscale_up) is true and the backend state is not Running, and optionally updates the package to the APT
candidate version.
- Needs gathered facts. It owns no firewall rule. No Tailscale SSH flag and no ACL tag are set.
options:
tailscale_up:
type: bool
default: true
description:
- >-
Runs C(tailscale up) when the backend state is not Running. C(tailscale_auth_key) must then be a non-empty
string; this is asserted before any change. False installs and runs C(tailscaled) without joining a tailnet.
tailscale_auth_key:
type: str
no_log: true
description:
- >-
Auth key for C(tailscale up), handed over through a root-owned 0600 temporary file that is always removed
afterwards. No default. Required, non-empty, when C(tailscale_up) is true and the node is not Running (or
C(tailscale_remove) is true); otherwise not read.
tailscale_update:
type: bool
default: false
description:
- Refreshes the APT cache and installs the package's candidate version, pinned as package=version.
tailscale_remove:
type: bool
default: false
description:
- >-
Logs out (best effort), stops and disables C(tailscaled), removes the package, the APT source list and the
keyring, then installs and configures Tailscale again.
tailscale_remove_data:
type: bool
default: false
description:
- Only used with C(tailscale_remove). Purges the package and deletes C(tailscale_state_dir).
tailscale_userspace_networking:
type: bool
default: false
description:
- >-
Adds C(--tun=userspace-networking) to C(FLAGS) in C(/etc/default/tailscaled) and restarts C(tailscaled), for
hosts without C(/dev/net/tun). False removes that flag again. Other flags in C(FLAGS) are kept.
tailscale_package:
type: str
default: tailscale
description:
- APT package installed, updated or removed.
tailscale_apt_distribution:
type: str
default: ubuntu
description:
- Distribution directory of the Tailscale repository, used in C(tailscale_repo_url) and the source list comment.
tailscale_apt_release:
type: raw
description:
- >-
Release codename of the repository suite and of the keyring file name. The role default is the Jinja
expression C(ansible_facts['distribution_release']), the host's own release.
tailscale_repo_url:
type: raw
description:
- >-
APT repository URL written to the source list. The role default is a Jinja expression built from
C(tailscale_apt_distribution) (C(https://pkgs.tailscale.com/stable/ubuntu)).
tailscale_keyring_url:
type: raw
description:
- >-
Download URL of the APT keyring. The role default is a Jinja expression built from C(tailscale_repo_url) and
C(tailscale_apt_release) (C(https://pkgs.tailscale.com/stable/ubuntu/<release>.noarmor.gpg)).
tailscale_keyring_checksum:
type: str
default: 3e03dacf222698c60b8e2f990b809ca1b3e104de127767864284e6c228f1fb39
description:
- >-
sha256 (hex) of the keyring at C(tailscale_keyring_url). The download fails on a mismatch, so a rotated
upstream key stops the install until the new key is checked and the hash updated.
tailscale_keyring_path:
type: path
default: /usr/share/keyrings/tailscale-archive-keyring.gpg
description:
- Destination of the downloaded keyring.
tailscale_apt_list_path:
type: path
default: /etc/apt/sources.list.d/tailscale.list
description:
- Destination of the APT source list.
tailscale_state_dir:
type: path
default: /var/lib/tailscale
description:
- State directory of C(tailscaled), deleted when C(tailscale_remove_data) is true.
+18
View File
@@ -0,0 +1,18 @@
---
galaxy_info:
role_name: tailscale
namespace: mortis
author: Defame1297
license: GPL-3.0-or-later
description: Install Tailscale from the upstream APT repository and bring the node up with an auth key.
min_ansible_version: "2.22"
platforms:
- name: Ubuntu
versions:
- noble
- resolute
galaxy_tags:
- tailscale
- vpn
- networking
dependencies: []
@@ -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
+385
View File
@@ -0,0 +1,385 @@
---
# 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() }}"
+174
View File
@@ -0,0 +1,174 @@
---
# --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
+41
View File
@@ -0,0 +1,41 @@
---
- name: Ensure APT keyrings directory exists
become: true
ansible.builtin.file:
path: "{{ tailscale_keyring_path | dirname }}"
state: directory
mode: "0755"
tags: [tailscale, install]
- name: Install Tailscale APT keyring
become: true
ansible.builtin.get_url:
url: "{{ tailscale_keyring_url }}"
dest: "{{ tailscale_keyring_path }}"
checksum: "sha256:{{ tailscale_keyring_checksum }}"
mode: "0644"
tags: [tailscale, install]
# Same content as upstream's <release>.tailscale-keyring.list.
- name: Install Tailscale APT source list
become: true
ansible.builtin.copy:
dest: "{{ tailscale_apt_list_path }}"
content: |
# Tailscale packages for {{ tailscale_apt_distribution }} {{ tailscale_apt_release }}
deb [signed-by={{ tailscale_keyring_path }}] {{ tailscale_repo_url }} {{ tailscale_apt_release }} main
mode: "0644"
register: __tailscale_apt_source
tags: [tailscale, install]
- name: Install Tailscale package
become: true
ansible.builtin.apt:
name: "{{ tailscale_package }}"
state: present
update_cache: true
register: __tailscale_package_install
notify: Restart tailscaled
# Under --check a new source line is only simulated, so apt would find no tailscale package.
when: not ansible_check_mode or __tailscale_apt_source is not changed
tags: [tailscale, install]
+30
View File
@@ -0,0 +1,30 @@
---
# Validation first, under always, so a bad input fails before any change whatever tags the run
# selects.
- name: Validate the role inputs
ansible.builtin.import_tasks: validate.yml
tags: always
- name: Remove Tailscale (explicit only)
ansible.builtin.include_tasks: remove.yml
when: tailscale_remove | bool
tags: [tailscale, remove]
- name: Install Tailscale
ansible.builtin.include_tasks: install.yml
tags: [tailscale, install]
- name: Configure Tailscale
ansible.builtin.include_tasks: configure.yml
tags: [tailscale, configure, health]
- name: Ensure Tailscale is up-to-date
ansible.builtin.include_tasks: update.yml
when:
- tailscale_update | bool
# Under --check a new source line is only simulated, so apt would have no candidate.
- not ansible_check_mode or __tailscale_apt_source | default({}) is not changed
tags: [tailscale, update]
- name: Flush handlers
ansible.builtin.meta: flush_handlers
+55
View File
@@ -0,0 +1,55 @@
---
# Run with tailscale_remove=true (and tailscale_remove_data=true to purge), tag: remove. main.yml
# installs and configures Tailscale again afterwards.
- name: Log Tailscale out of the tailnet
become: true
ansible.builtin.command: tailscale logout
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
tags: [tailscale, remove]
- name: Stop and disable tailscaled service
become: true
ansible.builtin.systemd:
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
become: true
ansible.builtin.apt:
name: "{{ tailscale_package }}"
state: absent
purge: "{{ tailscale_remove_data | bool }}"
tags: [tailscale, remove]
- name: Remove Tailscale APT source list
become: true
ansible.builtin.file:
path: "{{ tailscale_apt_list_path }}"
state: absent
tags: [tailscale, remove]
- name: Remove Tailscale APT keyring
become: true
ansible.builtin.file:
path: "{{ tailscale_keyring_path }}"
state: absent
tags: [tailscale, remove]
- name: Remove Tailscale state directory
become: true
ansible.builtin.file:
path: "{{ tailscale_state_dir }}"
state: absent
when: tailscale_remove_data | bool
tags: [tailscale, remove]
+39
View File
@@ -0,0 +1,39 @@
---
# Run with tailscale_update=true (tag: update).
- name: Refresh APT cache before checking for Tailscale updates
become: true
ansible.builtin.apt:
update_cache: true
tags: [tailscale, update]
- name: Determine latest available Tailscale candidate version
ansible.builtin.command: apt-cache policy {{ tailscale_package | quote }}
register: __tailscale_apt_policy
changed_when: false
# Read-only, so it also runs in the --check preview, which needs its stdout.
check_mode: false
tags: [tailscale, update]
- name: Extract latest Tailscale candidate version
ansible.builtin.set_fact:
__tailscale_candidate_version: >-
{{ __tailscale_apt_policy.stdout | regex_search('Candidate:\s+(\S+)', '\1') | default([''], true) | first }}
tags: [tailscale, update]
- name: Fail if Tailscale has no installable candidate
ansible.builtin.fail:
msg: >-
No installable candidate version found for {{ tailscale_package }} (apt-cache policy reported
'{{ __tailscale_candidate_version }}'). Check that the Tailscale APT repository is reachable.
when: __tailscale_candidate_version in ['', '(none)']
tags: [tailscale, update]
# Pinned to the resolved candidate version (never state=latest), so the task stays idempotent and
# passes ansible-lint's package-latest rule.
- name: Update Tailscale package to latest candidate version
become: true
ansible.builtin.apt:
name: "{{ tailscale_package }}={{ __tailscale_candidate_version }}"
state: present
notify: Restart tailscaled
tags: [tailscale, update]
+52
View File
@@ -0,0 +1,52 @@
---
# Input checks. Imported first in main.yml with tags: always, so a bad input fails before any
# change. The argument spec (types) runs before this file.
# The repository suite and the source list follow the host's release, read from gathered facts.
- name: Require Ubuntu
ansible.builtin.assert:
that:
- ansible_facts['distribution'] | default('') == 'Ubuntu'
fail_msg: >-
tailscale supports Ubuntu only (found: {{ ansible_facts['distribution'] | default('no gathered facts') }}).
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
become: true
ansible.builtin.command: tailscale status --json
register: __tailscale_status_before
changed_when: false
failed_when: false
check_mode: false
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. 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:
- 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
(or tailscale_remove is true), so the role runs tailscale up. Set the key, normally from a
vaulted variable, or set tailscale_up to false.
quiet: true
when:
- __tailscale_runs_up | bool
- >-
tailscale_remove | bool
or (__tailscale_status_before.stdout | default('{}', true) | from_json).BackendState | default('NoState') != 'Running'