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.
More on Vault, Privilege Escalation and Error Handling
What is Ansible Vault?BeginnerWhich `ansible-vault` commands do you use day to day?BeginnerHow do you supply the vault password when running a playbook?BeginnerWhat is `encrypt_string` and when is it better than encrypting a whole file?IntermediateWhat are vault IDs and why use them?AdvancedHow is Vault data encrypted and what does the file look like?Advanced