Ansible Tutorial · Advanced Topics · Lesson 16 of 20
Ansible Error Handling
Control how Ansible responds to task failures.
Default behaviour
By default, when a task fails on a host, Ansible stops running tasks on that host but continues on other hosts. At the end of the play, failed hosts are reported.
ignore_errors
Continue even if the task fails:
- name: Check if app is running (might not be)
ansible.builtin.command: pgrep -f myapp
register: app_check
ignore_errors: true
- name: Start app if not running
ansible.builtin.service:
name: myapp
state: started
when: app_check.rc != 0
failed_when — custom failure conditions
Override what Ansible considers a failure:
- name: Run database migration
ansible.builtin.command: python manage.py migrate
register: migrate_result
failed_when: "'ERROR' in migrate_result.stderr"
The task is marked as failed only when "ERROR" appears in stderr, not on any non-zero return code.
changed_when — control the "changed" status
- name: Check if reboot is needed
ansible.builtin.command: needs-restarting -r
register: reboot_check
changed_when: reboot_check.rc == 1
failed_when: reboot_check.rc > 1
This prevents the task from always reporting "changed" when it is really just a check.
any_errors_fatal
Stop the entire play (all hosts) if any single host fails:
- name: Critical deployment
hosts: webservers
any_errors_fatal: true
tasks:
- name: Run database migration (must succeed everywhere)
ansible.builtin.command: python manage.py migrate
block / rescue / always
- block:
- name: Deploy new code
ansible.builtin.git:
repo: https://github.com/org/app.git
dest: /var/www/app
version: "{{ version }}"
- name: Run post-deploy checks
ansible.builtin.uri:
url: "http://localhost:{{ app_port }}/health"
status_code: 200
rescue:
- name: Rollback
ansible.builtin.git:
repo: https://github.com/org/app.git
dest: /var/www/app
version: "{{ previous_version }}"
- name: Notify team
ansible.builtin.debug:
msg: "Deploy of {{ version }} failed — rolled back"
always:
- name: Clean temp files
ansible.builtin.file:
path: /tmp/deploy_lock
state: absent
max_fail_percentage
- name: Rolling update
hosts: webservers
serial: 2
max_fail_percentage: 25
If more than 25% of hosts in a batch fail, the play aborts for the remaining batches.
cat > /tmp/error_demo.yml << 'EOF'
---
- name: Error handling demo
hosts: localhost
connection: local
gather_facts: false
tasks:
- name: This will fail
ansible.builtin.command: /bin/false
ignore_errors: true
- name: This still runs
ansible.builtin.debug:
msg: "Previous task failed but we continued!"
EOF
ansible-playbook /tmp/error_demo.yml
Output
PLAY [Error handling demo] ****************************************************
TASK [This will fail] *********************************************************
fatal: [localhost]: FAILED! => {"changed": true, "cmd": ["/bin/false"], "delta": "0:00:00.002", "end": "2026-11-25 10:00:01", "msg": "non-zero return code", "rc": 1, "start": "2026-11-25 10:00:01", "stderr": "", "stdout": ""}
...ignoring
TASK [This still runs] ********************************************************
ok: [localhost] => {
"msg": "Previous task failed but we continued!"
}
PLAY RECAP ********************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1Run this on your computer — see “Get Started” for setup.
Test yourself
What does 'any_errors_fatal: true' do?
Ignores all errorsStops the entire play if any host failsRetries failed tasksSends an error notification
Show answer
Answer: Stops the entire play if any host fails
With any_errors_fatal, a failure on any single host causes Ansible to abort the entire play for all hosts, not just the failed one.