Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object of type HostVarsVars is not JSON serializable #1137

Open
pinkbubba opened this issue Sep 29, 2022 · 8 comments · May be fixed by #1181
Open

Object of type HostVarsVars is not JSON serializable #1137

pinkbubba opened this issue Sep 29, 2022 · 8 comments · May be fixed by #1181

Comments

@pinkbubba
Copy link

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

@github-actions github-actions bot added the needs_triage New item that needs to be triaged label Sep 29, 2022
@pinkbubba
Copy link
Author

pinkbubba commented Sep 29, 2022

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__}

@eqrx
Copy link
Contributor

eqrx commented Oct 4, 2022

@shanemcd @AlanCoding any thoughts on that?

@eqrx eqrx removed the needs_triage New item that needs to be triaged label Oct 4, 2022
@AlanCoding
Copy link
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.

@sivel
Copy link
Member

sivel commented Oct 25, 2022

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.

@AlanCoding
Copy link
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.

@sivel
Copy link
Member

sivel commented Oct 25, 2022

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.

Akasurde added a commit to Akasurde/ansible-runner that referenced this issue Jan 17, 2023
@Akasurde Akasurde linked a pull request Jan 17, 2023 that will close this issue
Akasurde added a commit to Akasurde/ansible-runner that referenced this issue Jan 17, 2023
@Akasurde
Copy link
Member

resolved_by_pr #1181

@AlanCoding
Copy link
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants