Files
Kamino/roles/traefik
Defame1297 c19c7abb20
Build / build (pull_request) Successful in 23s
Lint / lint (pull_request) Successful in 1m16s
Molecule / molecule (pull_request) Successful in 11m47s
Merge branch 'main' into ci/molecule-parallel-scenarios
2026-11-23 06:51:55 +00:00
..
2026-11-20 19:20:37 +00:00
2026-11-20 19:20:37 +00:00
2026-11-20 19:20:37 +00:00

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, which is the source of truth.

Supported platforms

Ubuntu 24.04 (noble), the image 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.

Requirements and run order

  • Use it by its fully qualified name, mortis.kamino.traefik.
  • 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

- 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.

Notes

  • 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. The molecule 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.
  • 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.
  • Molecule's verify also 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.
  • Molecule's second play expects the failure message containing unauthenticated Traefik dashboard (the dashboard guard: enabled without valid user:hash users); its third play expects collides with the middleware, raised by the dashboard-auth collision assert, which runs whenever users are set, also with the dashboard off.