Skip to content

[{}] | map(attribute = "foo", default = None) still fails #2165

Description

@westhand

According to the documentation, when using attribute in map instead of a filter, if the value of attribute is not a key of an input element, default can be specified to supply a value instead. In the past, that would fail if that value could be cast to False (#1331). I have no idea if that is some sort of regression, or if None was never tested against.

This seems indeed to work for most of those values, except for None. I have no jinja executable and no idea how to invoke jinja, so here is an example with ansible, with an error message being identical as if default had not been specified at all, as seen in the last meaningful line:

./venv/latest/bin/ansible -m debug -a 'msg={{[{}]|map(attribute="foo",default=none)}}' localhost
[ERROR]: Task failed: Finalization of task args for 'ansible.builtin.debug' failed: Error while resolving value for 'msg': object of type 'dict' has no attribute 'foo'
 
Task failed.
Origin: <adhoc 'debug' task>
 
{'action': 'debug', 'args': {'msg': '{{[{}]|map(attribute="foo",default=none)}}'}, 'timeout': 0, 'async_val': 0, [...]
 
<<< caused by >>>
 
Finalization of task args for 'ansible.builtin.debug' failed.
Origin: <CLI option '-m'>
 
debug
 
<<< caused by >>>
 
Error while resolving value for 'msg': object of type 'dict' has no attribute 'foo'
Origin: <CLI option '-a'>
 
{{[{}]|map(attribute="foo",default=none)}}
 
localhost | FAILED! => {
    "changed": false,
    "msg": "Task failed: Finalization of task args for 'ansible.builtin.debug' failed: Error while resolving value for 'msg': object of type 'dict' has no attribute 'foo'"
}

Environment:

  • Python version: 3.13.5
  • Jinja version: 3.1.6

I believe this results from how prepare_map() handles kwargs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions