Conditonally run task based on wether a variable has been set

Hi all,

This should be simple to solve: I want run a task only if a variable is set (by providing the variable on the command line). How best to do this?

The following doesn’t work:

  • name: Set mysql root password
    mysql_user: name=root password={{ mysql_pw }}
    when: “mysql_pw”

I get the error: ‘Conditional expression must evaluate to True or False’. Surely an empty string evaluates to false?

Nor does:

  • name: Set mysql root password
    mysql_user: name=root password={{ mysql_pw }}
    when: “mysql_pw != ‘’”

…which always runs whether irrespective of whether the variable mysql_pw is set.

Any other ideas?

Thanks,
Torrance

Hi Torrance,

try

when: mysql_pw is defined

Benno, thank you! That did the trick :slight_smile: