How to use extra vars with fail module

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

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

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

Hi,

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

--P

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:

I would do it like this:

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

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

One of my favorite parts of discrete math!

http://en.wikipedia.org/wiki/De_Morgan’s_laws