I'm new to Ansible so I'm probably approaching this incorrectly, but I'm trying to find a way to re-raise an error once it has been rescue
d by a block.
The purpose of this is to be able to log failed tasks to an API before terminating the playbook. I would use the always
section, but then the ansible_failed_task
and ansible_failed_result
variables are not populated.
I'm fairly confident that my approach is wrong, so how would an experienced Ansible dev handle this? Thanks!
If I correctly understand what you are trying to do, this is a basic (non functional) illustration of how I would manage that. The key is using the fail
module to end playbook in rescue phase after handling the error the way you wish.
- name: handle error nicely in my block
block:
- name: This is my task that can fail
debug:
msg: "I'm a a task that can fail"
register: some_var
rescue:
- name: Do whatever you need to log the failure
debug:
msg: "I'm a log task playing around with some_var: {{ some_var }}"
- name: fail the playbook as the task was not successful
fail:
msg: "The task was not successful. Aborting"