How Exactly Does Ansible Parse Boolean Variables?

Variables defined in YAML files (playbooks, vars_files, YAML-format inventories)


YAML principles

Playbooks, vars_files, and inventory files written in YAML are processed by a YAML parser first. It allows several aliases for values which will be stored as Boolean type: yes/no, true/false, on/off, defined in several cases: true/True/TRUE (thus they are not truly case-insensitive).

YAML definition specifies possible values as:

y|Y|yes|Yes|YES|n|N|no|No|NO
|true|True|TRUE|false|False|FALSE
|on|On|ON|off|Off|OFF

Ansible docs confirm that:

You can also specify a boolean value (true/false) in several forms:

create_key: yes
needs_agent: no
knows_oop: True
likes_emacs: TRUE
uses_cvs: false

However, Ansible documentation now also states:

Use lowercase ‘true’ or ‘false’ for boolean values in dictionaries if you want to be compatible with default yamllint options.

So keep that in mind if you use tools like ansible-lint, that will not be happy by default (Truthy value should be one of [false, true])



Variables defined in INI-format inventory files


Python principles

When Ansible reads an INI-format inventory, it processes the variables using Python built-in types:

Values passed in using the key=value syntax are interpreted as Python literal structure (strings, numbers, tuples, lists, dicts, booleans, None), alternatively as string. For example var=FALSE would create a string equal to FALSE.

If the value specified matches string True or False (starting with a capital letter) the type is set to Boolean, otherwise it is treated as string (unless it matches another type).



Variables defined through --extra_vars CLI parameter


All strings

All variables passed as extra-vars in CLI are of string type. You can work around that by using JSON syntax. Example:

--extra-vars '{"abc": false}'

abc will then be of type bool.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)