The archive and checksums URLs were hardcoded to the GitHub releases. traefik_release_url (default: the GitHub release directory of traefik_version, as before) lets a host download from a mirror. The Molecule scenario uses it: verify's update path now downloads stand-in releases that prepare.yml builds under /opt/molecule-download (a stub binary answering `version`, its archive and a sha256sum checksums file), so it no longer fetches two more real releases per host. The converge still downloads the real release from GitHub, which keeps the default URL proven. traefik_verify_other_version moves to the scenario's group vars so prepare and verify share it. The update path leaves the stub installed, which the README and verify now state. Verify replaces the fixed 15 s pause with a poll (30 x 2 s) for an "Unable to obtain ACME certificate" log line stamped at or after the running process's ExecMainStartTimestamp, so a line left by the process converge restarted does not count. It fails closed on a missing start time, a missing line or an unparseable stamp. It then asserts NRestarts is 0, since Restart=always would hide a crash. Assisted-by: Claude Code:claude-opus-5-5
386 lines
16 KiB
YAML
386 lines
16 KiB
YAML
---
|
|
# The input guards. Both fail after the role's prereqs (packages already present), and the
|
|
# collision case re-renders traefik.yml from the same group_vars with the dashboard still off,
|
|
# so neither changes the host. They run first, so every assertion below also shows that; the
|
|
# remove path stays last, since after it the role would install Traefik again.
|
|
- name: Refuse an enabled dashboard without basicAuth users
|
|
hosts: all
|
|
gather_facts: false
|
|
tasks:
|
|
- name: Run the role with the dashboard on and no users
|
|
block:
|
|
- name: Include traefik
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.traefik
|
|
vars:
|
|
traefik_dashboard_enabled: true
|
|
traefik_dashboard_basicauth_users: []
|
|
- name: Unreachable when the guard works
|
|
ansible.builtin.fail:
|
|
msg: traefik accepted an enabled dashboard without users
|
|
rescue:
|
|
- name: The dashboard guard, not something else, stopped the play
|
|
ansible.builtin.assert:
|
|
that: "'unauthenticated Traefik dashboard' in ansible_failed_result.msg | default('')"
|
|
quiet: true
|
|
|
|
- name: Refuse a dashboard-auth middleware collision
|
|
hosts: all
|
|
gather_facts: false
|
|
tasks:
|
|
- name: Run the role with a colliding middleware
|
|
block:
|
|
- name: Include traefik
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.traefik
|
|
vars:
|
|
traefik_dashboard_basicauth_users:
|
|
- "admin:$apr1$x$y"
|
|
traefik_dynamic_middlewares:
|
|
dashboard-auth:
|
|
basicAuth: {users: []}
|
|
- name: Unreachable when the check works
|
|
ansible.builtin.fail:
|
|
msg: traefik accepted a colliding dashboard-auth middleware
|
|
rescue:
|
|
- name: The collision check, not something else, stopped the play
|
|
ansible.builtin.assert:
|
|
that: "'collides with the middleware' in ansible_failed_result.msg | default('')"
|
|
quiet: true
|
|
|
|
- name: Verify
|
|
hosts: all
|
|
gather_facts: false
|
|
vars_files:
|
|
- ../../defaults/main.yml
|
|
tasks:
|
|
- name: Check the installed Traefik version
|
|
ansible.builtin.command: /usr/local/bin/traefik version
|
|
register: traefik_version_out
|
|
changed_when: false
|
|
|
|
- name: Stat the secret-bearing files and the binary
|
|
ansible.builtin.stat:
|
|
path: "{{ traefik_verify_file }}"
|
|
loop:
|
|
- /etc/traefik/traefik.env
|
|
- /etc/traefik/dynamic/dynamic.yml
|
|
- /etc/traefik/traefik.yml
|
|
- /usr/local/bin/traefik
|
|
loop_control:
|
|
loop_var: traefik_verify_file
|
|
register: traefik_files
|
|
|
|
- name: Read the dynamic configuration
|
|
ansible.builtin.slurp:
|
|
src: /etc/traefik/dynamic/dynamic.yml
|
|
register: traefik_dynamic
|
|
|
|
- name: List listening TCP sockets
|
|
ansible.builtin.command: ss -ltnH
|
|
register: traefik_sockets
|
|
changed_when: false
|
|
|
|
- name: Ask the web entrypoint for its redirect
|
|
ansible.builtin.uri:
|
|
url: http://127.0.0.1:80/
|
|
follow_redirects: none
|
|
status_code: [301, 302, 307, 308]
|
|
register: traefik_http
|
|
retries: 5
|
|
delay: 2
|
|
until: traefik_http.status is defined and traefik_http.status in [301, 302, 307, 308]
|
|
|
|
- name: Read the ufw rules
|
|
ansible.builtin.command: ufw status
|
|
register: traefik_ufw
|
|
changed_when: false
|
|
|
|
- name: Read the rules ufw has stored
|
|
ansible.builtin.command: ufw show added
|
|
register: traefik_ufw_added
|
|
changed_when: false
|
|
environment: {LC_ALL: C}
|
|
|
|
- name: Read the effective unit settings
|
|
ansible.builtin.systemd:
|
|
name: traefik
|
|
register: traefik_unit
|
|
|
|
# The dummy credentials make the ACME request fail, logged as "Unable to obtain ACME certificate
|
|
# for domain(s)". The log keeps the lines of the process converge restarted, so only a line
|
|
# stamped (RFC 3339, first field) since the running process started counts.
|
|
- name: Wait until the running Traefik has logged the ACME failure
|
|
ansible.builtin.shell:
|
|
executable: /bin/bash
|
|
cmd: |
|
|
set -euo pipefail
|
|
started=$(systemctl show --timestamp=unix -P ExecMainStartTimestamp traefik)
|
|
if [[ ! $started =~ ^@[0-9]+$ ]]; then
|
|
echo "no start time for traefik: '${started}'" >&2
|
|
exit 1
|
|
fi
|
|
last=$(grep 'Unable to obtain ACME certificate' /etc/traefik/logs/traefik.log | tail -n 1 | cut -d ' ' -f 1) || true
|
|
# Fails closed: a missing line, an unparseable stamp or an older one all end in exit 1.
|
|
if [ -n "$last" ] && stamp=$(date -d "$last" +%s) && [ "$stamp" -ge "${started#@}" ]; then
|
|
exit 0
|
|
fi
|
|
echo "no ACME failure logged since Traefik started at ${started} (last: ${last:-none})" >&2
|
|
exit 1
|
|
register: traefik_acme_failure
|
|
changed_when: false
|
|
until: traefik_acme_failure.rc == 0
|
|
retries: 30
|
|
delay: 2
|
|
|
|
- name: Read the service state after the ACME failure
|
|
ansible.builtin.service_facts:
|
|
|
|
- name: Read the restart count after the ACME failure
|
|
ansible.builtin.systemd:
|
|
name: traefik
|
|
register: traefik_unit_after_acme
|
|
|
|
- name: The pinned version is installed
|
|
ansible.builtin.assert:
|
|
that:
|
|
- traefik_version_out.stdout is search('(?m)^Version:')
|
|
- traefik_version | regex_replace('^v', '') in traefik_version_out.stdout
|
|
quiet: true
|
|
|
|
- name: Stat the archive leftovers
|
|
ansible.builtin.stat:
|
|
path: "{{ traefik_verify_leftover }}"
|
|
loop:
|
|
- /usr/local/bin/CHANGELOG.md
|
|
- /usr/local/bin/LICENSE.md
|
|
- /tmp/traefik.tar.gz
|
|
- /tmp/traefik_checksums.txt
|
|
loop_control:
|
|
loop_var: traefik_verify_leftover
|
|
register: traefik_leftovers
|
|
|
|
# Only the binary is extracted, and the archive and checksum file are removed.
|
|
- name: The install left nothing but the binary behind
|
|
ansible.builtin.assert:
|
|
that: traefik_leftovers.results | selectattr('stat.exists') | list | length == 0
|
|
fail_msg: >-
|
|
Left behind: {{ traefik_leftovers.results | selectattr('stat.exists') | map(attribute='traefik_verify_leftover') | list }}
|
|
quiet: true
|
|
|
|
- name: Secrets are root-only and the config belongs to the traefik user
|
|
ansible.builtin.assert:
|
|
that:
|
|
- (traefik_files.results | selectattr('traefik_verify_file', 'eq', '/etc/traefik/traefik.env') | first).stat.mode == '0600'
|
|
- (traefik_files.results | selectattr('traefik_verify_file', 'eq', '/etc/traefik/traefik.env') | first).stat.pw_name == 'root'
|
|
- (traefik_files.results | selectattr('traefik_verify_file', 'eq', '/etc/traefik/dynamic/dynamic.yml') | first).stat.mode == '0600'
|
|
- (traefik_files.results | selectattr('traefik_verify_file', 'eq', '/etc/traefik/dynamic/dynamic.yml') | first).stat.pw_name == 'traefik'
|
|
- (traefik_files.results | selectattr('traefik_verify_file', 'eq', '/etc/traefik/traefik.yml') | first).stat.mode == '0600'
|
|
- (traefik_files.results | selectattr('traefik_verify_file', 'eq', '/usr/local/bin/traefik') | first).stat.mode == '0755'
|
|
quiet: true
|
|
|
|
- name: Read the logrotate configuration
|
|
ansible.builtin.slurp:
|
|
src: /etc/logrotate.d/traefik
|
|
register: traefik_logrotate
|
|
|
|
- name: Let logrotate parse its configuration (debug mode changes nothing)
|
|
ansible.builtin.command: logrotate --debug /etc/logrotate.d/traefik
|
|
register: traefik_logrotate_debug
|
|
changed_when: false
|
|
|
|
- name: The Traefik logs are rotated by copying and truncating, not by signal
|
|
ansible.builtin.assert:
|
|
that:
|
|
- "'/etc/traefik/logs/*.log {' in traefik_logrotate.content | b64decode"
|
|
- "'copytruncate' in traefik_logrotate.content | b64decode"
|
|
- "'rotate 14' in traefik_logrotate.content | b64decode"
|
|
- "'su traefik traefik' in traefik_logrotate.content | b64decode"
|
|
- traefik_logrotate_debug.rc == 0
|
|
quiet: true
|
|
|
|
- name: Read the static configuration
|
|
ansible.builtin.slurp:
|
|
src: /etc/traefik/traefik.yml
|
|
register: traefik_static
|
|
|
|
- name: The ACME resolvers render when set and are absent when empty
|
|
ansible.builtin.assert:
|
|
that:
|
|
- "'resolvers:' in traefik_static_render"
|
|
- "'- \"192.0.2.53:53\"' in traefik_static_render"
|
|
- "'resolvers:' not in traefik_empty_render"
|
|
- "'dnsChallenge:' in traefik_empty_render"
|
|
quiet: true
|
|
vars:
|
|
traefik_static_render: "{{ traefik_static.content | b64decode }}"
|
|
traefik_empty_render: "{{ lookup('ansible.builtin.template', playbook_dir ~ '/../../templates/traefik.yml.j2') }}"
|
|
traefik_dns_challenge_resolvers: []
|
|
traefik_cloudflare_email: [email protected]
|
|
|
|
- name: The dashboard is off by default and the extra app is still routed
|
|
ansible.builtin.assert:
|
|
that:
|
|
- "'Host(`traefik.example.test`)' not in traefik_dyn_render"
|
|
- "'api@internal' not in traefik_dyn_render"
|
|
- "'dashboard-auth' not in traefik_dyn_render"
|
|
- "'Host(`whoami.example.test`)' in traefik_dyn_render"
|
|
- "'http://127.0.0.1:8081' in traefik_dyn_render"
|
|
- "'dashboard: false' in traefik_static.content | b64decode"
|
|
quiet: true
|
|
vars:
|
|
traefik_dyn_render: "{{ traefik_dynamic.content | b64decode }}"
|
|
|
|
- name: An enabled dashboard renders behind basicAuth
|
|
ansible.builtin.assert:
|
|
that:
|
|
- "'dashboard: true' in traefik_on_static"
|
|
- "'Host(`traefik.example.test`)' in traefik_on_dynamic"
|
|
- "'api@internal' in traefik_on_dynamic"
|
|
- "'dashboard-auth@file' in traefik_on_dynamic"
|
|
- "'admin:hash' in traefik_on_dynamic"
|
|
quiet: true
|
|
vars:
|
|
traefik_dashboard_enabled: true
|
|
traefik_dashboard_basicauth_users: ["admin:hash"]
|
|
traefik_on_static: "{{ lookup('ansible.builtin.template', playbook_dir ~ '/../../templates/traefik.yml.j2') }}"
|
|
traefik_on_dynamic: "{{ lookup('ansible.builtin.template', playbook_dir ~ '/../../templates/dynamic.yml.j2') }}"
|
|
|
|
- name: The service stays running with the role's unit settings
|
|
ansible.builtin.assert:
|
|
that:
|
|
- ansible_facts.services['traefik.service'].state == 'running'
|
|
- ansible_facts.services['traefik.service'].status == 'enabled'
|
|
# Restart=always would hide a crash on the ACME failure from the state above.
|
|
- traefik_unit_after_acme.status.NRestarts == '0'
|
|
- traefik_unit.status.NoNewPrivileges == 'yes'
|
|
- traefik_unit.status.MemoryDenyWriteExecute == 'yes'
|
|
- "'cap_net_bind_service' in traefik_unit.status.AmbientCapabilities | lower"
|
|
# the molecule drop-in took effect, so the unit was started without the sandbox
|
|
- traefik_unit.status.ProtectSystem == 'no'
|
|
quiet: true
|
|
|
|
- name: Traefik listens on 80 and 443 and redirects http to https
|
|
ansible.builtin.assert:
|
|
that:
|
|
- traefik_sockets.stdout is search(':80\s')
|
|
- traefik_sockets.stdout is search(':443\s')
|
|
- traefik_http.status in [301, 302, 307, 308]
|
|
quiet: true
|
|
|
|
- name: Ufw opens 80 and 443 next to SSH
|
|
ansible.builtin.assert:
|
|
that:
|
|
- traefik_ufw.stdout is search('(?m)^22/tcp\s+ALLOW')
|
|
- traefik_ufw.stdout is search('(?m)^80\s+ALLOW')
|
|
- traefik_ufw.stdout is search('(?m)^443\s+ALLOW')
|
|
quiet: true
|
|
|
|
- name: The traefik rules carry the traefik owner tag, in order
|
|
ansible.builtin.assert:
|
|
that:
|
|
- traefik_ufw_tagged | length == 2
|
|
- 'traefik_ufw_tagged[0] is search("80 comment .ansible-ufw\[traefik:[0-9a-f]{10}\]: Traefik http")'
|
|
- 'traefik_ufw_tagged[1] is search("443 comment .ansible-ufw\[traefik:[0-9a-f]{10}\]: Traefik https")'
|
|
- traefik_ufw_tagged[0] is search('\[traefik:' ~ traefik_ufw_key_80 ~ '\]')
|
|
- traefik_ufw_tagged[1] is search('\[traefik:' ~ traefik_ufw_key_443 ~ '\]')
|
|
- traefik_ufw_key_80 == '56ae54fb00'
|
|
- traefik_ufw_key_443 == '6d1cd18d60'
|
|
quiet: true
|
|
vars:
|
|
# sha1 of rule|port|proto|from|to|interface|direction, first ten hex characters
|
|
traefik_ufw_key_80: "{{ ('allow|80|||||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 }}"
|
|
|
|
# A version bump through the update path and mortis.kamino._binary_update: down to another
|
|
# release and back to the role default, both with traefik_update, from prepare.yml's stand-ins.
|
|
# After the idempotence step, because each run changes the binary, and before the remove path.
|
|
# 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
|
|
hosts: all
|
|
gather_facts: false
|
|
vars_files:
|
|
- ../../defaults/main.yml
|
|
tasks:
|
|
- name: The other release differs from the role default
|
|
ansible.builtin.assert:
|
|
that: traefik_verify_other_version != traefik_version
|
|
fail_msg: Move traefik_verify_other_version off the role default ({{ traefik_version }}).
|
|
quiet: true
|
|
|
|
- name: Update to the other release, then back to the role default
|
|
ansible.builtin.include_tasks: tasks/update_and_check.yml
|
|
loop:
|
|
- "{{ 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
|
|
# once, outside the idempotence step (a converge play that changed state would fail that step).
|
|
# Nothing runs after verify except molecule's own cleanup and destroy of the container.
|
|
# Assumptions: the plays above left the two tagged traefik rules in place, ufw active and the
|
|
# unit installed; traefik_remove_data stays false, so the configuration directory is not asserted.
|
|
# With traefik_remove set, the role skips prereqs, install, configure and update, so a reinstall
|
|
# or re-render would show as the binary or the unit coming back.
|
|
- name: Verify the remove path
|
|
hosts: all
|
|
gather_facts: false
|
|
vars_files:
|
|
- ../../defaults/main.yml
|
|
tasks:
|
|
- name: Seed a rule without an owner tag, which the removal must keep
|
|
ansible.builtin.command: ufw allow 8123/tcp comment 'molecule manual rule'
|
|
changed_when: true
|
|
|
|
- name: Read the rules ufw has stored before the removal
|
|
ansible.builtin.command: ufw show added
|
|
register: traefik_rm_before
|
|
changed_when: false
|
|
environment: {LC_ALL: C}
|
|
|
|
- name: The traefik rules exist before the removal
|
|
ansible.builtin.assert:
|
|
that: traefik_rm_before.stdout_lines | select('search', 'ansible-ufw\[traefik:') | list | length == 2
|
|
quiet: true
|
|
|
|
- name: Run the role with the remove flag
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.traefik
|
|
vars:
|
|
traefik_remove: true
|
|
|
|
- name: Read the rules ufw has stored after the removal
|
|
ansible.builtin.command: ufw show added
|
|
register: traefik_rm_after
|
|
changed_when: false
|
|
environment: {LC_ALL: C}
|
|
|
|
- name: Stat the unit and the binary the removal deletes
|
|
ansible.builtin.stat:
|
|
path: "{{ traefik_rm_path }}"
|
|
loop:
|
|
- "{{ traefik_service_unit }}"
|
|
- "{{ traefik_bin_path }}"
|
|
loop_control:
|
|
loop_var: traefik_rm_path
|
|
register: traefik_rm_files
|
|
|
|
- name: Ask systemd whether the service is still active
|
|
ansible.builtin.systemd:
|
|
name: traefik
|
|
register: traefik_rm_active
|
|
failed_when: false
|
|
|
|
- name: Only the traefik rules are gone, and nothing was installed again
|
|
ansible.builtin.assert:
|
|
that:
|
|
- traefik_rm_after.stdout_lines | select('search', 'ansible-ufw\[traefik:') | list | length == 0
|
|
- traefik_rm_after.stdout_lines | select('search', '(^|[ ])22/tcp') | list | length > 0
|
|
- traefik_rm_after.stdout_lines | select('search', '8123/tcp') | list | length == 1
|
|
- traefik_rm_files.results | map(attribute='stat') | map(attribute='exists') | select | list | length == 0
|
|
# the module errors for a unit that no longer exists, which is also a pass
|
|
- (traefik_rm_active.status | default({})).ActiveState | default('inactive') != 'active'
|
|
quiet: true
|