What is the difference between `assert` and `fail`?
IntermediateConceptPlaybooks, Plays, Tasks and Handlers
Answer
fail aborts with a message (usually together with when). assert evaluates a list of conditions and fails if any is false, so it reads as validation.
- ansible.builtin.assert:
that:
- app_version is defined
- app_version is match('^[0-9]+\.[0-9]+\.[0-9]+$')
- ansible_facts['memtotal_mb'] >= 2048
fail_msg: "Need a semver app_version and at least 2 GB RAM"
success_msg: "Preflight OK"
Put assertions at the top of a play to fail fast before anything changes.