Developing Ansible Tasks¶
Covers task key ordering, module selection, and iteration.
Task Key Ordering¶
Every task follows this key order:
name- required; place it first.when- if used, immediately aftername.become- if used, followswhen(ornameif nowhen).- Module key - the actual module and its arguments.
loop/loop_control- if used, after the module.notify- if used, near the end.changed_when/failed_when- if used, last.
Module Selection¶
- You MUST use fully qualified collection names (FQCN), e.g.
ansible.builtin.copy, notcopy. - You SHOULD prefer Ansible modules over
ansible.builtin.shell/ansible.builtin.command. - When
shellorcommandis unavoidable: - Add
createsorremovesto enable idempotency checks. - Add
changed_whenandfailed_whenfor correct change and error reporting. - You SHOULD use
ansible.builtin.templatefor Jinja2-rendered configs. - Use
ansible.builtin.copyfor static files.
Facts¶
- You MUST reference facts via
ansible_facts, e.g.ansible_facts['os_family']. - You MUST NOT use the legacy bare fact names (e.g.
ansible_os_family).
Iteration¶
- You MUST use
loopinstead of anywith_*construct. - You SHOULD set a custom loop variable via
loop_controlto avoid variable collisions: