Skip to content

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.conf for the role's [Service] settings, z00-after-<dependency>.conf for ordering) and run systemctl daemon-reload when changed.
  • For a unit of another software, use z00-<role>.conf (e.g. roles/librenms writes rrdcached.service.d/z00-librenms.conf), so the role owning that software does not overwrite it.
  • If a role's <role>__kernel_settings__*__dependent_var sets a value its service reads only at startup (e.g. net.core.somaxconn, applied in listen()), add a z00-after-tuned.conf with [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/example implements the chain (example__kernel_settings__sysctl__dependent_var, fed into kernel_settings by playbooks/example.yml, plus the drop-in). roles/icinga2_agent shows 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 include shared/tasks/reload-changed-timers.yml right after the role deployed its units, with the timers that changed (shared__reload_changed_timers, shared__reload_changed_timers_user for 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-reload task, and never notify a daemon-reload handler for a timer instead: the handler runs at the end of the play, and any enable / disable in 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.target in its [Install] section is fine.
  • roles/system_update implements this; extensions/molecule/shared/reload_changed_timers tests 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_credential and 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/ (0700 directory, 0600 file, root).
  • In the unit, add LoadCredentialEncrypted=<name> only when the registered result is encrypted, 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 to systemd-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', ...) as content, below templates/etc/credstore/. It keeps the header and version line, but is never deployed itself, so there is no backup: true copy of the secret. Exceptions: build a JSON credential inline with to_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: true copies.
  • 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 it systemd-creds decrypt --name=<name> /etc/credstore.encrypted/<name> - when the registered result is encrypted, else cat /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_local does 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 0600 and owned by the service user.
  • A rootless Podman container (a user unit) gets a user-scoped credential: store it with scope: 'user' and become_user: <service user>, which needs systemd 256 or newer and ends up in ~/.config/credstore.encrypted/<name>. systemd 257 cannot load it with LoadCredentialEncrypted= in a user unit (systemd 258 can), and Podman's shell secret driver runs its lookup as UID 0 in the user namespace, where systemd-creds fails too. So the Quadlet decrypts it in ExecStartPre= (systemd-creds decrypt --user) into its RuntimeDirectory= (tmpfs), mounts the file read-only with :U into 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_server implements this.
  • roles/borg_local (borg-backup), roles/duplicity (duba), roles/mariadb_server (mariadb-dump), roles/mongodb (mongodb-dump) and roles/nextcloud (nextcloud-update) implement this pattern.