Every role except network_interfaces runs its molecule scenarios on the 24.04 and 26.04 images and declares Ubuntu noble and resolute; jammy and Ubuntu all are dropped. trixie stays declared until the Debian 13 image (#226) tests it. docker and gitea stay on noble until the 26.04 image ships gnupg (#378). Closes #133 Assisted-by: Claude Code
104 lines
10 KiB
Markdown
104 lines
10 KiB
Markdown
# traefik
|
|
|
|
Installs the Traefik reverse proxy as a systemd-managed binary, renders its static and dynamic configuration with Cloudflare DNS-challenge certificates, opens its ports through the `mortis.kamino.ufw` role, and optionally updates or removes it (removal also deletes its ufw rules). 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 uses `ansible.builtin.apt`, so Debian-family hosts only, and needs outbound access to github.com for the release archive and checksum file. Only `traefik_install_type: binary` exists.
|
|
|
|
The role targets Ubuntu hosts that run the reverse proxy, not Proxmox nodes, so it does not cover Debian trixie.
|
|
|
|
## Requirements and run order
|
|
|
|
- Use it by its fully qualified name, `mortis.kamino.traefik`.
|
|
- Role dependencies: it includes `mortis.kamino.ufw` (`service_rules`) and `mortis.kamino._binary_update`, both of this Collection.
|
|
- Run the `system_baseline` and `ufw` roles of this Collection first. The configure tasks pass `traefik_ufw_service_rules` (ports 80 and 443 by default) to the `service_rules` entry point of `mortis.kamino.ufw`, owner `traefik`, which needs ufw installed and active and the SSH rule in place. See the README of `mortis.kamino.ufw`.
|
|
- Secrets: `traefik_cloudflare_email` and `traefik_cloudflare_api_token` have no role default and are required. Map them from vaulted variables in inventory; the role never holds the values itself.
|
|
- `traefik_domain` has no role default and must be set in inventory.
|
|
- Updates: the update path stages the verified binary and hands the swap to `mortis.kamino._binary_update`, an internal role of this Collection.
|
|
- Pin `traefik_version` in inventory to move Traefik independently of Collection releases; the role default changes only with a release.
|
|
|
|
## Role variables
|
|
|
|
| Variable | Default | Required | Description |
|
|
|---|---|---|---|
|
|
| `traefik_domain` | none | yes | Zone appended to the dashboard and app subdomains. |
|
|
| `traefik_update` | `false` | no | Run the update tasks when the installed version differs from `traefik_version`: download and verify into a staging directory before anything is stopped, then `mortis.kamino._binary_update` stops, backs up, swaps, restarts and checks the version, and rolls back on failure. |
|
|
| `traefik_remove` | `false` | no | Stop and disable the service, remove unit and binary, then delete the role's ufw rules (owner `traefik`, `ufw_service_state: absent`). The last remove task deletes the role's ufw rules, so ufw must be active on the host or that task fails. Prerequisites, install, configure and update are skipped. |
|
|
| `traefik_remove_data` | `false` | no | With `traefik_remove`, also delete `traefik_config_dir`. |
|
|
| `traefik_install_type` | `binary` | no | Task directory selector; only `binary` exists. |
|
|
| `traefik_version` | pinned in `defaults/main.yml` | no | Release tag to install. |
|
|
| `traefik_arch` | `amd64` | no | Archive architecture. |
|
|
| `traefik_os` | `linux` | no | Archive OS. |
|
|
| `traefik_archive_name` | `traefik_<version>_<os>_<arch>.tar.gz` | no | Archive file name (templated). |
|
|
| `traefik_local_archive` | `/tmp/traefik.tar.gz` | no | Download destination, removed after extraction. |
|
|
| `traefik_bin_path` | `/usr/local/bin/traefik` | no | Binary path used by the unit and version checks; only the `traefik` file of the archive is extracted into its directory, so the file name must stay `traefik`. Only the `traefik` file is extracted, so the archive's `CHANGELOG.md` and `LICENSE.md` never land there. |
|
|
| `traefik_config_dir` | `/etc/traefik` | no | Static config, `traefik.env` and logs directory. |
|
|
| `traefik_dynamic_dir` | `<traefik_config_dir>/dynamic` | no | File-provider directory. |
|
|
| `traefik_acme_file` | `<traefik_config_dir>/acme.json` | no | ACME storage, mode 0600. |
|
|
| `traefik_service_unit` | `/etc/systemd/system/traefik.service` | no | Systemd unit file path written (and removed on `traefik_remove`). |
|
|
| `traefik_dns_challenge_resolvers` | `[]` | no | `host:port` or `[ipv6]:port` resolvers for the ACME DNS-01 propagation check; empty omits the `resolvers` block. Set per environment. |
|
|
| `traefik_user` | `traefik` | no | System user and group running the service. |
|
|
| `traefik_log_rotate_frequency` | `daily` | no | How often logrotate rotates `<traefik_config_dir>/logs/*.log` (`/etc/logrotate.d/traefik`, `copytruncate`). |
|
|
| `traefik_log_rotate_count` | `14` | no | Rotated log files to keep. |
|
|
| `traefik_log_rotate_max_size` | `500M` | no | Size that rotates a log early when logrotate runs. |
|
|
| `traefik_dynamic_apps` | `[]` | no | Extra apps: `service_name`, `subdomain` (trailing dot), `server_url`, optional `pass_host_header`. |
|
|
| `traefik_dashboard_enabled` | `false` | no | Serve the dashboard. When `true`, `traefik_dashboard_basicauth_users` must be non-empty or the role fails. |
|
|
| `traefik_dashboard_subdomain` | `traefik.` | no | Dashboard host prefix. |
|
|
| `traefik_dashboard_basicauth_users` | `[]` | no | htpasswd lines for dashboard basicAuth (credentials, `no_log`); required when the dashboard is enabled, ignored otherwise. |
|
|
| `traefik_dynamic_middlewares` | `{}` | no | Extra middlewares; `dashboard-auth` is reserved. |
|
|
| `traefik_trusted_proxy_ips` | `127.0.0.1/32`, `::1/128` | no | CIDRs trusted for forwarded headers. |
|
|
| `traefik_cloudflare_email` | none | yes | Cloudflare e-mail (`no_log`, vaulted source). |
|
|
| `traefik_cloudflare_api_token` | none | yes | Cloudflare API key (`no_log`, vaulted source). |
|
|
| `traefik_headers` | HSTS and security headers, see defaults | no | `default-headers` middleware; overriding replaces the whole mapping. |
|
|
| `traefik_ufw_service_rules` | allow 80 and 443 | no | Rules passed to `ufw` `service_rules` (owner `traefik`). |
|
|
|
|
## Tags
|
|
|
|
`traefik`, `remove`, `install`, `configure` and `update` select their task files. `prereqs` tasks are also tagged `always`, so they run under any tag. The required-input assert (`traefik_domain` and the Cloudflare inputs) is tagged `always` and runs before the remove path, so `traefik_remove=true` also needs them defined.
|
|
|
|
- `ufw` and `binary` select nothing on their own: they sit only on tasks inside included files (`configure.yml`, `install.yml`) whose includes in `main.yml` do not carry them. The `ufw` include sets `apply: tags`, so the `service_rules` tasks also run under `--tags traefik` or `--tags configure`. The rule removal in `tasks/binary/remove.yml` also carries `ufw` and `remove`, but `main.yml` includes the remove path only under `traefik` and `remove`.
|
|
- `health` is not read-only: it re-renders `dynamic.yml` (which can notify `Restart Traefik`) and checks that the service is `active`.
|
|
|
|
## Example play
|
|
|
|
```yaml
|
|
- name: Install Traefik
|
|
hosts: proxy
|
|
roles:
|
|
- role: mortis.kamino.traefik
|
|
vars:
|
|
traefik_domain: example.com
|
|
traefik_cloudflare_email: [email protected]
|
|
traefik_cloudflare_api_token: "{{ vault_cloudflare_token }}"
|
|
traefik_dynamic_apps:
|
|
- {service_name: whoami, subdomain: "whoami.", server_url: "http://127.0.0.1:8081"}
|
|
```
|
|
|
|
Update or remove: pass `-e traefik_update=true`, or `-e "traefik_remove=true traefik_remove_data=true" --tags remove`.
|
|
|
|
## 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 downloads real Traefik releases from GitHub. Converge runs `mortis.kamino.ufw`, then the role. 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 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.
|
|
- 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 update path: it updates to another release and back with `traefik_update` (a real download, checksum check and swap) and asserts the version, an active unit and no leftovers.
|
|
- 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.
|
|
|
|
## Notes
|
|
|
|
- Idempotent: yes.
|
|
- 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.
|
|
- 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.
|
|
- The unit sets `NoNewPrivileges`, `ProtectSystem=full`, `ProtectHome`, `PrivateTmp`, `PrivateDevices`, `ProtectKernelModules`, `ProtectKernelTunables`, `MemoryDenyWriteExecute` and `CAP_NET_BIND_SERVICE`; see Tests for what CI covers of them.
|
|
- The role fails early (assert at the top of `tasks/main.yml`, runs under any tag and before the `remove` include) when `traefik_dns_challenge_resolvers` is not a list of `host:port` (or `[ipv6]:port`) strings, when `traefik_bin_path` is not an absolute path ending in `traefik`, or when `traefik_config_dir` or `traefik_service_unit` is not absolute.
|
|
- The DNS-challenge resolvers are environment-specific, so the role default is empty, which omits the `resolvers` block.
|
|
- The play fails when the service is not `active` after start.
|