{"record":{"id":"3bc95d27cd542a15","repo":"ansible/ansible","slug":"conditional-result-bool-result-was-derived-fro","errorCode":null,"errorMessage":"Conditional result ({bool_result}) was derived from value of type {native_type_name(result)!r} at {str(result_origin)!r}. Conditionals must have a boolean result.","messagePattern":"Conditional result \\((.+?)\\) was derived from value of type (.+?) at (.+?)\\. Conditionals must have a boolean result\\.","errorType":"exception","errorClass":"AnsibleBrokenConditionalError","httpStatus":null,"severity":"error","filePath":"lib/ansible/_internal/_templating/_engine.py","lineNumber":591,"sourceCode":"\n        result_origin = Origin.get_tag(result) or Origin.UNKNOWN\n\n        msg = (\n            f'Conditional result ({bool_result}) was derived from value of type {native_type_name(result)!r} at {str(result_origin)!r}. '\n            'Conditionals must have a boolean result.'\n        )\n\n        if _TemplateConfig.allow_broken_conditionals:\n            _display.deprecated(\n                msg=msg,\n                obj=conditional,\n                help_text=self._BROKEN_CONDITIONAL_ALLOWED_FRAGMENT,\n                version='2.23',\n            )\n\n            return bool_result\n\n        raise AnsibleBrokenConditionalError(msg, obj=conditional)\n\n    @staticmethod\n    def _trust_check(value: str, skip_handler: bool = False) -> bool:\n        \"\"\"\n        Return True if the given value is trusted for templating, otherwise return False.\n        When the value is not trusted, a warning or error may be generated, depending on configuration.\n        \"\"\"\n        if TrustedAsTemplate.is_tagged_on(value):\n            return True\n\n        if not skip_handler:\n            with Skippable, _TemplateConfig.untrusted_template_handler.handle(TemplateTrustCheckFailedError, skip_on_ignore=True):\n                raise TemplateTrustCheckFailedError(obj=value)\n\n        return False\n","sourceCodeStart":573,"sourceCodeEnd":607,"githubUrl":"https://github.com/ansible/ansible/blob/9cf16a4aca7898481c257f1e17ad28d0b67b1f85/lib/ansible/_internal/_templating/_engine.py#L573-L607","documentation":"AnsibleBrokenConditionalError raised by evaluate_conditional() when the conditional's evaluated result is not a bool. The message reports the coerced truthiness (bool_result), the native type of the result, and the origin-tagged location of the result value, then rejects non-boolean conditionals. With allow_broken_conditionals the truthy value is accepted with a deprecation warning instead.","triggerScenarios":"A `when:` expression that evaluates to a string/number/list instead of a bool, e.g. `when: \"{{ result.stdout }}\"` (string), `when: \"{{ items }}\"` (list), or `when: count` where count is an int. Any truthy non-bool result after templating triggers it.","commonSituations":"Migrating from older ansible-core that accepted truthy strings ('yes', 'on', any non-empty string) as True; conditionals returning module output directly; using `| default(...)` with string defaults; vars defined as 'no'/'yes' strings that no longer coerce.","solutions":["Make the comparison explicit: `when: \"{{ result.stdout | length > 0 }}\"` or add `| bool`.","Define the source variable as a real boolean in YAML (true/false), not a quoted string.","For list checks use `| length > 0`; for strings use explicit comparisons or `| bool`.","Temporarily enable allow_broken_conditionals to locate all offenders via deprecation warnings, then fix each before 2.23."],"exampleFix":"# before\nwhen: \"{{ result.stdout }}\"\n\n# after\nwhen: \"{{ result.stdout | trim | length > 0 }}\"","handlingStrategy":"type-guard","validationCode":"# preflight in playbook: assert the conditional input is boolean-shaped\n- assert:\n    that: flag | type_debug in ['bool', 'AnsibleUnicode', 'str']\n  when: flag is defined","typeGuard":"from ansible._internal._errors import _messages  # controller-side helper\ndef yields_boolean(templar, cond) -> bool:\n    \"\"\"Template the conditional and verify the result type before evaluation.\"\"\"\n    val = templar.template(cond, mode=templar._TemplateMode_DEFAULT) if isinstance(cond, str) else cond\n    return isinstance(val, bool)","tryCatchPattern":"try:\n    ok = templar.evaluate_conditional(cond)\nexcept AnsibleBrokenConditionalError as ex:\n    if 'Conditionals must have a boolean result' in str(ex):\n        ok = templar.evaluate_conditional('{{ (%s) | bool }}' % cond)\n    else:\n        raise","preventionTips":["End every when: expression in an explicit comparison or `| bool`.","Define booleans unquoted (true/false) in YAML.","Migrate via allow_broken_conditionals deprecation warnings to find all offenders.","Ban bare variable references in when: in lint rules (ansible-lint 'bare-variable' style)."],"tags":["ansible","conditionals","boolean-coercion","when"],"backgroundTag":null,"analyzedSha":"9cf16a4aca7898481c257f1e17ad28d0b67b1f85","analyzedAt":"2026-08-15T00:15:47.100Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}