Skip to content

Failures

Ansible evaluates the return code of each task to determine whether the task succeeded or failed. There are a number of Ansible features that can be used to manage task errors.

Ignoring Task Failure

By default, if a task fails, the play is aborted. You can use the ignore_errors keyword to override it.

Example:

- name: Install xyz
  yum:
    name: xyz
    state: latest
  ignore_errors: yes

Set the force_handlers: yes on a play so that notified handlers are called even if the play aborted.

Specifying Task Failure Conditions

You can use the failed_when keyword on a task to specify which conditions indicate that the task has failed.

Example:

tasks:
  - name: Run script
    shell: /usr/local/bin/script.sh
    register: command_result
    failed_when: "'some_string' in command_result.stdout"

Ansible Blocks and Error Handling

In playbooks, blocks are clauses that logically group tasks, and can be used to control how tasks are executed. A task block can have a when keyword to apply a conditional to multiple tasks.

  • block: Defines the main tasks to run.

  • rescue: Defines the tasks to run if the tasks defined in the block clause fail.

  • always: Defines the tasks that will always run independently of the success or failure of tasks defined in the block and rescue clauses.