DevOps / Ansible Interview questions
When should you use blocks with rescue in Ansible?
A block groups related tasks together, and pairing it with rescue gives Ansible try/catch-like error handling: if any task inside the block fails, execution jumps to the rescue section instead of immediately failing the whole host.
- block: - name: Deploy new application version ansible.builtin.command: /opt/deploy.sh rescue: - name: Roll back to previous version ansible.builtin.command: /opt/rollback.sh always: - name: Notify deployment status ansible.builtin.debug: msg: "Deployment attempt finished"
Use this pattern whenever a sequence of steps needs a defined fallback if something partway through fails, such as rolling back a deployment, cleaning up a partially-created resource, or sending an alert before giving up on that host. The optional always section runs regardless of whether the block succeeded or the rescue was triggered, making it a natural place for cleanup or notification logic that must happen either way.
More Related questions...