Ansible display the diff output of a task even if the task failed

I have also written a simple ansible task module which does the same as shell, but will include the stdout & stderr as part of it's diff results, so you can see it if you use --diff. But if you run ansible with --diff and a task fails, it doesn't display the diff output.

If the task fails, ansible will just print the raw JSON of the whole task details, with the stdout & stderr displayed as part of the JSON results. It is a little irritating to have to read the large multiline JSON output string.

Is there anyway to format and display the diff output even if the task fails?

ansible 2.9.6 on python 2.7.


When you write your own Ansible module, you're on your own how to handle task errors. I expect, that f the underlying shell execution fails, your module checks the return code and fails programmatically. Your code returns a dictionary containing stdout and stderr. But you could return whatever you want in failure state.

In that case a default behaviour of Ansible starts and stops the process, displaying anything it has ignoring any format etc.

Because you didn't post any content, it is difficult to help you, except explaining it in meta form.

  • should the module return a failure state, when you want the diff in that case? or is it useful to return some dict key:value that you can register and use it to check, if the play should fail after showing the diff
  • shoudl the failure state contain the stdout/stderr or can you just output the wanted format (be aware, that a shell command that fails - what is the diff, that you use to find the difference)

Also - not the question - but it sounds strange to implement a non-idempotent shell module with a diff tool. When you have a "value before" (what ever that is), how do you see, what is the "value after" to create a difference?