Boolean evaluation fails on variables set via inventory

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

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

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

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.

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]

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.

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?

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