Interview Prep/Ansible/Playbook Design, Handlers and Idempotency

A step is allowed to fail, but you still need to react to the failure.

IntermediateScenarioPlaybook Design, Handlers and Idempotency
Answer

Situation read → Not every failure is fatal.

What I'd do → Use block/rescue/always, or ignore_errors plus register for simple cases.

- block:
    - ansible.builtin.command: /opt/app/migrate
  rescue:
    - ansible.builtin.command: /opt/app/rollback
    - ansible.builtin.fail: { msg: "migration failed, rolled back" }
  always:
    - ansible.builtin.debug: { msg: "cleanup done" }

Why → rescue runs on failure, always runs regardless, and re-failing keeps the run honest.

Interview tip → ignore_errors hides failures; failed_when redefines them. Pick intentionally.

All Ansible questions Roadmap: Ansible

More on Playbook Design, Handlers and Idempotency