Compare commits

..
Author SHA1 Message Date
Defame1297 7bc3cc6bdf feat(traefik): skip a fresh apt refresh and trim the scenario's repeated work
Build / build (pull_request) Successful in 1m16s
Lint / lint (pull_request) Successful in 5m47s
Molecule / molecule (pull_request) Successful in 8m58s
The prerequisites install takes only distribution packages and runs
before any source is added, yet refreshed the APT cache on every run.
It now passes cache_valid_time: 3600, as docker, system_baseline and
user_management already do; the README tells a consumer who adds a
source just before the role to refresh the cache themselves.

The default scenario repeated work other steps already cover. Verify
now exercises the update path once, to the other release, so prepare
builds only that stand-in; the role default's stand-in was used only
by the way back. mortis.kamino.ufw moves to prepare, as the ufw
scenario tests it, so converge and idempotence run only traefik, with
fact gathering off since none of the roles reads facts.

Closes #458

Assisted-by: Claude Code
2026-11-28 21:39:33 +00:00
8 changed files with 97 additions and 84 deletions

No files matched your search

@@ -0,0 +1,6 @@
---
minor_changes:
- >-
traefik - The prerequisites install refreshes the APT cache only when it is older than an hour;
before, every run refreshed it. A consumer who adds an APT source just before the role must
refresh the cache first.
+52 -42
View File
@@ -297,7 +297,48 @@
changed_when: false changed_when: false
failed_when: docker_verify_start_run.rc != 2 failed_when: docker_verify_start_run.rc != 2
# Converge's daemon.json, read before the role runs below change it. # Switching an input back to false removes that unit and script and leaves the other alone.
- name: Verify that a unit is removed when its input is false
hosts: all
vars:
docker_gpg_url: file:///srv/docker-stub-repo/docker.asc
docker_keyring_checksum: "{{ docker_stub_key.stat.checksum }}"
docker_repo_base_url: file:///srv/docker-stub-repo
docker_users: [moleculeuser]
docker_start_missed: false
docker_stop_at_shutdown: true
docker_stop_at_shutdown_timeout: 20
tasks:
- name: Stat the stand-in repository key
ansible.builtin.stat:
path: /srv/docker-stub-repo/docker.asc
checksum_algorithm: sha256
register: docker_stub_key
- name: Apply the role with the boot unit off
ansible.builtin.include_role:
name: mortis.kamino.docker
- name: Stat the boot unit's files and the shutdown unit's script
ansible.builtin.stat:
path: "{{ item }}"
loop:
- /usr/local/sbin/docker-start-missed
- /etc/systemd/system/docker-start-missed.service
- /usr/local/sbin/docker-stop-at-shutdown
register: docker_verify_off
- name: The boot unit is gone and the shutdown unit is still there
ansible.builtin.assert:
that: docker_verify_off.results | map(attribute='stat.exists') | list == [false, false, true]
quiet: true
- name: Apply the role again with both inputs on
ansible.builtin.include_role:
name: mortis.kamino.docker
vars:
docker_start_missed: true
- name: Verify the daemon settings - name: Verify the daemon settings
hosts: all hosts: all
gather_facts: false gather_facts: false
@@ -328,26 +369,22 @@
- docker_verify_daemon_stat.stat.pw_name == 'root' - docker_verify_daemon_stat.stat.pw_name == 'root'
quiet: true quiet: true
# The stand-in docker.service runs for real, so a restart shows in its start time. The first run turns the boot unit off and changes # The stand-in docker.service runs for real, so a restart shows in its start time: a changed file with docker_daemon_restart false leaves
# daemon.json with docker_daemon_restart false: the unit goes, the other stays and dockerd keeps running. The second turns everything # it alone, and with true restarts it.
# back on with docker_daemon_restart true: the boot unit returns and dockerd restarts. - name: Verify that dockerd restarts only when asked
- name: Verify the unit inputs and that dockerd restarts only when asked
hosts: all hosts: all
vars: vars:
docker_gpg_url: file:///srv/docker-stub-repo/docker.asc docker_gpg_url: file:///srv/docker-stub-repo/docker.asc
docker_keyring_checksum: "{{ docker_stub_key.stat.checksum }}" docker_keyring_checksum: "{{ docker_stub_key.stat.checksum }}"
docker_repo_base_url: file:///srv/docker-stub-repo docker_repo_base_url: file:///srv/docker-stub-repo
docker_users: [moleculeuser] docker_users: [moleculeuser]
docker_start_missed: true
docker_stop_at_shutdown: true docker_stop_at_shutdown: true
docker_stop_at_shutdown_timeout: 20 docker_stop_at_shutdown_timeout: 20
docker_log_driver: local docker_log_driver: local
docker_log_max_size: 10m docker_log_max_size: 10m
docker_log_max_file: "3" docker_log_max_file: "3"
docker_live_restore: true docker_live_restore: true
docker_verify_unit_paths:
- /usr/local/sbin/docker-start-missed
- /etc/systemd/system/docker-start-missed.service
- /usr/local/sbin/docker-stop-at-shutdown
tasks: tasks:
- name: Stat the stand-in repository key - name: Stat the stand-in repository key
ansible.builtin.stat: ansible.builtin.stat:
@@ -360,11 +397,10 @@
register: docker_verify_before register: docker_verify_before
changed_when: false changed_when: false
- name: Apply the boot unit off and a changed setting with docker_daemon_restart false - name: Apply a changed setting with docker_daemon_restart false
ansible.builtin.include_role: ansible.builtin.include_role:
name: mortis.kamino.docker name: mortis.kamino.docker
vars: vars:
docker_start_missed: false
docker_daemon_options: {max-concurrent-downloads: 5} docker_daemon_options: {max-concurrent-downloads: 5}
docker_daemon_restart: false docker_daemon_restart: false
@@ -373,30 +409,10 @@
register: docker_verify_kept register: docker_verify_kept
changed_when: false changed_when: false
- name: Stat the boot unit's files and the shutdown unit's script - name: Apply another change with docker_daemon_restart true
ansible.builtin.stat:
path: "{{ item }}"
loop: "{{ docker_verify_unit_paths }}"
register: docker_verify_off
- name: Read daemon.json
ansible.builtin.slurp:
src: /etc/docker/daemon.json
register: docker_verify_daemon_kept
- name: The boot unit is gone, the shutdown unit is still there, and the changed file left dockerd running
ansible.builtin.assert:
that:
- docker_verify_off.results | map(attribute='stat.exists') | list == [false, false, true]
- (docker_verify_daemon_kept.content | b64decode | from_json)['max-concurrent-downloads'] == 5
- docker_verify_kept.stdout == docker_verify_before.stdout
quiet: true
- name: Apply both units on and another change with docker_daemon_restart true
ansible.builtin.include_role: ansible.builtin.include_role:
name: mortis.kamino.docker name: mortis.kamino.docker
vars: vars:
docker_start_missed: true
docker_daemon_options: {max-concurrent-downloads: 6} docker_daemon_options: {max-concurrent-downloads: 6}
docker_daemon_restart: true docker_daemon_restart: true
@@ -405,23 +421,17 @@
register: docker_verify_restarted register: docker_verify_restarted
changed_when: false changed_when: false
- name: Stat the unit files again - name: Read daemon.json
ansible.builtin.stat:
path: "{{ item }}"
loop: "{{ docker_verify_unit_paths }}"
register: docker_verify_on
- name: Read daemon.json again
ansible.builtin.slurp: ansible.builtin.slurp:
src: /etc/docker/daemon.json src: /etc/docker/daemon.json
register: docker_verify_daemon_after register: docker_verify_daemon_after
- name: Both units are back and the second change restarted dockerd - name: The first change left dockerd alone and the second restarted it
ansible.builtin.assert: ansible.builtin.assert:
that: that:
- docker_verify_on.results | map(attribute='stat.exists') | unique | list == [true] - docker_verify_kept.stdout == docker_verify_before.stdout
- (docker_verify_daemon_after.content | b64decode | from_json)['max-concurrent-downloads'] == 6
- docker_verify_restarted.stdout != docker_verify_kept.stdout - docker_verify_restarted.stdout != docker_verify_kept.stdout
- (docker_verify_daemon_after.content | b64decode | from_json)['max-concurrent-downloads'] == 6
quiet: true quiet: true
# A key that does not match docker_keyring_checksum fails at the key download, so the source line and packages are untouched. It goes to a separate path, so # A key that does not match docker_keyring_checksum fails at the key download, so the source line and packages are untouched. It goes to a separate path, so
+3 -2
View File
@@ -80,14 +80,14 @@ Update or remove: pass `-e traefik_update=true`, or `-e "traefik_remove=true tra
## Tests ## 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 needs outbound network: the role installs packages with apt and the converge downloads the real Traefik release from GitHub. Converge runs `mortis.kamino.ufw`, then the role. It runs `check` before the converge (on a fresh host) and after it (on the configured host). Verify covers: The `default` molecule scenario runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). It needs outbound network: the role installs packages with apt and the converge downloads the real Traefik release from GitHub. Prepare runs `mortis.kamino.ufw`, so converge runs only the role. It runs `check` before the converge (on a fresh host) and after it (on the configured host). Verify covers:
- The input guards, first: one play expects the failure message containing `unauthenticated Traefik dashboard` (the dashboard guard: enabled without valid `user:hash` users); the next expects `collides with the middleware`, raised by the dashboard-auth collision assert, which runs whenever users are set, also with the dashboard off. - The input guards, first: one play expects the failure message containing `unauthenticated Traefik dashboard` (the dashboard guard: enabled without valid `user:hash` users); the next expects `collides with the middleware`, raised by the dashboard-auth collision assert, which runs whenever users are set, also with the dashboard off.
- The install: the pinned version runs, and only the binary is left from the archive. - The install: the pinned version runs, and only the binary is left from the archive.
- Files: `traefik.env` mode 0600 owned by root, `dynamic.yml` mode 0600 owned by `traefik`, `traefik.yml` mode 0600, the binary mode 0755; the logrotate configuration, which `logrotate --debug` parses. - Files: `traefik.env` mode 0600 owned by root, `dynamic.yml` mode 0600 owned by `traefik`, `traefik.yml` mode 0600, the binary mode 0755; the logrotate configuration, which `logrotate --debug` parses.
- Rendering: the ACME resolvers render when set and are absent when empty; the dashboard is off by default with the extra app routed, and renders behind basicAuth when enabled (a template render, not a running dashboard). - Rendering: the ACME resolvers render when set and are absent when empty; the dashboard is off by default with the extra app routed, and renders behind basicAuth when enabled (a template render, not a running dashboard).
- The service: running and enabled with `NoNewPrivileges`, `MemoryDenyWriteExecute` and `CAP_NET_BIND_SERVICE`, listening on 80 and 443 and redirecting http to https; ufw allows 80 and 443 next to SSH, with the traefik owner tag, in order. - The service: running and enabled with `NoNewPrivileges`, `MemoryDenyWriteExecute` and `CAP_NET_BIND_SERVICE`, listening on 80 and 443 and redirecting http to https; ufw allows 80 and 443 next to SSH, with the traefik owner tag, in order.
- The update path: it updates to another release and back with `traefik_update` (download from local `file://` stand-in releases, checksum check and swap) and asserts the version, an active unit and no leftovers. It leaves the stand-in stub installed, so nothing after it asserts real Traefik behaviour. - The update path: it updates to another release with `traefik_update` (download from local `file://` stand-in releases, checksum check and swap) and asserts the version, an active unit and no leftovers. It leaves the stand-in stub installed, so nothing after it asserts real Traefik behaviour.
- The remove path, last: unit and binary are deleted, the service is no longer active, and only the traefik ufw rules are removed. - The remove path, last: unit and binary are deleted, the service is no longer active, and only the traefik ufw rules are removed.
The scenario disables the namespace-based sandboxing and the `User=` switch with a drop-in because its container is unprivileged, so that sandboxing is not covered by CI. Certificate issuance is not covered: the Cloudflare credentials are dummies. The scenario disables the namespace-based sandboxing and the `User=` switch with a drop-in because its container is unprivileged, so that sandboxing is not covered by CI. Certificate issuance is not covered: the Cloudflare credentials are dummies.
@@ -96,6 +96,7 @@ The scenario disables the namespace-based sandboxing and the `User=` switch with
- Idempotent: yes. - Idempotent: yes.
- Check mode: `--check` is the supported preview; a play-level `check_mode: true` is not supported. On a host without the Traefik binary, the ACME file permissions, the service start and its running check, the `Restart Traefik` handler and the `traefik_update` tasks are skipped under `--check`, because nothing is extracted and the unit file is only simulated. - Check mode: `--check` is the supported preview; a play-level `check_mode: true` is not supported. On a host without the Traefik binary, the ACME file permissions, the service start and its running check, the `Restart Traefik` handler and the `traefik_update` tasks are skipped under `--check`, because nothing is extracted and the unit file is only simulated.
- The role does not refresh an APT cache younger than an hour, so a package from a source you add just before the role (`ansible.builtin.deb822_repository` does not refresh) needs your own cache refresh first.
- The role is not a state selector. `main.yml` runs prerequisites and configure unless `traefik_remove` is true; install runs only when the binary is absent; update only when `traefik_update` is true; remove only when `traefik_remove` is true, and then every other task in `main.yml` is skipped. - The role is not a state selector. `main.yml` runs prerequisites and configure unless `traefik_remove` is true; install runs only when the binary is absent; update only when `traefik_update` is true; remove only when `traefik_remove` is true, and then every other task in `main.yml` is skipped.
- Download integrity: the archive SHA-256 must appear in the release's checksum file or the play fails. - Download integrity: the archive SHA-256 must appear in the release's checksum file or the play fails.
- Secrets: `traefik.env` (mode 0600, root) and `dynamic.yml` are rendered with `no_log: true`, and no backups are kept, so `--diff` never shows them. `dynamic.yml` may hold dashboard htpasswd hashes. - Secrets: `traefik.env` (mode 0600, root) and `dynamic.yml` are rendered with `no_log: true`, and no backups are kept, so `--diff` never shows them. `dynamic.yml` may hold dashboard htpasswd hashes.
+3 -5
View File
@@ -1,12 +1,10 @@
--- ---
# Role variables come from the inventory group_vars in molecule.yml, so the idempotence step # Role variables come from the inventory group_vars in molecule.yml, so the idempotence step
# reruns converge with identical configuration; verify's plays override only what they test. # reruns converge with identical configuration; verify's plays override only what they test.
# The base ufw play comes first: traefik passes its rules through the mortis.kamino.ufw # mortis.kamino.ufw runs in prepare.yml, so only traefik counts toward idempotence. Facts are
# service_rules entry point, which needs ufw installed and active. ufw_rules comes from the # off: neither the role nor the roles it includes reads any.
# same group_vars (the SSH allow rule the lock-out guard requires).
- name: Converge - name: Converge
hosts: all hosts: all
gather_facts: true gather_facts: false
roles: roles:
- role: mortis.kamino.ufw
- role: mortis.kamino.traefik - role: mortis.kamino.traefik
+7 -6
View File
@@ -3,7 +3,7 @@
# (MOLECULE_IMAGE) and molecule-ubuntu2604 (MOLECULE_IMAGE_2604) images, pinned in # (MOLECULE_IMAGE) and molecule-ubuntu2604 (MOLECULE_IMAGE_2604) images, pinned in
# .gitea/workflows/molecule.yml. Needs outbound network, unlike most scenarios: the role installs # .gitea/workflows/molecule.yml. Needs outbound network, unlike most scenarios: the role installs
# packages with apt and the converge downloads the Traefik release from GitHub (verify's update # packages with apt and the converge downloads the Traefik release from GitHub (verify's update
# path uses file:// stand-ins built by prepare.yml). No privileged mode: systemd needs a writable # path uses a file:// stand-in built by prepare.yml). No privileged mode: systemd needs a writable
# cgroup and the firewall tools need NET_ADMIN. # cgroup and the firewall tools need NET_ADMIN.
driver: driver:
name: docker name: docker
@@ -44,9 +44,10 @@ platforms:
- apparmor=unconfined - apparmor=unconfined
provisioner: provisioner:
name: ansible name: ansible
# One source for every play in converge.yml and verify.yml. The role renders traefik.yml before # One source for the prepare plays that read them, converge.yml and verify.yml. The role renders
# its dashboard-auth collision check, so that guard play with other values would change it (and # traefik.yml before its dashboard-auth collision check, so that guard play with other values
# restart Traefik) before verify's assertions; converge's values must also stay fixed for idempotence. # would change it (and restart Traefik) before verify's assertions; converge's values must also
# stay fixed for idempotence.
inventory: inventory:
group_vars: group_vars:
all: all:
@@ -55,8 +56,8 @@ provisioner:
traefik_cloudflare_email: [email protected] traefik_cloudflare_email: [email protected]
traefik_cloudflare_api_token: molecule-not-a-token traefik_cloudflare_api_token: molecule-not-a-token
traefik_domain: example.test traefik_domain: example.test
# Any release other than the role default: verify's update path goes there and back, # Any release other than the role default: verify's update path goes there, from the
# from the stand-ins prepare.yml builds. # stand-in prepare.yml builds.
traefik_verify_other_version: v3.7.18 traefik_verify_other_version: v3.7.18
ufw_rules: ufw_rules:
- {rule: allow, port: 22, proto: tcp, comment: ssh} - {rule: allow, port: 22, proto: tcp, comment: ssh}
+18 -19
View File
@@ -41,26 +41,34 @@
ReadWritePaths= ReadWritePaths=
User= User=
# Stand-in releases for verify's update path, behind file:// URLs: the role's download, checksum # traefik passes its rules through the mortis.kamino.ufw service_rules entry point, which needs ufw
# check and swap run unchanged, without fetching two more real releases per host. The converge # installed and active. ufw_rules comes from the inventory group_vars (the SSH allow rule the
# lock-out guard requires); the ufw scenario tests that role, so it runs once here, not in converge.
- name: Install and enable ufw
hosts: all
gather_facts: false
roles:
- role: mortis.kamino.ufw
# A stand-in release for verify's update path, behind a file:// URL: the role's download, checksum
# check and swap run unchanged, without fetching another real release per host. The converge
# still downloads the real role default from GitHub. # still downloads the real role default from GitHub.
- name: Prepare the stand-in releases - name: Prepare the stand-in release
hosts: all hosts: all
gather_facts: false gather_facts: false
vars_files: vars_files:
- ../../defaults/main.yml - ../../defaults/main.yml
vars:
traefik_test_release: "{{ traefik_verify_other_version }}"
tasks: tasks:
- name: Create a directory per stand-in release - name: Create the stand-in release directory
ansible.builtin.file: ansible.builtin.file:
path: "/opt/molecule-download/{{ traefik_test_release }}" path: "/opt/molecule-download/{{ traefik_test_release }}"
state: directory state: directory
mode: "0755" mode: "0755"
loop: ["{{ traefik_verify_other_version }}", "{{ traefik_version }}"]
loop_control:
loop_var: traefik_test_release
# Answers `version` like Traefik and otherwise stays up like the service. # Answers `version` like Traefik and otherwise stays up like the service.
- name: Write the stand-in binaries - name: Write the stand-in binary
ansible.builtin.copy: ansible.builtin.copy:
dest: "/opt/molecule-download/{{ traefik_test_release }}/traefik" dest: "/opt/molecule-download/{{ traefik_test_release }}/traefik"
mode: "0755" mode: "0755"
@@ -70,25 +78,16 @@
version) echo "Version: {{ traefik_test_release | regex_replace('^v', '') }}" ;; version) echo "Version: {{ traefik_test_release | regex_replace('^v', '') }}" ;;
*) exec sleep infinity ;; *) exec sleep infinity ;;
esac esac
loop: ["{{ traefik_verify_other_version }}", "{{ traefik_version }}"]
loop_control:
loop_var: traefik_test_release
- name: Pack the stand-in archives - name: Pack the stand-in archive
ansible.builtin.command: ansible.builtin.command:
argv: [tar, -czf, "{{ traefik_test_archive }}", -C, "/opt/molecule-download/{{ traefik_test_release }}", traefik] argv: [tar, -czf, "{{ traefik_test_archive }}", -C, "/opt/molecule-download/{{ traefik_test_release }}", traefik]
creates: "{{ traefik_test_archive }}" creates: "{{ traefik_test_archive }}"
vars: vars:
traefik_test_archive: "/opt/molecule-download/{{ traefik_test_release }}/traefik_{{ traefik_test_release }}_{{ traefik_os }}_{{ traefik_arch }}.tar.gz" traefik_test_archive: "/opt/molecule-download/{{ traefik_test_release }}/traefik_{{ traefik_test_release }}_{{ traefik_os }}_{{ traefik_arch }}.tar.gz"
loop: ["{{ traefik_verify_other_version }}", "{{ traefik_version }}"]
loop_control:
loop_var: traefik_test_release
- name: Write the checksums files in the release format - name: Write the checksums file in the release format
ansible.builtin.shell: ansible.builtin.shell:
cmd: sha256sum traefik_*.tar.gz > traefik_{{ traefik_test_release }}_checksums.txt cmd: sha256sum traefik_*.tar.gz > traefik_{{ traefik_test_release }}_checksums.txt
chdir: "/opt/molecule-download/{{ traefik_test_release }}" chdir: "/opt/molecule-download/{{ traefik_test_release }}"
creates: "traefik_{{ traefik_test_release }}_checksums.txt" creates: "traefik_{{ traefik_test_release }}_checksums.txt"
loop: ["{{ traefik_verify_other_version }}", "{{ traefik_version }}"]
loop_control:
loop_var: traefik_test_release
+7 -10
View File
@@ -293,11 +293,11 @@
traefik_ufw_key_443: "{{ ('allow|443|||||in' | hash('sha1'))[:10] }}" traefik_ufw_key_443: "{{ ('allow|443|||||in' | hash('sha1'))[:10] }}"
traefik_ufw_tagged: "{{ traefik_ufw_added.stdout_lines | select('match', '^ufw ') | select('search', 'ansible-ufw') | list }}" traefik_ufw_tagged: "{{ traefik_ufw_added.stdout_lines | select('match', '^ufw ') | select('search', 'ansible-ufw') | list }}"
# A version bump through the update path and mortis.kamino._binary_update: down to another # A version bump through the update path and mortis.kamino._binary_update: to another release
# release and back to the role default, both with traefik_update, from prepare.yml's stand-ins. # with traefik_update, from prepare.yml's stand-in. One bump covers the download, checksum check
# After the idempotence step, because each run changes the binary, and before the remove path. # and swap. After the idempotence step, because it changes the binary, and before the remove path.
# It leaves the stand-in stub installed, so nothing after it may assert real Traefik behaviour. # It leaves the stand-in stub installed, so nothing after it may assert real Traefik behaviour.
- name: Verify the update path with stand-in releases - name: Verify the update path with a stand-in release
hosts: all hosts: all
gather_facts: false gather_facts: false
vars_files: vars_files:
@@ -309,13 +309,10 @@
fail_msg: Move traefik_verify_other_version off the role default ({{ traefik_version }}). fail_msg: Move traefik_verify_other_version off the role default ({{ traefik_version }}).
quiet: true quiet: true
- name: Update to the other release, then back to the role default - name: Update to the other release
ansible.builtin.include_tasks: tasks/update_and_check.yml ansible.builtin.include_tasks: tasks/update_and_check.yml
loop: vars:
- "{{ traefik_verify_other_version }}" traefik_verify_target: "{{ traefik_verify_other_version }}"
- "{{ traefik_version }}"
loop_control:
loop_var: traefik_verify_target
# The remove path. Last on purpose: it destroys what the plays above verify, and it runs # The remove path. Last on purpose: it destroys what the plays above verify, and it runs
# once, outside the idempotence step (a converge play that changed state would fail that step). # once, outside the idempotence step (a converge play that changed state would fail that step).
+1
View File
@@ -11,6 +11,7 @@
- logrotate - logrotate
state: present state: present
update_cache: true update_cache: true
cache_valid_time: 3600
tags: [traefik, prereqs, always] tags: [traefik, prereqs, always]
- name: Create Traefik group - name: Create Traefik group