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

Not all spellings of boolean "false" in inventory are interpreted as false by Jinja #25784

Closed
fthommen opened this issue Jun 16, 2017 · 8 comments · Fixed by #25798
Closed

Not all spellings of boolean "false" in inventory are interpreted as false by Jinja #25784

fthommen opened this issue Jun 16, 2017 · 8 comments · Fixed by #25798
Labels
affects_2.3 This issue/PR affects Ansible v2.3 bug This issue/PR relates to a bug. support:core This issue/PR relates to code supported by the Ansible Engineering Team.

Comments

@fthommen
Copy link

ISSUE TYPE
  • Bug Report
COMPONENT NAME

inventory
template
jinja

ANSIBLE VERSION
ansible 2.3.0.0
  configured module search path = Default w/o overrides
  python version = 2.7.5 (default, Nov  6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
CONFIGURATION

none special

OS / ENVIRONMENT

CentOS 7 on x86_64

SUMMARY

Boolean variables set in the inventory are not respected by Jinjia as one would expect. According to http://docs.ansible.com/ansible/YAMLSyntax.html e.g. the boolean value false can be written "false", "False", "FALSE" or "no". But only the exact spelling "False" is seen as false by Jinjia, all other spellings are seen as true value.

This is similar to and maybe related to (or the same problem as) #25778.

STEPS TO REPRODUCE
---
- hosts: all
  tasks:
  - shell: "echo \"The switch is {% if switch %}ON{% else %}OFF{% endif %} ({{ switch }})\""
    register: echo

  - debug:
      var: echo

set "switch" to various values in the inventory.

EXPECTED RESULTS

switch=False: The switch is OFF (False)
switch=FALSE: The switch is OFF (FALSE)
switch=false: The switch is OFF (false)
switch=no: The switch is OFF (no)
switch=0: The switch is OFF (0)
switch=true: The switch is ON (true)
switch=blah: (not sure what to expect here, probably an error message)

ACTUAL RESULTS

echo results:

switch=False: The switch is OFF (False)
switch=FALSE: The switch is ON (FALSE)
switch=false: The switch is ON (false)
switch=no: The switch is ON (no)
switch=0: The switch is OFF (0)
switch=true: The switch is ON (true)
switch=blah: The switch is ON (blah)

@ansibot ansibot added affects_2.3 This issue/PR affects Ansible v2.3 bug_report needs_triage Needs a first human triage before being processed. labels Jun 16, 2017
@jctanner jctanner removed the needs_triage Needs a first human triage before being processed. label Jun 16, 2017
@pilou-
Copy link
Contributor

pilou- commented Jun 16, 2017

@fthommen could you specify how you set switch value in the inventory (paste inventory content and filename) ?

I am not able to reproduce this issue defining the switch variable in an YAML file:

$ cat switch.yml
---
switch: FALSE

$ ansible-playbook -i localhost -e@switch.yml test.yml
[...]
"cmd": "echo \"The switch is OFF (False)\"",
[...]

@fthommen
Copy link
Author

I do it in the ansible inventory with switch=FALSE (http://docs.ansible.com/ansible/intro_inventory.html#host-variables)

@pilou-
Copy link
Contributor

pilou- commented Jun 16, 2017

See 25778:

Variables which are not behavioral inventory parameters and are defined in an INI-like inventory are interpreted as strings. This doesn't seem documented, there is an hint in the note here.

@fthommen
Copy link
Author

Thanks @pilou- . Very pity and IMHO somehow inconsistent. In this case - since that seems to be a design decision - please make this issue a documentation issue, not a bug report.

@fthommen
Copy link
Author

btw I then still wonder, why "False" is interpreted as a boolean.... I can't find the notion of a boolean type in the Jinja documentation at all

@bcoca
Copy link
Member

bcoca commented Jun 16, 2017

You need to specify type on 'consumption' we try to conserve it but both jinja2 and the ini format 'stringify everything'.

This should give you more predictable results:
- shell: "echo \"The switch is {% if switch|bool %}ON{% else %}OFF{% endif %} ({{ switch }})\""

@pilou-
Copy link
Contributor

pilou- commented Jun 16, 2017

@fthommen INI parser tries ast.literal_eval for every value and 'stringify' when literal_eval fails.

@bcoca
Copy link
Member

bcoca commented Jun 16, 2017

^ note that ast evals use 'python booleans' unlike jinja2 and yaml which use an extensive list.

pilou- added a commit to pilou-/ansible that referenced this issue Jun 16, 2017
@ansibot ansibot added the support:core This issue/PR relates to code supported by the Ansible Engineering Team. label Jun 29, 2017
thaumos pushed a commit to thaumos/ansible that referenced this issue Jul 13, 2017
…#25798)

* INI inventory: check variable types
* INI inventory: add doc about variable types

Fixes ansible#25784
@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 7, 2018
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.3 This issue/PR affects Ansible v2.3 bug This issue/PR relates to a bug. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants