Skip to content

Filter using dataclasses.asdict triggers _lazy_containers.UnsupportedConstructionMethodError #86947

@hansmi

Description

@hansmi

Summary

With ansible-core 2.20 a filter plugin using Python's dataclasses module with caller-provided values raises UnsupportedConstructionMethodError ("Direct construction of lazy containers is not supported.") for lists and dictionaries.

Similar to #85606.

Issue Type

Bug Report

Component Name

_lazy_containers

Ansible Version

$ ansible --version
ansible [core 2.20.5]
  config file = None
  configured module search path = ['<redacted>/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.13/dist-packages/ansible
  ansible collection location = <redacted>/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.13.5 (main, Jun 25 2025, 18:55:22) [GCC 14.2.0] (/usr/bin/python3)
  jinja version = 3.1.6
  pyyaml version = 6.0.3 (with libyaml v0.2.5)

Configuration

# if using a version older than ansible-core 2.12 you should omit the '-t all'
$ ansible-config dump --only-changed -t all
CONFIG_FILE() = None
EDITOR(env: EDITOR) = /usr/bin/nano
PAGER(env: PAGER) = /usr/bin/less

GALAXY_SERVERS:

OS / Environment

Debian 13 (Trixie) with Ansible installed via Python pip.

Steps to Reproduce

Playbook:

- hosts: localhost
  gather_facts: false
  connection: local
  vars:
    literal: hello world
    mylist:
      - a
      - b
    mydict:
      first: hello
      second: world
  tasks:
    # Working
    - debug: msg="{{ literal | bug }}"

    # Failing
    - debug: msg="{{ mylist | bug }}"
    - debug: msg="{{ mydict | bug }}"

filter_plugins/test.py:

import dataclasses

@dataclasses.dataclass()
class _Data:
    value: dict

def bug(value):
    return dataclasses.asdict(_Data(value=value))

class FilterModule:
    def filters(self):
        return {"bug": bug}

Expected Results

ok: [localhost] => {
    "msg": {
        "value": "hello world"
    }
}

ok: [localhost] => {
    "msg": {
        "value": ["a", "b"]
    }
}

ok: [localhost] => {
    "msg": {
        "value": {"first": "hello", "second": "world"}
    }
}

Actual Results

PLAY [localhost] **********************************************************************************************************************************************

TASK [debug] **************************************************************************************************************************************************
ok: [localhost] => {
    "msg": {
        "value": "hello world"
    }
}

TASK [debug] **************************************************************************************************************************************************
[ERROR]: Task failed: Finalization of task args for 'ansible.builtin.debug' failed: Error while resolving value for 'msg': The filter plugin 'bug' failed: Direct construction of lazy containers is not supported.

Task failed: Finalization of task args for 'ansible.builtin.debug' failed.
Origin: <redacted>/temp/ansible-lazy-direct-construction/playbook.yml:17:7

15
16     # Failing
17     - debug: msg="{{ mylist | bug }}"
         ^ column 7

<<< caused by >>>

Error while resolving value for 'msg': The filter plugin 'bug' failed: Direct construction of lazy containers is not supported.
Origin: <redacted>/temp/ansible-lazy-direct-construction/playbook.yml:17:14

15
16     # Failing
17     - debug: msg="{{ mylist | bug }}"
                ^ column 14

fatal: [localhost]: FAILED! => {"msg": "Task failed: Finalization of task args for 'ansible.builtin.debug' failed: Error while resolving value for 'msg': The filter plugin 'bug' failed: Direct construction of lazy containers is not supported."}

PLAY RECAP ****************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

Code of Conduct

  • I agree to follow the Ansible Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    affects_2.20bugThis issue/PR relates to a bug.needs_verifiedThis issue needs to be verified/reproduced by maintainer

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions