Skip to content

Object of type HostVarsVars is not JSON serializable #1137

Open

Description

@pinkbubba

I'm getting error

[WARNING]: Failure using method (v2_runner_on_ok) in callback plugin
(<ansible.plugins.callback.awx_display.CallbackModule object at
0x7f54a8551c70>): Object of type HostVarsVars is not JSON serializable

when running the below playbook in AWX (ansible-runner)

- name: test 
  hosts: localhost
  tasks:
    - debug: 
        msg: 
         - "{{ item.inventory_hostname}}"
      loop: "{{ ansible_play_hosts
        | default([])
        | map('extract', hostvars) 
        | list 
        }}"

ansible-runner --version
2.1.4.dev2

python --version
Python 3.8.13

Activity

  1. pinkbubba commented on Sep 29, 2022

    @pinkbubba
    Author
    Callback Exception: 
      File "/usr/local/lib/python3.8/site-packages/ansible/executor/task_queue_manager.py", line 450, in send_callback
        method(*new_args, **kwargs)
       File "/usr/local/lib/python3.8/site-packages/ansible_runner/display_callback/module.py", line 495, in v2_runner_item_on_ok
        with self.capture_event_data('runner_item_on_ok', **event_data):
       File "/usr/lib64/python3.8/contextlib.py", line 113, in __enter__
        return next(self.gen)
       File "/usr/local/lib/python3.8/site-packages/ansible_runner/display_callback/module.py", line 100, in capture_event_data
        event_context.dump_begin(sys.stdout)
       File "/usr/local/lib/python3.8/site-packages/ansible_runner/display_callback/events.py", line 197, in dump_begin
        self.cache.set(":1:ev-{}".format(begin_dict['uuid']), begin_dict)
       File "/usr/local/lib/python3.8/site-packages/ansible_runner/display_callback/events.py", line 72, in set
        partial_data = json.dumps(value, cls=AnsibleJSONEncoderLocal)
       File "/usr/lib64/python3.8/json/__init__.py", line 234, in dumps
        return cls(
       File "/usr/lib64/python3.8/json/encoder.py", line 199, in encode
        chunks = self.iterencode(o, _one_shot=True)
       File "/usr/lib64/python3.8/json/encoder.py", line 257, in iterencode
        return _iterencode(o, 0)
       File "/usr/local/lib/python3.8/site-packages/ansible_runner/display_callback/events.py", line 52, in default
        return super(AnsibleJSONEncoderLocal, self).default(o)
       File "/usr/lib64/python3.8/json/encoder.py", line 179, in default
        raise TypeError(f'Object of type {o.__class__.__name__}
    
  2. eqrx commented on Oct 4, 2022

    @eqrx
    Contributor

    @shanemcd @AlanCoding any thoughts on that?

  3. removed
    needs_triageNew item that needs to be triaged
    on Oct 4, 2022
  4. AlanCoding commented on Oct 20, 2022

    @AlanCoding
    Member

    I ran that playbook, and I can't get it to give any error. Could you provide more details to reproduce the issue?

    With the thing that it's looping over, it's likely sensitive to the contents of your inventory, or maybe the adjacent host_vars/ folder (vars plugin). I don't doubt that you might have hit this with some given inventory content, but I would be guessing to try to figure out how.

  5. sivel commented on Oct 25, 2022

    @sivel
    Member

    It looks like the JSON encoder class is missing support for Mapping which core has:

    class AnsibleJSONEncoderLocal(json.JSONEncoder):

    I didn't specifically test this playbook, but I'd guess it has something to do with the item from the loop that is getting serialized somehow.

  6. AlanCoding commented on Oct 25, 2022

    @AlanCoding
    Member

    Well, we can't import from ansible in that context, and Mapping is an internal import. That is, then, conditionally imported depending on python version, and for what runner supports that resolves to collections.abc.Mapping. We could do a somewhat superficial unit test for this and trust that it fixes the issue, ideally, or we could get a playbook reproducer.

  7. sivel commented on Oct 25, 2022

    @sivel
    Member

    Well, we can't import from ansible in that context

    Yep, I wasn't recommending that. Just that you could import collections.abc.Mapping and mirror the logic that core has, by casting Mapping to dict.

  8. added 2 commits that reference this issue on Jan 17, 2023
    23f0375
    6707b8a
  9. Akasurde commented on Jan 17, 2023

    @Akasurde
    Member

    resolved_by_pr #1181

  10. AlanCoding commented on Jan 18, 2023

    @AlanCoding
    Member

    I tried again, and still can't reproduce this. Could someone hold my hand for getting the above error?

    added file test/fixtures/projects/debug/project/hostvars_as_mapping.yml

    - name: Test using hostvars as mapping
      connection: local
      hosts: localhost
      gather_facts: false
      tasks:
        - debug:
            msg:
             - "{{ item.inventory_hostname}}"
          loop: "{{ ansible_play_hosts
            | default([])
            | map('extract', hostvars)
            | list
            }}"

    run

    ansible-runner run test/fixtures/projects/debug/ -p hostvars_as_mapping.yml
    
  11. added a commit that references this issue on Jan 27, 2026
    c4fe842
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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