using host variables as condition

Hello,
I am trying to do some special jobs only for some hosts.
My setup until now: One playbook, which creates dynamic groups and includes different roles based on the dynamic groups (e.g. common for all hosts, or Suse-11-x86_64)

This Suse-role should now have an include like this:

  • include: sap-appl.yml tags=sap-appl
    when: sap_appli == “yes”

The variable sap_appli is self-defined for every host. I tried 2 methods
a) defining it inside the inventory, e.g.

myHost001 sap_appli=yes

b) writing host_vars in a seperate folder

cat host_vars/myHost001
sap_appli: yes

Both methods have the same effect. It works for hosts with sap_appli defined. But if this variable is not defined, the playbook-run crashes:

fatal: [gisu951] => Conditional expression must evaluate to True or False: (True) and ({% if sap_appli == “yes” %} True {% else %} False {% endif %})

Is it possible to define a default value?
In my case now:

sap_appli=no

Using a file host_vars/all doesn’t have any effect. Including an special vars_file with the default value overwrites every host-defined variable.

Thanks for your help,
best regards
Alex

Try:

when: sap_appli is defined and sap_appli == "yes"

See if that will handle it correctly?

-jlk

If you define your variables in your “host_vars/foo” file instead of in the INI file, it’s possible for them to keep boolean types too!

Thank you, this really solved my problem.

foo: yes

in YAML will produce a boolean, so don’t check against “yes”, check truth.

when: sap_appli is defined and sap_appli

A default can be obtained by the |default filter in Jinja2 if you want.