# Boolean evaluation fails on variables set via inventory

**URL:** <https://forum.ansible.com/t/boolean-evaluation-fails-on-variables-set-via-inventory/13562>\
**Category:** Ansible Project\
**Created:** [September 26, 2013, 9:21pm UTC](https://forum.ansible.com/t/boolean-evaluation-fails-on-variables-set-via-inventory/13562 "2013-09-26T21:21:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ross\_Becker](https://avatars.discourse-cdn.com/v4/letter/r/ed8c4c/32.png) [@Ross\_Becker](https://forum.ansible.com/u/Ross_Becker)\
**Post date:** [September 26, 2013, 9:21pm UTC](https://forum.ansible.com/t/boolean-evaluation-fails-on-variables-set-via-inventory/13562/1 "2013-09-26T21:21:20Z")

</div>

I’ve verified this in both ansible 1.2.2 and 1.3.0; any variable I set in an inventory file cannot be evaluated as a variable, which is a pretty big caveat the docs don’t mention. I can only evaluate variables as booleans if they are set in the playbooks.

====== inventory file =======  
[all:vars]  
epic=False

[configurator]  
127.0.0.1

=======playbook========

- hosts: configurator  
user: root  
gather\_facts: False

tasks:

- name: Boolean test  
debug: msg=“This is so epic!”  
when: epic

---

<div class="post-metadata">

**Author:** ![James\_Cammarata1](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@James\_Cammarata1](https://forum.ansible.com/u/James_Cammarata1)\
**Post date:** [September 26, 2013, 9:29pm UTC](https://forum.ansible.com/t/boolean-evaluation-fails-on-variables-set-via-inventory/13562/2 "2013-09-26T21:29:00Z")

</div>

Use “when: epic|bool”, to ensure the variable is cast as a boolean - otherwise it is evaluated as a string, which is always true.

---

<div class="post-metadata">

**Author:** ![Ross\_Becker](https://avatars.discourse-cdn.com/v4/letter/r/ed8c4c/32.png) [@Ross\_Becker](https://forum.ansible.com/u/Ross_Becker)\
**Post date:** [September 26, 2013, 9:58pm UTC](https://forum.ansible.com/t/boolean-evaluation-fails-on-variables-set-via-inventory/13562/3 "2013-09-26T21:58:38Z")

</div>

Thanks! Works in ansible 1.3, notsomuch in 1.2.2, but at least it’s solveable.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [September 26, 2013, 10:17pm UTC](https://forum.ansible.com/t/boolean-evaluation-fails-on-variables-set-via-inventory/13562/4 "2013-09-26T22:17:32Z")

</div>

It only affects setting them in the ini files, works in yaml(host\_vars/group\_vars).

I need to test but I assume it also works in the json returned from inventory scripts.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [September 26, 2013, 10:24pm UTC](https://forum.ansible.com/t/boolean-evaluation-fails-on-variables-set-via-inventory/13562/5 "2013-09-26T22:24:33Z")

</div>

try this, might fix the issue (also enable passing lists/dicts)

— a/lib/ansible/inventory/ini.py  
+++ b/lib/ansible/inventory/ini.py  
@@ -25,6 +25,7 @@ from ansible.inventory.expand\_hosts import expand\_hostname\_range  
from ansible import errors  
import shlex  
import re  
+import ast

class InventoryParser(object):  
“”"  
@@ -116,7 +117,7 @@ class InventoryParser(object):  
(k,v) = t.split(“=”)  
except ValueError, e:  
raise errors.AnsibleError(“Invalid ini entry: %s - %s” % (t, str(e)))

- host.set\_variable(k,v)

- host.set\_variable(k,ast.literal\_eval(v))  
self.groups[active\_group\_name].add\_host(host)

# [southeast:children]

---

<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:** [September 26, 2013, 10:28pm UTC](https://forum.ansible.com/t/boolean-evaluation-fails-on-variables-set-via-inventory/13562/6 "2013-09-26T22:28:09Z")

</div>

Set your variables in host\_vars and group\_vars instead and types are preserved.

I don’t want to do ast evals and we are likely to discourage keeping variables in the mainline INI.

---

<div class="post-metadata">

**Author:** ![mickours](https://avatars.discourse-cdn.com/v4/letter/m/b3f665/32.png) [@mickours](https://forum.ansible.com/u/mickours)\
**Post date:** [November 26, 2015, 9:45am UTC](https://forum.ansible.com/t/boolean-evaluation-fails-on-variables-set-via-inventory/13562/7 "2015-11-26T09:45:20Z")

</div>

Hi, sorry for necro bumping this but I think that have variable in inventory files are necessary in some use case and that boolean should be working in it.

For example I have to environments (and to inventory files) one with a proxy the other without. To associate the proxy to the environment I add a use\_proxy=true in my inventory file. If I put it in group\_vars I will have to change it each time I switch environment.

So if this use case make sens to you I think the variable should be properly evaluated, don’t you?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [November 27, 2015, 9:03pm UTC](https://forum.ansible.com/t/boolean-evaluation-fails-on-variables-set-via-inventory/13562/8 "2015-11-27T21:03:03Z")

</div>

@mickours, not sure what you are stating, but the inventory has for a  
long time included code similar to the sample you are quoting.
