Files
Kamino/roles/traefik/tasks/binary/configure.yml
T
Defame1297 cf2286b7a7 test(roles): run molecule's check step before and after converge
Every role scenario with a converge now runs check right before it (a
fresh host) and right after it (a configured host), and a script test
fails a scenario without both. pihole and pve_lxc move their second
check from after idempotence to after converge to match.

The roles that failed under --check on a fresh host get the two allowed
guards: cloudflared, docker, gitea, gitea_runner, traefik and
user_management. ufw and fail2ban preview only a host where their
package is installed, as their READMEs now say.

Closes #134

Assisted-by: Claude Code
2026-11-26 10:11:54 +00:00

217 lines
7.3 KiB
YAML

---
# 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
# Under --check on a first install the unit file is only simulated, so systemd would not find it.
when: not ansible_check_mode or traefik_installed | bool
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:
- not ansible_check_mode or traefik_installed | bool
- traefik_service_status.status.ActiveState != "active"
tags: [traefik, configure, health]