Interview Prep/Ansible/Vault, Privilege Escalation and Error Handling

How do you inspect what failed in a `rescue` block?

AdvancedConceptVault, Privilege Escalation and Error Handling
Answer

Inside rescue, two variables describe the failure: ansible_failed_task (name, action and so on) and ansible_failed_result (the module's return data).

rescue:
  - ansible.builtin.debug:
      msg: "Task '{{ ansible_failed_task.name }}' failed: {{ ansible_failed_result.msg | default('no msg') }}"
  - ansible.builtin.uri:
      url: "{{ slack_webhook }}"
      method: POST
      body_format: json
      body: { text: "Deploy failed on {{ inventory_hostname }}" }

Notify, roll back, then decide whether to let the play carry on or end it with a final fail (or meta: end_host) so the failure is not masked.

All Ansible questions Roadmap: Ansible

More on Vault, Privilege Escalation and Error Handling