# using host variables as condition

**URL:** <https://forum.ansible.com/t/using-host-variables-as-condition/13227>\
**Category:** Ansible Project\
**Created:** [July 31, 2013, 4:58am UTC](https://forum.ansible.com/t/using-host-variables-as-condition/13227 "2013-07-31T04:58:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![work](https://avatars.discourse-cdn.com/v4/letter/w/e99b99/32.png) [@work](https://forum.ansible.com/u/work)\
**Post date:** [July 31, 2013, 4:58am UTC](https://forum.ansible.com/t/using-host-variables-as-condition/13227/1 "2013-07-31T04:58:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jesse\_Keating](https://avatars.discourse-cdn.com/v4/letter/j/3bc359/32.png) [@Jesse\_Keating](https://forum.ansible.com/u/Jesse_Keating)\
**Post date:** [July 31, 2013, 5:51pm UTC](https://forum.ansible.com/t/using-host-variables-as-condition/13227/2 "2013-07-31T17:51:37Z")

</div>

Try:

when: sap\_appli is defined and sap\_appli == "yes"

See if that will handle it correctly?

-jlk

---

<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:** [July 31, 2013, 6:41pm UTC](https://forum.ansible.com/t/using-host-variables-as-condition/13227/3 "2013-07-31T18:41:18Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![work](https://avatars.discourse-cdn.com/v4/letter/w/e99b99/32.png) [@work](https://forum.ansible.com/u/work)\
**Post date:** [August 2, 2013, 1:39pm UTC](https://forum.ansible.com/t/using-host-variables-as-condition/13227/4 "2013-08-02T13:39:47Z")

</div>

Thank you, this really solved my problem.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [August 2, 2013, 2:00pm UTC](https://forum.ansible.com/t/using-host-variables-as-condition/13227/5 "2013-08-02T14:00:38Z")

</div>

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.
