Interview Prep/Ansible/Variables, Facts and Templates

How do you detect that a registered result was skipped or failed and branch on it?

IntermediateScenarioVariables, Facts and Templates
Answer

Situation read → Use result tests rather than raw fields.

What I'd do →

- ansible.builtin.command: grep -q ready /tmp/state
  register: r
  failed_when: false
- ansible.builtin.debug: { msg: "not ready" }
  when: r is failed
- ansible.builtin.debug: { msg: "changed" }
  when: r is changed

Why → is failed, is changed, is skipped and is succeeded are readable and robust.

Interview tip → Avoid the legacy r|failed filter syntax.

All Ansible questions Roadmap: Ansible

More on Variables, Facts and Templates