Abort executing playbook if condition not match

25 views
Skip to first unread message

bablu

unread,
Feb 14, 2017, 11:43:30 AM2/14/17
to Ansible Project

 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
 
  

Kai Stian Olstad

unread,
Feb 14, 2017, 11:55:47 AM2/14/17
to ansible...@googlegroups.com
On 14. feb. 2017 17:43, bablu wrote:
>
> 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

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.

>
> - name: set the desire value
> set_fact: test_val=1
> tags: fact2
>
> - name: condition1
> action: sending email here
> when: C != test_val
> tags: message

Just add a fail task:

- fail: msg="Failed because {{ C }} not equal to {{ test_val }}"
when: C != test_val

>
> - name: condition2
>
> - name: condition3

--
Kai Stian Olstad

DHAVAL JAISWAL

unread,
Feb 14, 2017, 12:34:46 PM2/14/17
to ansible...@googlegroups.com
I tried with fail and it works.

Is this works when we set  any_errors_fatal: true




--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/4FYx5Pj-VlQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/47edc3a5-3727-e0af-e699-a8e339f42961%40olstad.com.

For more options, visit https://groups.google.com/d/optout.


Madhusudhanan Santhanam

unread,
Feb 15, 2017, 7:19:12 AM2/15/17
to Ansible Project
you can also use "assert" and the execution stops right there and it will not execute any tasks further
Reply all
Reply to author
Forward
0 new messages