Skip to content

Templates

  • Always use ansible.builtin.template, never copy, even without variables: easier to extend, and it allows the header.
  • Always set backup: true (timestamped copy, e.g. keycloak.conf.23875.2025-02-14@15:19:16~), except for a file that holds or held a secret: the copy keeps the secret after it moved to a systemd credential or changed. Delete the copies such a file got from earlier versions of the role (roles/borg_local, roles/nextcloud).
  • Start every template with # {{ ansible_managed }} and a # YYYYMMDDNN version line (e.g. # 2021081601), in the target's comment syntax.
  • Update the version line whenever the rendered file changes, and only then. A change that renders the same on every host (a variable renamed to one with the same value, a Jinja comment such as the Based on line) keeps it: the version line alone would make every host report a changed file.
  • Never use {{ template_run_date }}, it breaks idempotency.
  • Mirror the target path below templates/ (templates/etc/httpd/sites-available/default.conf.j2) and always use .j2.
  • Deploy self-written scripts to /usr/local/sbin (SELinux). Helpers run only by a systemd unit (not by an admin, not exec'd by a confined domain) MAY live in /usr/local/libexec: systemd starts them in unconfined_service_t, as from /usr/local/bin, since the policy transitions init_t to it for usr_t just as for bin_t; no AVC denial on RHEL 8, 9 and 10. Admin-invokable commands stay in /usr/local/sbin, and a script a confined domain must exec never goes to /usr/local/libexec.
  • Keep templates as close to the original file as possible, which eases rpmnew/rpmsave handling.
  • A template that is a copy of a packaged file names the package release it was taken from in a Jinja comment right below the version line, {# Based on graylog-server 7.2.1 #}, which does not end up on the host. List it in tools/follow-upstream.yml, see Following Upstream Releases.
  • If the role picks a file by the installed version (<version>-<name>.conf.j2, vars/<version>.yml), list the shipped versions in vars/main.yml and assert against them right after reading the version, as roles/example does (__example__supported_versions), so the admin gets the supported versions instead of a missing-file path. Document the abort under ## Troubleshooting (pin a supported version, or add the file).
  • After deploying a file that may get rpmnew / rpmsave files (or Debian equivalents), include shared with tasks_from: 'remove-rpmnew-rpmsave.yml' and shared__remove_rpmnew_rpmsave_config_file, as in roles/example.