when: syntax

Dear List,

We are trying this:


######################################

  • hosts: $omgeving
    sudo: true
    vars_files:
  • ~/playbooks/vars/global.yml
  • [ “$vars/${ansible_distribution}.yml”, “$vars/defaults.yml” ]

tasks:

  • include: $basedir/europort/ep_stop_start/tasks/ep_stop.yml
    when: $running == False or $running == None

  • include: $basedir/europort/ep_stop_start/tasks/ep_patch_aix.yml
    when: “aix” in $ansible_hostname

  • include: $basedir/europort/ep_stop_start/tasks/ep_patch_eib.yml
    when: “eib” in $ansible_hostname

  • include: $basedir/europort/ep_stop_start/tasks/finttpci_start.yml
    when: $running == True or $running == None

But none of this is working… Am I going about this the wrong way, or have I simply got the syntax wrong?

Especially the “when: “eib” in $ansible_hostname” bit is troubling. Does the “when:” statement actually support the “in” test with a variable?

Thanks,
Mark

Hi,

First thing is to inspect nice examples here https://github.com/ansible/ansible-examples/blob/master/language_features/conditionals_part2.yml
So, no $ for variables and “is defined” instead of “None” I guess in your case

Regards,
Dima

Ok, so it’s usually best to do what the documentation does and try to be consistent. You’re mixing a bit of 1.1-isms and 1.2-isms. Not understanding what “doesn’t work” means, I’ll err on the side of too much information :slight_smile:

(A)

If you are using “when” you should first make sure you are on 1.2, as when is a 1.2 feature.

It will raise an error and “not work” when you are using less than 1.2

(B)

If on 1.2, it’s best to be consistent.

Don’t use $foo in when expressions, as the whole point of 1.2 is to eliminate the need for the legacy ${perl_like} syntaxes

With when, all you have to do is say:

when: foo == 2

And so on.

If you put $foo in a when statement, it’s going to evaluate that variable to a string and lose it’s type information. You definitely don’t want that in the case where you are passing in a list or integer something.

(C)

If you want to use in, you are probably starting your YAML expression with a string and have to quote the whole line to keep YAML happy:

when: ‘“asdf” in bar’

(D)

To avoid weird quoting from having to start the line, this is often just as nice:

when: bar.find(“asdf”) != -1

Hope that helps!

Hi Michael,

Yeah, you are right, it was a baaaaaad weekend, and it impaired my reasoning and questioning skills.

Anyhow, your exemplary reply basicly cleared away the fog now :wink:

Thanks!
Mark