# Abort executing playbook if condition not match

**URL:** <https://forum.ansible.com/t/abort-executing-playbook-if-condition-not-match/23728>\
**Category:** Ansible Project\
**Created:** [February 14, 2017, 4:43pm UTC](https://forum.ansible.com/t/abort-executing-playbook-if-condition-not-match/23728 "2017-02-14T16:43:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DHAVAL\_JAISWAL](https://avatars.discourse-cdn.com/v4/letter/d/34f0e0/32.png) [@DHAVAL\_JAISWAL](https://forum.ansible.com/u/DHAVAL_JAISWAL)\
**Post date:** [February 14, 2017, 4:43pm UTC](https://forum.ansible.com/t/abort-executing-playbook-if-condition-not-match/23728/1 "2017-02-14T16:43:30Z")

</div>

If condition fails it should not go for the next step and exit from there only.

In following situation, when: C != test\_val it should exit from here only instead of going to the next step. How can i achieve this.

- name: Set the output of the this state  
set\_fact: A=“{{image\_avl.stdout}}”  
tags: avl1

- name: Define our static value for the desired image number  
set\_fact: B={{retain}}  
tags: num2

- name: difference  
set\_fact: C = {{A}} - {{B}}  
tags: del1

- name: set the desire value  
set\_fact: test\_val=1  
tags: fact2

- name: condition1  
action: sending email here  
when: C != test\_val  
tags: message

- name: condition2

- name: condition3

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 14, 2017, 4:55pm UTC](https://forum.ansible.com/t/abort-executing-playbook-if-condition-not-match/23728/2 "2017-02-14T16:55:35Z")

</div>

> If condition fails it should not go for the next step and exit from there  
> only.
> 
> In following situation, when: C != test\_val it should exit from here  
> only instead of going to the next step. How can i achieve this.
> 
> &nbsp;&nbsp;&nbsp;- name: Set the output of the this state  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_fact: A="{{image\_avl.stdout}}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tags: avl1
> 
> &nbsp;&nbsp;&nbsp;- name: Define our static value for the desired image number  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_fact: B={{retain}}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tags: num2
> 
> &nbsp;&nbsp;&nbsp;- name: difference  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_fact: C = {{A}} - {{B}}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tags: del1

I don't think this will work, you need to have in inside one {{ }} par.  
set\_fact: C = {{ A - B }}

You also might need to filter A and/or B through filter int or float depending on data type.

> &nbsp;&nbsp;&nbsp;- name: set the desire value  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_fact: test\_val=1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tags: fact2
> 
> &nbsp;&nbsp;&nbsp;- name: condition1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action: sending email here  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: C != test\_val  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tags: message

Just add a fail task:

- fail: msg="Failed because {{ C }} not equal to {{ test\_val }}"  
&nbsp;&nbsp;&nbsp;when: C != test\_val

---

<div class="post-metadata">

**Author:** ![DHAVAL\_JAISWAL](https://avatars.discourse-cdn.com/v4/letter/d/34f0e0/32.png) [@DHAVAL\_JAISWAL](https://forum.ansible.com/u/DHAVAL_JAISWAL)\
**Post date:** [February 14, 2017, 5:34pm UTC](https://forum.ansible.com/t/abort-executing-playbook-if-condition-not-match/23728/3 "2017-02-14T17:34:41Z")

</div>

I tried with fail and it works.

Is this works when we set any\_errors\_fatal: true

---

<div class="post-metadata">

**Author:** ![Madhu](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@Madhu](https://forum.ansible.com/u/Madhu)\
**Post date:** [February 15, 2017, 4:24am UTC](https://forum.ansible.com/t/abort-executing-playbook-if-condition-not-match/23728/4 "2017-02-15T04:24:16Z")

</div>

you can also use “assert” and the execution stops right there and it will not execute any tasks further
