Ansible Error Handling

By default, Ansible stops execution on a host and moves on to the other host when a non-zero return code is received from a command, task, or failure from a module. However, in some cases we might expect different behavior. A non-zero return code may also indicate success. You may also want that a failure on one host should stop execution on all the hosts. Ansible facilitates settings to handle these situations and helps you get the behavior, output, and reporting you to want through error handling.

Ignoring failed commands

Consider the below playbook:

- name: "strategy demo"
  hosts: webserver1,sqlserver1
  tasks:
    - name: "first task"
      command: touch /tmp/task/task1.txt
    - name: "second task"
      command: touch /tmp/task2.txt

Suppose that in the first task, the /tmp/task directory does not exist. In that case the first task would fail and Ansible would terminate the execution.

To ignore the failed commands, you can use ignore_errors to continue to the next task in spite of the failure:

Modified playbook:

- name: "strategy demo"
  hosts: webserver1,sqlserver1
  tasks:
    - name: "first task"
      command: touch /tmp/task/task1.txt
      ignore_errors: True
    - name: "second task"
      command: touch /tmp/task2.txt

The above playbook will execute the 'second task' even after the first task's failure: ansible-loop-output

Kindly note that the ignore_errors directive will only work when the task is able to run and return a value associated with failure. Any undefined variables or syntax errors, connection failures, execution issues will not be ignored and will have to be addressed.