# terminate the whole play if any fatal errors on any host occurs

**URL:** <https://forum.ansible.com/t/terminate-the-whole-play-if-any-fatal-errors-on-any-host-occurs/38062>\
**Category:** Ansible Project\
**Created:** [June 5, 2023, 2:07pm UTC](https://forum.ansible.com/t/terminate-the-whole-play-if-any-fatal-errors-on-any-host-occurs/38062 "2023-06-05T14:07:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Edvinas](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/edvinas/32/5406_2.png) [@Edvinas](https://forum.ansible.com/u/Edvinas)\
**Post date:** [June 5, 2023, 2:07pm UTC](https://forum.ansible.com/t/terminate-the-whole-play-if-any-fatal-errors-on-any-host-occurs/38062/1 "2023-06-05T14:07:42Z")

</div>

Hello,

My playbook has several hosts, and I have some tasks with **assert** and **fail** modules. How to do that if the task gets any fatal error in the play within particular host - the whole play should is terminated ?

I thought that playbook command **any\_errors\_fatal** would work, but seems not.

Is there any other way to make playbook stop if any of the hosts experiences errors ?

I see ansible has meta modules - there are some kind like end\_play module, maybe this one would work…

Thank you.

---

<div class="post-metadata">

**Author:** ![Kosala\_Atapattu1](https://avatars.discourse-cdn.com/v4/letter/k/7feea3/32.png) [@Kosala\_Atapattu1](https://forum.ansible.com/u/Kosala_Atapattu1)\
**Post date:** [June 5, 2023, 10:13pm UTC](https://forum.ansible.com/t/terminate-the-whole-play-if-any-fatal-errors-on-any-host-occurs/38062/2 "2023-06-05T22:13:46Z")

</div>

Yes any\_errors\_fatal is what you need: [https://docs.ansible.com/ansible/latest/playbook\_guide/playbooks\_error\_handling.html#aborting-on-the-first-error-any-errors-fatal](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_error_handling.html#aborting-on-the-first-error-any-errors-fatal)

When you say it doesn’t seem to work, what does not seem to work?

---

<div class="post-metadata">

**Author:** ![Edvinas](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/edvinas/32/5406_2.png) [@Edvinas](https://forum.ansible.com/u/Edvinas)\
**Post date:** [June 6, 2023, 6:51am UTC](https://forum.ansible.com/t/terminate-the-whole-play-if-any-fatal-errors-on-any-host-occurs/38062/3 "2023-06-06T06:51:48Z")

</div>

Hello, thanks for reply.

I’ve two hosts in the play, and one of the hosts assert module doesnt validate to true and fails. With any\_errors\_fatal command I expect the playbook to stop on all hosts within the play. But in my case, the play stops only on failed host, it continues with other host where assertions passed.

Is this expected behaviour ?

---

<div class="post-metadata">

**Author:** ![Kosala\_Atapattu1](https://avatars.discourse-cdn.com/v4/letter/k/7feea3/32.png) [@Kosala\_Atapattu1](https://forum.ansible.com/u/Kosala_Atapattu1)\
**Post date:** [June 6, 2023, 7:05am UTC](https://forum.ansible.com/t/terminate-the-whole-play-if-any-fatal-errors-on-any-host-occurs/38062/4 "2023-06-06T07:05:40Z")

</div>

The doc says it should do what you’re after.

If you set `any_errors_fatal` and a task returns an error, Ansible finishes the fatal task on all hosts in the current batch, then stops executing the play on all hosts. Subsequent tasks and plays are not executed. You can recover from fatal errors by adding a [rescue section](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_blocks.html#block-error-handling) to the block. You can set `any_errors_fatal` at the play or block level.

I never have used it personally, but are you setting any\_errors\_fatal:true at the task level or playbook level?

---

<div class="post-metadata">

**Author:** ![Will\_McDonald](https://avatars.discourse-cdn.com/v4/letter/w/f475e1/32.png) [@Will\_McDonald](https://forum.ansible.com/u/Will_McDonald)\
**Post date:** [June 6, 2023, 9:24am UTC](https://forum.ansible.com/t/terminate-the-whole-play-if-any-fatal-errors-on-any-host-occurs/38062/5 "2023-06-06T09:24:00Z")

</div>

Hey Edvinas,

As Kosala mentions, this should do what you want, however I think the specific piece of documentation to note is “Ansible **finishes the fatal task on all hosts in the current batch** ”. With 2 hosts, and the default of 5 forks, your batch size will be 5 so your play will carry on across both hosts. If you set “serial: 1” it’ll probably behave the way you want.

Cheers,

Will.

---

<div class="post-metadata">

**Author:** ![Edvinas](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/edvinas/32/5406_2.png) [@Edvinas](https://forum.ansible.com/u/Edvinas)\
**Post date:** [June 6, 2023, 10:07am UTC](https://forum.ansible.com/t/terminate-the-whole-play-if-any-fatal-errors-on-any-host-occurs/38062/6 "2023-06-06T10:07:24Z")

</div>

I havent mentioned, that im using roles, when thereis simple playbook - everything works as expected - it quits playbook after assertion fails.  
When using roles - the assertion fails - and the playbook continues only with the host, which had assertion passed.

I think its a bug, but i will try serial 1.

---

<div class="post-metadata">

**Author:** ![Edvinas](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/edvinas/32/5406_2.png) [@Edvinas](https://forum.ansible.com/u/Edvinas)\
**Post date:** [June 6, 2023, 1:29pm UTC](https://forum.ansible.com/t/terminate-the-whole-play-if-any-fatal-errors-on-any-host-occurs/38062/7 "2023-06-06T13:29:51Z")

</div>

checked again, problem with serial 1 is that it doesn’t run tasks in parallel.

so then i have same playbook which is not in roles it runs like this:

_TASK [assert] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*_  
_ok: [10.8.250.46]_  
_fatal: [10.8.250.47]: FAILED! =\> {“assertion”: “(‘MGMT0’ not in interface\_running\_config[‘interface’][‘Ethernet’ ~ full\_fex\_interface\_number][‘description’] | default (‘’))”, “changed”: false, “evaluated\_to”: false, “msg”: “failed”}_  
_[10.8.250.47] TASK: assert (debug)\>_

but when the same playbook is in roles modes it continues to play even assert fails:

_TASK [network\_port\_channel : assert] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*_  
_fatal: [TEONET01B]: FAILED! =\> {“assertion”: “(‘MGMT0’ not in interface\_running\_config[‘interface’][‘Ethernet’ ~ full\_fex\_interface\_number][‘description’] | default (‘’))”, “changed”: false, “evaluated\_to”: false, “msg”: “failed”}_  
_ok: [TEONET01A]_

_TASK [network\_port\_channel : fail] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*_  
_skipping: [TEONET01A]_

So i still think that it’s the bug - the any\_errors\_fatal: true doenst work in roles.

Will, i think this sentence (“Ansible **finishes the fatal task on all hosts in the current batch** ”). Means that it finishes only the TASK but not the whole playbook.

---

<div class="post-metadata">

**Author:** ![Edvinas](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/edvinas/32/5406_2.png) [@Edvinas](https://forum.ansible.com/u/Edvinas)\
**Post date:** [June 7, 2023, 2:16pm UTC](https://forum.ansible.com/t/terminate-the-whole-play-if-any-fatal-errors-on-any-host-occurs/38062/8 "2023-06-07T14:16:11Z")

</div>

Last update (I think).

So i managed to clarify the situation. Seems that the problem occurs only when i’m using roles with block/rescue options. Without it everything works smooth.

For example:  
**Roles Playbook without block/rescue:**  
**- hosts: TEONET01A,TEONET01B**

 

**```auto
  gather_facts: false
  any_errors_fatal: True
  roles:
    - test**
	
**Roles task (block commented):
---
# - name: Run test pbook
# block:
    - assert:
        that:
          - ansible_hostname == 'TEONET01A'
    - debug:
        msg: In role**

**# rescue:
# - debug:
# msg: Rescue**

**```**

 

**```auto**

**Runs as expected and play exists after fatal error in assert:
root@PCEDVKAIL3:/etc/ansible# ansible-playbook playbook.yml**

**```**
