Tasks¶
- Always use the FQCN of the module.
- Use meta modules:
ansible.builtin.packageinstead ofyum/dnf/apt,ansible.builtin.serviceinstead ofsystemd. - Prefer
ansible.builtin.command(oransible.windows.win_command) overshelloverraw, andansible.builtin.templateovercopy,lineinfileorblockinfile. Exception: see Setting options in a packaged file instead of templating it. - Pin
ansible.builtin.shelltoexecutable: '/bin/bash'(inargs:or next tocmd:): Debian's/bin/shisdash, which rejectsset -o pipefail,[[ ... ]]andsource./bin/bashresolves with or without usrmerge,/usr/bin/bashdoes not. - Never
state: 'latest'withansible.builtin.package, usestate: 'present'. - Use
delegate_to: 'localhost', neverlocal_action. - Set both
become: falseandvars: ansible_become: false # noqa var-naming[pattern]on every task or block delegated to localhost. Play-levelbecome: truepropagates to delegated tasks, andansible_become: truein the inventory overrides thebecome: falsekeyword; only the task variable overrides the inventory. Otherwise the task fails withsudo: a password is required, or runs as root on the controller. Verified with ansible-core 2.16 and 2.18. - Avoid
run_once: true: it evaluateswhen(also that of an enclosing block or include) against the first host only, and if that host skips, every host skips. Let controller-side tasks run per host:ansible.builtin.get_urlwriting a single file is race-safe (temp file plus atomic rename); with a version-pinneddestonly the first host downloads.- Tasks mutating a shared controller path in place (
ansible.builtin.gitinto a shared working dir,command/shellbuilding files under/tmp) getthrottle: 1with a comment# serialize: .... - Where hosts write to different directories depending on their facts (interpreter, architecture), a
flockper directory instead ofthrottle: 1lets the directories be built in parallel, while the hosts sharing one wait for the first. Reference: the dependency download inroles/monitoring_plugins. run_oncestays only for a single read-only lookup shared to all hosts, e.g. a GitHub release API query stored withset_fact.
- Set
vars: mitogen_task_isolation: 'fork' # noqa var-naming[pattern]on controller-side tasks that take time per host:ansible.posix.synchronize(which runsrsyncon the controller), downloads,git ls-remote. Under the Mitogen strategy, all tasks that run on the controller go to the one Mitogen context of the controller, which runs one call at a time, so these tasks run one host after the other: ten hosts delegating a two-second command to localhost take 20.9 s, 3.4 s withfork(Mitogen 0.3.44, ansible-core 2.16.19). Other strategies ignore the variable. Tasks withthrottle: 1and quick ones (stat,set_fact) gain nothing. - Download on the controller (
delegate_to: 'localhost') and copy to the target: release artifacts, Git checkouts, language packages. Only the controller needs outbound access; say who needs what in "How the Role Behaves". Seeroles/exampleand Python dependencies for air-gapped targets. - Retry every download with
retries: 3anddelay: 10:get_url,git,rpm_keywith a URL,urilookups such as the GitHub release API, and everypackage,apt,dnfandpiptask that installs or updates (notstate: 'absent'). Since ansible-core 2.16retrieswithoutuntilrepeats until success. Never retry calls that change something, such as an APIPOST. - Always give
commandandshelltaskschanged_when,createsorremoves;changed_when: falsewhen read-only, andchanged_when: true # <reason>when a command always runs and its effect cannot be detected. - A read-only
commandwithcheck_mode: falsethat calls a binary the role installs itself (package, unpacked archive) fails under--checkon a host without it, since nothing is installed in check mode. Register the task that provides the binary, skip the command withwhen: 'not (ansible_check_mode and <result> is changed)', and let the tasks using its result cope with a skipped one (<result>["stdout_lines"] is defined). Add<result> is defined andwhen a tag can skip the providing task. References:roles/example,roles/lvm(growpart). - Prefer
chown -R --changesoveransible.builtin.filewithrecurse: true(slow on large trees), withchanged_when: '<result>["stdout"] | length > 0'. - With
ansible.builtin.template, always setbackup,src,dest,owner,groupandmode. - The web server user (or any other service user) owns only what the application writes at runtime, such as uploads, caches or a configuration the web UI edits. Code, and configuration it only reads, belong to
root; grant read access through the group (owner: 'root',group: '{{ __shared__apache_httpd_group }}',mode: 0o640). Otherwise a compromised web application can change the code or its own configuration. Seeroles/example. - Prefer
ansible.builtin.assertoveransible.builtin.failwithwhen, for consistency. - Optionally add
ansible.builtin.debugtasks for__combined_varvariables. - Split service
enabledandstateinto separate tasks, see Handlers. - Check
ansible_facts["selinux"]["status"] != "disabled"before managing SELinux ports, file contexts or booleans.
Controller-side Caches¶
What a role prepares on the controller for every host (Git checkouts, staging trees, downloaded packages) is cached, so that the first host prepares it and every further host and later run finds it done. Reference: roles/monitoring_plugins.
- Keep the cache below
/var/tmp/lfops/<role>/, which outlives a reboot./var/tmp/lfopsis the one directory the README asks ansible-navigator users to mount into the EE, whose own/var/tmpstarts empty with every run. - Create both directories with
mode: 0o700. The cache holds what is deployed to every host as root; below the world-writable/var/tmp, another user could create the directory first, and setting the mode then fails, so the run aborts instead of deploying that user's content. - Name each entry after the state it holds, a release tag or a resolved commit (
git ls-remote), never after a branch, so that an entry found there is final and needs no update. Hosts with different versions in one play then never share an entry. - Build an entry in a temporary directory next to it and move it into place, or mark it complete with a file inside it as the last step, so that a run that aborts halfway leaves nothing a later host takes for complete.
- Remove entries older than 7 days at the start of the role (
find <cache> -mindepth 1 -maxdepth 1 -mtime +7 -exec rm -rf -- {} +). systemd-tmpfiles ages out files below/var/tmpafter 30 days one by one, which could leave an entry that looks complete with files missing; this also takes away versions no longer in use.
OS-specific Tasks¶
Platform-specific tasks go into tasks/<platform>.yml; only the most specific match runs, then main.yml (a CentOS 7.9 host with CentOS7.4.yml, CentOS7.yml, RedHat.yml runs CentOS7.yml). Include them with the include_tasks + first_found task (skip: true) from roles/example/tasks/main.yml, which tries distribution + distribution_version, distribution + distribution_major_version, distribution, then the same for os_family. Tag always on the include_tasks task itself, not on a surrounding block, which would pass the tag on to all included tasks.
Handling of GPG Keys under Debian (APT Keyring)¶
/etc/apt/trusted.gpg.d trusts a key for all repositories, so apt-key and ansible.builtin.apt_key are deprecated. Instead:
- Store the key in
/etc/apt/keyrings/with the extension matchingfile:PGP public key block Public-Key (old)is.asc,OpenPGP Public Keyis.gpg. - Reference it in
/etc/apt/sources.list.d/:deb [signed-by=/etc/apt/keyrings/icinga.asc] https://....
References: roles/repo_icinga/tasks/Debian.yml (ASCII-armored), roles/repo_mariadb/tasks/Debian.yml (binary).