Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend boolean values section in playbooks_variables #1285

Open
wants to merge 10 commits into
base: devel
Choose a base branch
from
71 changes: 64 additions & 7 deletions docs/docsite/rst/playbook_guide/playbooks_variables.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,76 @@ Boolean variables
=================

Ansible accepts a broad range of values for boolean variables: ``true/false``, ``1/0``, ``yes/no``, ``True/False`` and so on. The matching of valid strings is case insensitive.
While documentation examples focus on ``true/false`` to be compatible with ``ansible-lint`` default settings, you can use any of the following:
Documentation examples focus on ``true/false`` to be compatible with ``ansible-lint`` default settings. However, you can use any of the following values:

Native Boolean
oraNod marked this conversation as resolved.
Show resolved Hide resolved
skilyazhnev marked this conversation as resolved.
Show resolved Hide resolved
--------------

Ansible treats these values as native booleans:

.. table::
:class: documentation-table

=============================================================================================== ====================================================================
Valid values Description
=============================================================================================== ====================================================================
``True`` , ``'true'`` , ``'t'`` , ``'yes'`` , ``'y'`` , ``'on'`` , ``'1'`` , ``1`` , ``1.0`` Truthy values
================= =================
Valid values Description
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not really 'ansible native' but YAML that uses true and false and the rest of the list below, it would be 'more true' that True and False are the 'native' options as Ansible evaluation happens inside Python and those are the 'Python native booleans', this also depends on context as inside a {{ }} expression it is Jinja doing the evaluation, which is again using Python under the hood.

================= =================
``true`` Truthy values

``false`` Falsy values

================= =================

Implicit boolean
----------------

Ansible recognizes these values as booleans:

.. table::
:class: documentation-table

================================================================================= ====================================================================
Valid values Description
================================================================================= ====================================================================
``yes`` , ``Yes`` , ``YES`` , ``True`` , ``TRUE`` , ``on`` , ``On`` , ``ON`` Truthy values

``no`` , ``No`` , ``NO`` , ``False`` , ``FALSE`` , ``off`` , ``Off`` , ``OFF`` Falsy values

================================================================================= ====================================================================

Interpretable as Boolean
skilyazhnev marked this conversation as resolved.
Show resolved Hide resolved
------------------------

``False`` , ``'false'`` , ``'f'`` , ``'no'`` , ``'n'`` , ``'off'`` , ``'0'`` , ``0`` , ``0.0`` Falsy values
You need to use the ``| bool`` filter to interpret these strings values as native booleans in logical expressions. Boolean expressions also work with these values without a filter, but Ansible interprets them in Python style.

=============================================================================================== ====================================================================
.. table::
Copy link
Member

@bcoca bcoca Jun 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the bool filter, while is it Jinja, it is an Ansible customization which approximates (but does not really match) the YAML booleans, the table below is incorrect as this is the actual code:

    if a is None or isinstance(a, bool):
        return a
    if isinstance(a, string_types):
        a = a.lower()
    if a in ('yes', 'on', '1', 'true', 1):
        return True
    return False

so it realy only converts 'matching True' and everything else is False

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

though this is something I'm planning to fix so they are closer to YAML by default but can be 'pythonic' otherwise.

:class: documentation-table

================================================================================= ====================================================================
Valid values Description
================================================================================= ====================================================================
``'true'`` , ``'t'`` , ``'yes'`` , ``'y'`` , ``'on'`` , ``'1'`` , ``1`` Truthy values

``'false'`` , ``'f'`` , ``'no'`` , ``'n'`` , ``'off'`` , ``'0'`` , ``0`` Falsy values

================================================================================= ====================================================================

oraNod marked this conversation as resolved.
Show resolved Hide resolved
For example, using ``'false'`` value:
skilyazhnev marked this conversation as resolved.
Show resolved Hide resolved

.. code-block:: yaml

- hosts: web_servers
vars:
condition_var: 'false'
tasks:
- name: This task will run
debug:
msg: "{{ condition_var }}"
when: condition_var

- name: This task will be skipped
debug:
msg: "{{ condition_var }}"
when: condition_var | bool

.. _list_variables:

Expand Down