Is there a way to act on a failed task

Hi,

I’m new to Ansible, and trying to find a way to write a file when a task fails. I’m cool with the play stopping so it’s not ignore_fail I’m looking for but something like

when: fail command echo “sometext” > /tmp/error

Is something like this possible, and if so, how ?

/Steen

Off the top of my head (and untested (and before coffee) ) ...

- name: command that fails
  shell: exit 1
  register: last_command
  ignore_errors: true

- name catch failed command
  shell: echo "Oops!"
  when: last_command.rc != 0

Kahlil (Kal) Hodgson GPG: C9A02289
Head of Technology (m) +61 (0) 4 2573 0382
DealMax Pty Ltd (w) +61 (0) 3 9008 5281

Suite 1415
401 Docklands Drive
Docklands VIC 3008 Australia

"All parts should go together without forcing. You must remember that
the parts you are reassembling were disassembled by you. Therefore,
if you can't get them together again, there must be a reason. By all
means, do not use a hammer." -- IBM maintenance manual, 1925

exactly!

Thanks for the reply. If I do it like that however I would need to stop the play explicitly since i ignore the errors. Is there a way to do that ?

/Steen

​Add a subsequent fail: task when: last_command.rc != 0​

Ah, thanks. Wasn’t aware of the fail task. That will do nicely.

/Steen