feat(traefik): add the traefik role
Moves the traefik role from the environment repository, the first wave 2 role and the first caller of the internal _binary_update role. Against the source: - The update path stages the downloaded, checksum-verified binary in a directory next to the live one and hands the stop, backup, swap, restart, version check and rollback to mortis.kamino._binary_update (include_role with apply tags, so --tags update reaches it). A download or checksum failure still leaves the running service untouched and says so; a failure in the swap keeps the rollback message of _binary_update. - Two small behaviour changes come with _binary_update: the backup is taken whenever a live binary exists (the source skipped it when the binary reported no version), and a failure before the stop no longer restarts the running service. - The legacy name guards (pre-rename variables of the environment) are dropped, as in the wave 1 roles, and the argument_specs and README no longer mention them. - Environment references are gone from comments, the README and the argument_specs: issue numbers, host names, inventory paths, playbooks and the role's own history. The Renovate comment on traefik_version is dropped, since this repository has no manager for it; the README and argument_specs recommend pinning the version in inventory. - meta namespace mortis; the scenario follows the Kamino conventions (header, test_sequence, no provisioner env) and calls the role by FQCN. - The scenario's verify gains a real version bump: down to another release (v3.7.18) and back to the role default with traefik_update, asserting the version, an active unit and no staging directory, backup or download left behind. Changelog fragment, README Roadmap and the CLAUDE.md role list updated. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
This commit is contained in:
1 parent
82de44dade
commit
589b71a312
23 files changed
+1624
-3
No files matched your search
@@ -0,0 +1,212 @@
|
||||
---
|
||||
# Fail closed: an enabled dashboard without credentials would be an open admin UI.
|
||||
# Before the first template, so a refused run changes no file.
|
||||
- name: Refuse an enabled dashboard without basicAuth users
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- >-
|
||||
not (traefik_dashboard_enabled | bool)
|
||||
or (traefik_dashboard_basicauth_users | default([]) | length > 0
|
||||
and traefik_dashboard_basicauth_users | map('string') | select('match', '^[^:\s]+:\S+$') | list | length
|
||||
== traefik_dashboard_basicauth_users | length)
|
||||
fail_msg: >-
|
||||
traefik_dashboard_enabled is true but traefik_dashboard_basicauth_users is empty or holds an entry that is
|
||||
not a 'user:hash' htpasswd line, which would deploy an unauthenticated Traefik dashboard. Set vaulted
|
||||
htpasswd users, or leave the dashboard disabled (the default).
|
||||
quiet: true
|
||||
# health too: the dynamic.yml render below carries it, and a guard that a tag skips guards nothing.
|
||||
tags: [traefik, configure, health]
|
||||
|
||||
- name: Render Traefik static configuration
|
||||
become: true
|
||||
ansible.builtin.template:
|
||||
src: traefik.yml.j2
|
||||
dest: "{{ traefik_config_dir }}/traefik.yml"
|
||||
owner: "{{ traefik_user }}"
|
||||
group: "{{ traefik_user }}"
|
||||
mode: "0600"
|
||||
backup: true
|
||||
notify: Restart Traefik
|
||||
tags: [traefik, configure]
|
||||
|
||||
- name: Fail if dashboard-auth collides with a user-defined dynamic middleware
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- "'dashboard-auth' not in (traefik_dynamic_middlewares | default({}, true))"
|
||||
fail_msg: >-
|
||||
traefik_dynamic_middlewares defines a 'dashboard-auth' key, which
|
||||
collides with the middleware traefik_dashboard_basicauth_users
|
||||
renders under the same name. Rename the dynamic middleware.
|
||||
when: traefik_dashboard_basicauth_users | default([]) | length > 0
|
||||
tags: [traefik, configure]
|
||||
|
||||
# no_log unconditionally: with dashboard basicAuth on, this file holds
|
||||
# htpasswd hashes, and a --diff run prints the old file body too - so the
|
||||
# run that disables auth would leak the removed hashes if no_log depended on
|
||||
# the new value. Tradeoff: --diff never shows changes to dynamic.yml.
|
||||
# No `backup: true`, same as traefik.env below: every backup would be
|
||||
# another root-owned copy of the hashes. A `users | length == 0` condition
|
||||
# is not enough, as the disabling run would back up the hash-bearing file.
|
||||
- name: Render Traefik dynamic configuration
|
||||
become: true
|
||||
ansible.builtin.template:
|
||||
src: dynamic.yml.j2
|
||||
dest: "{{ traefik_dynamic_dir }}/dynamic.yml"
|
||||
owner: "{{ traefik_user }}"
|
||||
group: "{{ traefik_user }}"
|
||||
mode: "0600"
|
||||
backup: false
|
||||
# no_log hides the result; diff: false keeps the rendered hashes out of --diff.
|
||||
diff: false
|
||||
no_log: true
|
||||
notify: Restart Traefik
|
||||
tags: [traefik, configure, health]
|
||||
|
||||
# Clean up copies left by the `backup: true` this task used to carry.
|
||||
# Ansible names them <file>.<pid>.<timestamp>~.
|
||||
- name: Find stale Traefik dynamic config backups
|
||||
become: true
|
||||
ansible.builtin.find:
|
||||
paths: "{{ traefik_dynamic_dir }}"
|
||||
patterns: "dynamic.yml.*~"
|
||||
register: traefik_dynamic_backups
|
||||
tags: [traefik, configure]
|
||||
|
||||
- name: Remove stale Traefik dynamic config backups
|
||||
become: true
|
||||
ansible.builtin.file:
|
||||
path: "{{ traefik_stale_backup.path }}"
|
||||
state: absent
|
||||
loop: "{{ traefik_dynamic_backups.files }}"
|
||||
loop_control:
|
||||
loop_var: traefik_stale_backup
|
||||
label: "{{ traefik_stale_backup.path }}"
|
||||
tags: [traefik, configure]
|
||||
|
||||
# no_log: this file holds the Cloudflare API key, and without it a --diff
|
||||
# run prints the old and new file bodies to the console (and CI logs).
|
||||
# No `backup: true` either, unlike the config templates above: each backup
|
||||
# is another root-owned copy of the key, and a rotated key would live on in
|
||||
# them indefinitely.
|
||||
# Notifies a restart because Traefik only reads the file at start - a
|
||||
# rotated key would otherwise sit unused until an unrelated restart.
|
||||
- name: Create environment file for Traefik secrets
|
||||
become: true
|
||||
ansible.builtin.copy:
|
||||
dest: "{{ traefik_config_dir }}/traefik.env"
|
||||
content: |
|
||||
CF_API_EMAIL={{ traefik_cloudflare_email }}
|
||||
CF_API_KEY={{ traefik_cloudflare_api_token }}
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0600"
|
||||
no_log: true
|
||||
diff: false
|
||||
notify: Restart Traefik
|
||||
tags: [traefik, configure]
|
||||
|
||||
# Clean up copies left by the `backup: true` this task used to carry.
|
||||
# Ansible names them <file>.<pid>.<timestamp>~.
|
||||
- name: Find stale Traefik secret env backups
|
||||
become: true
|
||||
ansible.builtin.find:
|
||||
paths: "{{ traefik_config_dir }}"
|
||||
patterns: "traefik.env.*~"
|
||||
register: traefik_env_backups
|
||||
tags: [traefik, configure]
|
||||
|
||||
- name: Remove stale Traefik secret env backups
|
||||
become: true
|
||||
ansible.builtin.file:
|
||||
path: "{{ traefik_stale_backup.path }}"
|
||||
state: absent
|
||||
loop: "{{ traefik_env_backups.files }}"
|
||||
loop_control:
|
||||
loop_var: traefik_stale_backup
|
||||
label: "{{ traefik_stale_backup.path }}"
|
||||
tags: [traefik, configure]
|
||||
|
||||
- name: Rotate the Traefik logs
|
||||
become: true
|
||||
ansible.builtin.template:
|
||||
src: logrotate.traefik.j2
|
||||
dest: /etc/logrotate.d/traefik
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0644"
|
||||
tags: [traefik, configure]
|
||||
|
||||
- name: Create systemd service file for Traefik
|
||||
become: true
|
||||
ansible.builtin.copy:
|
||||
dest: "{{ traefik_service_unit }}"
|
||||
content: |
|
||||
[Unit]
|
||||
Description=Traefik
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
User={{ traefik_user }}
|
||||
Group={{ traefik_user }}
|
||||
EnvironmentFile={{ traefik_config_dir }}/traefik.env
|
||||
ExecStart={{ traefik_bin_path }} --configFile={{ traefik_config_dir }}/traefik.yml
|
||||
WorkingDirectory={{ traefik_config_dir }}
|
||||
Restart=always
|
||||
AmbientCapabilities=CAP_NET_BIND_SERVICE
|
||||
NoNewPrivileges=true
|
||||
ProtectSystem=full
|
||||
ProtectHome=true
|
||||
PrivateTmp=true
|
||||
ReadWritePaths={{ traefik_config_dir }} {{ traefik_dynamic_dir }} {{ traefik_acme_file }} {{ traefik_config_dir }}/logs
|
||||
PrivateDevices=true
|
||||
ProtectKernelModules=true
|
||||
ProtectKernelTunables=true
|
||||
MemoryDenyWriteExecute=true
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0644"
|
||||
notify:
|
||||
- Reload Systemd
|
||||
- Restart Traefik
|
||||
tags: [traefik, configure]
|
||||
|
||||
- name: Reload systemd daemon
|
||||
become: true
|
||||
ansible.builtin.systemd:
|
||||
daemon_reload: true
|
||||
tags: [traefik, configure]
|
||||
|
||||
- name: Trigger UFW after installing Traefik
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.ufw
|
||||
tasks_from: service_rules
|
||||
apply:
|
||||
tags: [traefik, configure, ufw]
|
||||
vars:
|
||||
ufw_service_name: traefik
|
||||
ufw_service_rules: "{{ traefik_ufw_service_rules }}"
|
||||
tags: [traefik, configure, ufw]
|
||||
|
||||
- name: Enable and start Traefik service
|
||||
become: true
|
||||
ansible.builtin.systemd:
|
||||
name: traefik
|
||||
enabled: true
|
||||
state: started
|
||||
tags: [traefik, configure]
|
||||
|
||||
- name: Check Traefik service status
|
||||
become: true
|
||||
ansible.builtin.systemd:
|
||||
name: traefik
|
||||
register: traefik_service_status
|
||||
tags: [traefik, configure, health]
|
||||
|
||||
- name: Fail if Traefik service is not running
|
||||
ansible.builtin.fail:
|
||||
msg: "Traefik service is not running"
|
||||
when: traefik_service_status.status.ActiveState != "active"
|
||||
tags: [traefik, configure, health]
|
||||
@@ -0,0 +1,69 @@
|
||||
---
|
||||
# The one download of the Traefik release, shared by install.yml (extracts straight into the
|
||||
# live binary's directory, since nothing runs yet) and update.yml (extracts into a staging
|
||||
# directory next to it, before the service is stopped). Only the traefik binary is extracted:
|
||||
# the archive also holds CHANGELOG.md and LICENSE.md, which do not belong in a bin directory.
|
||||
# Input: traefik_download_dir, the directory the binary is extracted into.
|
||||
# Tagged install and update: an include does not pass its tags on to the tasks it includes,
|
||||
# so under --tags update a task tagged install only would be skipped.
|
||||
- name: Download, verify and extract the Traefik binary
|
||||
block:
|
||||
# force: an archive left in /tmp by an earlier run (an interrupted run, or another tool)
|
||||
# would otherwise be kept and then fail the checksum check.
|
||||
- name: Download Traefik archive
|
||||
ansible.builtin.get_url:
|
||||
url: "https://github.com/traefik/traefik/releases/download/{{ traefik_version }}/{{ traefik_archive_name }}"
|
||||
dest: "{{ traefik_local_archive }}"
|
||||
mode: "0644"
|
||||
force: true
|
||||
tags: [traefik, install, binary, update]
|
||||
|
||||
- name: Download Traefik checksums
|
||||
ansible.builtin.get_url:
|
||||
url: "https://github.com/traefik/traefik/releases/download/{{ traefik_version }}/traefik_{{ traefik_version }}_checksums.txt"
|
||||
dest: /tmp/traefik_checksums.txt
|
||||
mode: "0644"
|
||||
force: true
|
||||
tags: [traefik, install, binary, update]
|
||||
|
||||
- name: Compute SHA256 of Traefik archive
|
||||
ansible.builtin.command: sha256sum {{ traefik_local_archive }}
|
||||
# The archive is not downloaded under --check, so there is nothing to hash, verify or unpack.
|
||||
when: not ansible_check_mode
|
||||
register: traefik_sha256
|
||||
changed_when: false
|
||||
failed_when: traefik_sha256.stdout is not match("^[a-f0-9]{64} ")
|
||||
tags: [traefik, install, binary, update]
|
||||
|
||||
- name: Verify Traefik checksum against official list
|
||||
ansible.builtin.shell: |
|
||||
grep -F "{{ traefik_sha256.stdout.split()[0] }} {{ traefik_archive_name }}" /tmp/traefik_checksums.txt
|
||||
when: not ansible_check_mode
|
||||
register: traefik_checksum_verify
|
||||
changed_when: false
|
||||
failed_when: traefik_checksum_verify.rc != 0
|
||||
tags: [traefik, install, binary, update]
|
||||
|
||||
- name: Extract the Traefik binary
|
||||
become: true
|
||||
ansible.builtin.unarchive:
|
||||
src: "{{ traefik_local_archive }}"
|
||||
dest: "{{ traefik_download_dir }}"
|
||||
remote_src: true
|
||||
include: [traefik]
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0755"
|
||||
when: not ansible_check_mode
|
||||
tags: [traefik, install, binary, update]
|
||||
always:
|
||||
- name: Remove the downloaded Traefik archive and checksums
|
||||
ansible.builtin.file:
|
||||
path: "{{ traefik_download_file }}"
|
||||
state: absent
|
||||
loop:
|
||||
- "{{ traefik_local_archive }}"
|
||||
- /tmp/traefik_checksums.txt
|
||||
loop_control:
|
||||
loop_var: traefik_download_file
|
||||
tags: [traefik, install, binary, update]
|
||||
@@ -0,0 +1,52 @@
|
||||
---
|
||||
- name: Create Traefik directories
|
||||
become: true
|
||||
ansible.builtin.file:
|
||||
path: "{{ traefik_dir }}"
|
||||
state: directory
|
||||
owner: "{{ traefik_user }}"
|
||||
group: "{{ traefik_user }}"
|
||||
mode: "0700" # "0755"
|
||||
loop:
|
||||
- "{{ traefik_config_dir }}"
|
||||
- "{{ traefik_dynamic_dir }}"
|
||||
- "{{ traefik_config_dir }}/logs"
|
||||
loop_control:
|
||||
loop_var: traefik_dir
|
||||
tags: [traefik, install, binary, update]
|
||||
|
||||
- name: Ensure ACME file exists (without overwriting)
|
||||
become: true
|
||||
ansible.builtin.command:
|
||||
cmd: touch {{ traefik_acme_file }}
|
||||
args:
|
||||
creates: "{{ traefik_acme_file }}"
|
||||
tags: [traefik, install, binary, update]
|
||||
|
||||
- name: Ensure correct permissions on ACME file
|
||||
become: true
|
||||
ansible.builtin.file:
|
||||
path: "{{ traefik_acme_file }}"
|
||||
owner: "{{ traefik_user }}"
|
||||
group: "{{ traefik_user }}"
|
||||
mode: "0600"
|
||||
tags: [traefik, install, binary, update]
|
||||
|
||||
- name: Install the Traefik binary
|
||||
ansible.builtin.include_tasks: download.yml
|
||||
vars:
|
||||
traefik_download_dir: "{{ traefik_bin_path | dirname }}"
|
||||
tags: [traefik, install, binary]
|
||||
|
||||
- name: Ensure Traefik binary exists
|
||||
ansible.builtin.stat:
|
||||
path: "{{ traefik_bin_path }}"
|
||||
register: traefik_binary
|
||||
tags: [traefik, install, binary, health, update]
|
||||
|
||||
- name: Fail if Traefik binary missing
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- traefik_binary.stat.exists
|
||||
fail_msg: "Traefik binary not found after extraction"
|
||||
tags: [traefik, install, binary, health, update]
|
||||
@@ -0,0 +1,43 @@
|
||||
---
|
||||
- name: Install common packages
|
||||
become: true
|
||||
ansible.builtin.apt:
|
||||
name:
|
||||
- wget
|
||||
- tar
|
||||
- ca-certificates
|
||||
- curl
|
||||
- gnupg
|
||||
- logrotate
|
||||
state: present
|
||||
update_cache: true
|
||||
tags: [traefik, prereqs, always]
|
||||
|
||||
- name: Create Traefik group
|
||||
become: true
|
||||
ansible.builtin.group:
|
||||
name: "{{ traefik_user }}"
|
||||
system: true
|
||||
tags: [traefik, prereqs, always]
|
||||
|
||||
- name: Create Traefik user
|
||||
become: true
|
||||
ansible.builtin.user:
|
||||
name: "{{ traefik_user }}"
|
||||
system: true
|
||||
shell: /usr/sbin/nologin
|
||||
comment: "Traefik Reverse Proxy"
|
||||
create_home: false
|
||||
group: "{{ traefik_user }}"
|
||||
tags: [traefik, prereqs, always]
|
||||
|
||||
- name: Check if Traefik binary exists
|
||||
ansible.builtin.stat:
|
||||
path: "{{ traefik_bin_path }}"
|
||||
register: traefik_binary
|
||||
tags: [traefik, prereqs, always]
|
||||
|
||||
- name: Set Traefik facts
|
||||
ansible.builtin.set_fact:
|
||||
traefik_installed: "{{ traefik_binary.stat.exists }}"
|
||||
tags: [traefik, prereqs, always]
|
||||
@@ -0,0 +1,51 @@
|
||||
---
|
||||
# Run with -e "traefik_remove=true" (and traefik_remove_data=true to delete the configuration too).
|
||||
- name: Stop Traefik service
|
||||
become: true
|
||||
ansible.builtin.systemd:
|
||||
name: traefik
|
||||
state: stopped
|
||||
enabled: false
|
||||
tags: [traefik, remove]
|
||||
|
||||
- name: Remove systemd service
|
||||
become: true
|
||||
ansible.builtin.file:
|
||||
path: "{{ traefik_service_unit }}"
|
||||
state: absent
|
||||
tags: [traefik, remove]
|
||||
|
||||
- name: Reload systemd
|
||||
become: true
|
||||
ansible.builtin.systemd:
|
||||
daemon_reload: true
|
||||
tags: [traefik, remove]
|
||||
|
||||
- name: Remove Traefik binary
|
||||
become: true
|
||||
ansible.builtin.file:
|
||||
path: "{{ traefik_bin_path }}"
|
||||
state: absent
|
||||
tags: [traefik, remove]
|
||||
|
||||
- name: Remove Traefik configuration
|
||||
become: true
|
||||
ansible.builtin.file:
|
||||
path: "{{ traefik_config_dir }}"
|
||||
state: absent
|
||||
recurse: true
|
||||
when: traefik_remove_data | default(false) | bool
|
||||
tags: [traefik, remove]
|
||||
|
||||
# State absent deletes every rule this owner tagged, regardless of ufw_purge_stale, and keeps
|
||||
# the lines that may carry SSH. Runs last: the service is already stopped and gone.
|
||||
- name: Remove the Traefik UFW rules
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.ufw
|
||||
tasks_from: service_rules
|
||||
apply:
|
||||
tags: [traefik, ufw, remove]
|
||||
vars:
|
||||
ufw_service_name: traefik
|
||||
ufw_service_state: absent
|
||||
tags: [traefik, ufw, remove]
|
||||
@@ -0,0 +1,86 @@
|
||||
---
|
||||
- name: Check current Traefik version
|
||||
ansible.builtin.command: "{{ traefik_bin_path }} version"
|
||||
register: traefik_version_cmd
|
||||
changed_when: false
|
||||
# Failure is expected before the first install (binary missing): empty stdout falls through
|
||||
# to the default version below ("v0.0.0"), which differs from traefik_version and so triggers
|
||||
# the update path.
|
||||
failed_when: false
|
||||
# Read-only, so it also runs in the --check preview. Skipped there, stdout would be empty, the version
|
||||
# would fall back to 0.0.0 and an up-to-date binary would look like it needs an install.
|
||||
check_mode: false
|
||||
tags: [traefik, update]
|
||||
|
||||
- name: Extract installed Traefik version
|
||||
ansible.builtin.set_fact:
|
||||
traefik_installed_version: >-
|
||||
{{
|
||||
'v' + (traefik_version_cmd.stdout
|
||||
| regex_findall('v?([0-9]+\.[0-9]+\.[0-9]+)')
|
||||
| first
|
||||
| default('0.0.0'))
|
||||
}}
|
||||
tags: [traefik, update]
|
||||
|
||||
- name: Determine if Traefik update required
|
||||
ansible.builtin.set_fact:
|
||||
traefik_update_required: "{{ traefik_installed_version != traefik_version }}"
|
||||
# Next to the live binary, so the swap is a rename on one filesystem.
|
||||
traefik_staging_dir: "{{ traefik_bin_path | dirname }}/.traefik-staging"
|
||||
tags: [traefik, update]
|
||||
|
||||
# Download and verify before anything is stopped: a failed download or checksum leaves the
|
||||
# running Traefik untouched. mortis.kamino._binary_update then stops, backs up, swaps, restarts,
|
||||
# checks the version and rolls back on failure, and removes the staged binary.
|
||||
- name: Stage and swap in the new Traefik binary
|
||||
when: traefik_update_required
|
||||
block:
|
||||
- name: Create the Traefik staging directory
|
||||
become: true
|
||||
ansible.builtin.file:
|
||||
path: "{{ traefik_staging_dir }}"
|
||||
state: directory
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0700"
|
||||
tags: [traefik, update]
|
||||
|
||||
# Its own rescue, so that only a staging failure gets this message: a failure in the swap
|
||||
# below keeps the rollback message of mortis.kamino._binary_update.
|
||||
- name: Stage the new Traefik binary
|
||||
block:
|
||||
- name: Download and verify into the staging directory
|
||||
ansible.builtin.include_tasks: download.yml
|
||||
vars:
|
||||
traefik_download_dir: "{{ traefik_staging_dir }}"
|
||||
tags: [traefik, update]
|
||||
rescue:
|
||||
- name: Fail the staging
|
||||
ansible.builtin.fail:
|
||||
msg: >-
|
||||
Staging Traefik {{ traefik_version }} failed ({{ ansible_failed_result.msg | default('see the task above') }}).
|
||||
Traefik was not stopped and still runs {{ traefik_installed_version }}.
|
||||
tags: [traefik, update]
|
||||
|
||||
# An include does not pass its tags on to the included tasks, hence apply.
|
||||
- name: Swap in the staged Traefik binary
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino._binary_update
|
||||
apply:
|
||||
tags: [traefik, update]
|
||||
vars:
|
||||
_binary_update_service: traefik
|
||||
_binary_update_binary_path: "{{ traefik_bin_path }}"
|
||||
_binary_update_staged_path: "{{ traefik_staging_dir }}/traefik"
|
||||
_binary_update_version_command: ["{{ traefik_bin_path }}", version]
|
||||
_binary_update_expected_version: "{{ traefik_version }}"
|
||||
tags: [traefik, update]
|
||||
|
||||
always:
|
||||
- name: Remove the Traefik staging directory
|
||||
become: true
|
||||
ansible.builtin.file:
|
||||
path: "{{ traefik_staging_dir }}"
|
||||
state: absent
|
||||
tags: [traefik, update]
|
||||
Reference in new issue
Block a user