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

bool filter wrongly treats nonempty strings/sequences/dicts as 'false' #25893

Closed
pjnagel opened this issue Jun 20, 2017 · 4 comments
Closed

bool filter wrongly treats nonempty strings/sequences/dicts as 'false' #25893

pjnagel opened this issue Jun 20, 2017 · 4 comments
Labels
affects_2.3 This issue/PR affects Ansible v2.3 bug This issue/PR relates to a bug. c:plugins/filter needs_info This issue requires further information. Please answer any outstanding questions.

Comments

@pjnagel
Copy link

pjnagel commented Jun 20, 2017

ISSUE TYPE
  • Bug Report
COMPONENT NAME

builtin filters

ANSIBLE VERSION
ansible 2.3.1.0
  config file = /home/pieter.nagel/setup/lautus_ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 3.5.2 (default, Nov 17 2016, 17:05:23) [GCC 5.4.0 20160609]

CONFIGURATION

Nothing relevant

OS / ENVIRONMENT

Running Ansible on Ubuntu 16.04.2 LTS.

SUMMARY

The bool filter does not reliably cast all true values to 'true'. In particular, nonempty strings, and nonempty sequences, and nonempty dictionaries, are all cast to 'false'.

STEPS TO REPRODUCE
---
- name: debug
  hosts: localhost
  vars:
    v1: stuff
    v2: true
    v3:
      - a
      - b
      - c
    v4:
      a: 1
      b: 2
  tasks:

  - debug: msg='String is true'
    when: 'v1 | bool'

  - debug: msg='True is true'
    when: 'v2 | bool'

  - debug: msg='Nonempty list is true'
    when: 'v3 | bool'

  - debug: msg='Nonempty dict is true'
    when: 'v4 | bool'
EXPECTED RESULTS

I expect to see all four debug statements print their messages.

ACTUAL RESULTS

The only debug statement that is not skipped is 'True is true'.

 [WARNING]: Host file not found: /etc/ansible/hosts

 [WARNING]: provided hosts list is empty, only localhost is available


PLAY [debug] **********************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [debug] **********************************************************************************************************************************************************************************************************************************************************************
skipping: [localhost]

TASK [debug] **********************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "True is true"
}

TASK [debug] **********************************************************************************************************************************************************************************************************************************************************************
skipping: [localhost]

TASK [debug] **********************************************************************************************************************************************************************************************************************************************************************
skipping: [localhost]

PLAY RECAP ************************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0   
@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 20, 2017
@jctanner
Copy link
Contributor

jctanner commented Jun 20, 2017

@pjnagel The implementation for this filter is fairly simple ...

https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/filter/core.py#L117-L125

def to_bool(a):
    ''' return a bool for the arg '''
    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

I think what are you asking for is ...

def to_bool(a):
    ''' return a bool for the arg '''
    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

    # true for anything else that is not length 0
    if len(a) > 0:
        return True
    
return False

Is that correct?

needs_info

@jctanner jctanner removed the needs_triage Needs a first human triage before being processed. label Jun 20, 2017
@sivel
Copy link
Member

sivel commented Jun 20, 2017

There is another pull request to add a "python" truthiness evaluation branch to to_bool that would likely resolve this issue:

#22021

I fear that any change made to the bool filter at this point to accept an empty string as false, would be a backwards incompatible change that could cause problems for existing playbooks.

@ansibot ansibot added the needs_info This issue requires further information. Please answer any outstanding questions. label Jun 20, 2017
@bcoca
Copy link
Member

bcoca commented Jun 20, 2017

The bool filter is not pythonic, it is not meant to be, closing this in favor of the feature request as this currently works as expected.

@bcoca bcoca closed this as completed Jun 20, 2017
@sivel
Copy link
Member

sivel commented Jun 20, 2017

fwiw, as a work around, to get this behavior currently, you can do:

some_var|default(False, boolean=True)|bool

The boolean argument to default tells jinja2 to do a python truthiness check on the var. So default would see "" as falsey and substitute the boolean False.

@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. c:plugins/filter needs_info This issue requires further information. Please answer any outstanding questions.
Projects
None yet
Development

No branches or pull requests

5 participants