diff --git a/lib/ansible/plugins/filter/core.py b/lib/ansible/plugins/filter/core.py index a082590557d..f9f9da73a00 100644 --- a/lib/ansible/plugins/filter/core.py +++ b/lib/ansible/plugins/filter/core.py @@ -98,6 +98,7 @@ _valid_bool_false = {'no', 'off', 'false', '0'} def to_bool(value: object) -> bool: """Convert well-known input values to a boolean value.""" value_to_check: object + if isinstance(value, str): value_to_check = value.lower() # accept mixed case variants elif isinstance(value, int): # bool is also an int @@ -105,14 +106,17 @@ def to_bool(value: object) -> bool: else: value_to_check = value - if value_to_check in _valid_bool_true: - return True + try: + if value_to_check in _valid_bool_true: + return True - if value_to_check in _valid_bool_false: - return False + if value_to_check in _valid_bool_false: + return False - # if we're still here, the value is unsupported- always fire a deprecation warning - result = value_to_check == 1 # backwards compatibility with the old code which checked: value in ('yes', 'on', '1', 'true', 1) + # if we're still here, the value is unsupported- always fire a deprecation warning + result = value_to_check == 1 # backwards compatibility with the old code which checked: value in ('yes', 'on', '1', 'true', 1) + except TypeError: + result = False # NB: update the doc string to reflect reality once this fallback is removed display.deprecated( diff --git a/test/integration/targets/filter_core/tasks/main.yml b/test/integration/targets/filter_core/tasks/main.yml index 7d7590ecea7..e812eec6ac6 100644 --- a/test/integration/targets/filter_core/tasks/main.yml +++ b/test/integration/targets/filter_core/tasks/main.yml @@ -366,6 +366,7 @@ - (1.0|bool)==true - (0.0|bool)==false - (7|bool)==false + - ({}|bool)==false - name: Verify to_datetime assert: