systemd¶
systemd Drop-ins and Service Ordering¶
- Override units with drop-ins under
/etc/systemd/system/<unit>.d/, never by templating the unit file. Name them after their purpose (z00-linuxfabrik.conffor the role's[Service]settings,z00-after-<dependency>.conffor ordering) and runsystemctl daemon-reloadwhen changed. - For a unit of another software, use
z00-<role>.conf(e.g.roles/librenmswritesrrdcached.service.d/z00-librenms.conf), so the role owning that software does not overwrite it. - If a role's
<role>__kernel_settings__*__dependent_varsets a value its service reads only at startup (e.g.net.core.somaxconn, applied inlisten()), add az00-after-tuned.confwith[Unit]After=tuned.service; otherwise the service may start before TuneD and keep the old value. Continuously honoured values (vm.swappiness,net.bridge.bridge-nf-call-iptables) need none; decide per parameter. - Put the ordering into the consuming unit, never a
Before=list into the dependency: it would need syncing per host and invites ordering cycles, which systemd breaks by silently dropping an edge. Ordering on a missing unit is ignored. - Ordering drop-ins trigger no restart; they take effect at the next boot.
roles/exampleimplements the chain (example__kernel_settings__sysctl__dependent_var, fed intokernel_settingsbyplaybooks/example.yml, plus the drop-in).roles/icinga2_agentshows a non-TuneD case (z00-after-sssd.conf).
Calendar Timers¶
systemd computes the next run of a calendar timer from its last run, not from now. A schedule moved to a point between the last run and now, for example from weekly to daily, fires as soon as the manager loads the new unit: an update lane would update in the middle of the day, a reboot timer would reboot. daemon-reload, restart and the reload that systemctl enable / disable do implicitly all keep the last run, and a Persistent=true timer reads it back from its stamp file below /var/lib/systemd/timers/ on every start. Upstream keeps this catch-up as the default (systemd#16732).
- Register every task that deploys a timer unit or a drop-in with
OnCalendar=, and includeshared/tasks/reload-changed-timers.ymlright after the role deployed its units, with the timers that changed (shared__reload_changed_timers,shared__reload_changed_timers_userfor a user's timers). It stops and disables them before reloading, so that the manager unloads them together with their last run, sets the stamp files of persistent timers to now, and restores their state afterwards. - Include it before the role's own
daemon-reloadtask, and never notify adaemon-reloadhandler for a timer instead: the handler runs at the end of the play, and anyenable/disablein between reloads the manager with the new schedule. - Unloading only works for a timer that no other loaded unit references. Do not add
Wants=/Requires=on a timer to another unit;WantedBy=timers.targetin its[Install]section is fine. roles/system_updateimplements this;extensions/molecule/shared/reload_changed_timerstests the behaviour.
Secrets for systemd Services¶
A secret that a systemd service reads from a file (a daemon, or a script run by a timer), or that a script needs which only admins run by hand, is stored as a systemd credential, neither in a config file below /etc nor templated into the script. Backups of /etc then hold no plaintext password, since the host key in /var/lib/systemd/ is not backed up.
- Store it with
linuxfabrik.lfops.systemd_credentialand register the result. The module encrypts the credential into/etc/credstore.encrypted/with systemd 251 and newer, and otherwise writes it in plain text to/etc/credstore/(0700directory,0600file, root). - In the unit, add
LoadCredentialEncrypted=<name>only when the registered result isencrypted, so the unit loads the credential from the store the module chose. - The service reads
${CREDENTIALS_DIRECTORY:-/etc/credstore}/<name>. A script aborts with a hint to run the unit if the file is not readable: started by hand, outside the unit, it cannot decrypt the credential. - A tool that admins also or only run by hand (e.g.
duba,nextcloud-update) instead falls back tosystemd-creds decrypt --name=<name> /etc/credstore.encrypted/<name> -if neither file exists, which works for root only. It passes the secret on to the program it calls through the environment or stdin, never on the command line. - Use a template rendered through
lookup('ansible.builtin.template', ...)ascontent, belowtemplates/etc/credstore/. It keeps the header and version line, but is never deployed itself, so there is nobackup: truecopy of the secret. Exceptions: build a JSON credential inline withto_json(e.g.'{{ {"password": ...} | to_json }}'), since ansible-core before 2.19 turns a rendered string that looks like a dict into one, and the module then stores its Python repr. Pass a bare secret inline if the consumer takes the whole file as the value and a header would become part of it (roles/nextcloud_whiteboard_server). - When moving an existing secret file to a credential, delete the old file and its
backup: truecopies. - Name the credential after the file it replaces, e.g.
mariadb-dump-login.cnf. - Document in the role README where the secret ends up per platform, that a script has to run via
systemctl start <unit>, and the TPM2 caveat: with a TPM2 chip, the credential is bound to the Secure Boot state, so after a firmware or Secure Boot change the unit cannot read it until the role runs again. - If the service must keep working without the secret, leave
LoadCredentialEncrypted=out of the unit: when systemd cannot decrypt a credential listed there, the unit does not start at all. The script then decrypts it itself (systemd-creds decrypt --name=<name> /etc/credstore.encrypted/<name> -, else reads/etc/credstore/<name>) and goes on without it on failure.roles/schedule_reboot(do-reboot) does this, so that a credential the TPM2 no longer releases costs the Icinga downtime, not the reboot. - If the program fetches the secret through a command itself (e.g. borg's
BORG_PASSCOMMAND), give itsystemd-creds decrypt --name=<name> /etc/credstore.encrypted/<name> -when the registered result isencrypted, elsecat /etc/credstore/<name>. The secret then reaches neither an environment variable nor the command line, and admins can run the program by hand as root.roles/borg_localdoes this. - If the service only takes the secret inline in its config, the pattern does not apply without a wrapper that renders the config at start; keep the config file
0600and owned by the service user. - A rootless Podman container (a user unit) gets a user-scoped credential: store it with
scope: 'user'andbecome_user: <service user>, which needs systemd 256 or newer and ends up in~/.config/credstore.encrypted/<name>. systemd 257 cannot load it withLoadCredentialEncrypted=in a user unit (systemd 258 can), and Podman'sshellsecret driver runs its lookup as UID 0 in the user namespace, wheresystemd-credsfails too. So the Quadlet decrypts it inExecStartPre=(systemd-creds decrypt --user) into itsRuntimeDirectory=(tmpfs), mounts the file read-only with:Uinto the container, and an entrypoint wrapper reads it there and refuses to start without it. Never hand the secret to the container as an environment variable (Environment=,EnvironmentFile=,--secret type=env): Podman keeps the environment of a running container in plain text in its storage below the user's home.roles/nextcloud_whiteboard_serverimplements this. roles/borg_local(borg-backup),roles/duplicity(duba),roles/mariadb_server(mariadb-dump),roles/mongodb(mongodb-dump) androles/nextcloud(nextcloud-update) implement this pattern.