From c8324aa01a4c96073a80b9a3f67cfb0193333698 Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Wed, 11 Jun 2025 11:18:12 -0700 Subject: [PATCH] Fix bool filter for non-hashable types (#85300) --- lib/ansible/plugins/filter/core.py | 16 ++++++++++------ .../targets/filter_core/tasks/main.yml | 1 + 2 files changed, 11 insertions(+), 6 deletions(-) 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: