# How to use extra vars with fail module

**URL:** <https://forum.ansible.com/t/how-to-use-extra-vars-with-fail-module/13511>\
**Category:** Ansible Project\
**Created:** [September 20, 2013, 9:59am UTC](https://forum.ansible.com/t/how-to-use-extra-vars-with-fail-module/13511 "2013-09-20T09:59:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Edgars](https://avatars.discourse-cdn.com/v4/letter/e/f17d59/32.png) [@Edgars](https://forum.ansible.com/u/Edgars)\
**Post date:** [September 20, 2013, 9:59am UTC](https://forum.ansible.com/t/how-to-use-extra-vars-with-fail-module/13511/1 "2013-09-20T09:59:44Z")

</div>

Hi

I have:

tasks:

- name: Check if dbid is set correctly  
action: fail msg=“Please set correct dbid variable”  
when: dbid is not defined or dbid != 1 or dbid != 2

I run:

ansible-playbook test.yml --extra-vars “dbid=1”

And always get:

TASK: [Check if dbid is set correctly] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
failed: [localhost] =\> {“failed”: true}  
msg: Please set correct dbid variable

FATAL: all hosts have already failed – aborting

What is the problem here?

Edgars

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [September 20, 2013, 10:07am UTC](https://forum.ansible.com/t/how-to-use-extra-vars-with-fail-module/13511/2 "2013-09-20T10:07:17Z")

</div>

​Not sure which logic you need, but shouldn't this be:​

​​when: dbid is not defined or ( dbid != 1 and dbid != 2 )

---

<div class="post-metadata">

**Author:** ![Petr\_Svoboda](https://avatars.discourse-cdn.com/v4/letter/p/e19b73/32.png) [@Petr\_Svoboda](https://forum.ansible.com/u/Petr_Svoboda)\
**Post date:** [September 20, 2013, 10:09am UTC](https://forum.ansible.com/t/how-to-use-extra-vars-with-fail-module/13511/3 "2013-09-20T10:09:24Z")

</div>

Hi,

&nbsp;&nbsp;you need to fix you condition: 1 != 1 or 1 != 2 is actualy true. It  
probably should be dbib != 1 and dbib != 2.

--P

---

<div class="post-metadata">

**Author:** ![Edgars](https://avatars.discourse-cdn.com/v4/letter/e/f17d59/32.png) [@Edgars](https://forum.ansible.com/u/Edgars)\
**Post date:** [September 20, 2013, 10:13am UTC](https://forum.ansible.com/t/how-to-use-extra-vars-with-fail-module/13511/4 "2013-09-20T10:13:58Z")

</div>

Hi

Unfortunately your example does not work either.

What I need is that dbid must be defined and must have values 1 or 2.

Edgars

piektdiena, 2013. gada 20. septembris 12:07:17 UTC+2, Serge van Ginderachter rakstīja:

---

<div class="post-metadata">

**Author:** ![John\_Jarvis](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@John\_Jarvis](https://forum.ansible.com/u/John_Jarvis)\
**Post date:** [September 20, 2013, 1:56pm UTC](https://forum.ansible.com/t/how-to-use-extra-vars-with-fail-module/13511/5 "2013-09-20T13:56:07Z")

</div>

I would do it like this:

when: dbid is not defined or not ( dbid == 1 or dbid == 2 )

---

<div class="post-metadata">

**Author:** ![James\_Cammarata1](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@James\_Cammarata1](https://forum.ansible.com/u/James_Cammarata1)\
**Post date:** [September 20, 2013, 2:29pm UTC](https://forum.ansible.com/t/how-to-use-extra-vars-with-fail-module/13511/6 "2013-09-20T14:29:06Z")

</div>

And one more way:

- fail: msg=“Please set correct dbid variable”  
when: dbid is not defined or dbid not in (‘1’,‘2’)

Output:

$ ansible-playbook test\_logic.yml -e “dbid=1”

TASK: [fail msg=“Please set correct dbid variable”] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: [127.0.0.1]

$ ansible-playbook test\_logic.yml -e “dbid=2”

TASK: [fail msg=“Please set correct dbid variable”] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: [127.0.0.1]

$ ansible-playbook test\_logic.yml -e “dbid=3”

TASK: [fail msg=“Please set correct dbid variable”] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
failed: [127.0.0.1] =\> {“failed”: true}  
msg: Please set correct dbid variable

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 21, 2013, 1:06pm UTC](https://forum.ansible.com/t/how-to-use-extra-vars-with-fail-module/13511/7 "2013-09-21T13:06:06Z")

</div>

One of my favorite parts of discrete math!

[http://en.wikipedia.org/wiki/De\_Morgan’s\_laws](http://en.wikipedia.org/wiki/De_Morgan's_laws)
