I solved this by adding
ignore_errors: true
register: results
to the no_log-task. This makes ansible continue to the next task, even when the task fails. Then for the next task define a debug task, which always fails and outputs the registered variable, but only runs when the previous task failed:
- name: Error output
debug:
var: results
failed_when: true
when:
results is failed
So even with no_log: true, this will make ansible display the output of the failed task.
This solution is not logging it to a file as requested but fullfils your need to 'see the log when failed' , and of course, you can redirect or use tee to output the full ansible output to a file, which will, with this solution also contain the the log of the failed task.
failed_when
condition, and remove the log file, if the prev. task was succesful :)-vvv
after theansible-playbook
command for getting verbose logs.