mirror of
https://github.com/ansible/ansible.git
synced 2026-08-03 00:22:10 +02:00
Add temporary module result serialization hook (#85609)
* Add temporary module result serialization hook * Sanity test fix --------- Co-authored-by: Matt Clay <matt@mystile.com>
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
minor_changes:
|
||||
- AnsibleModule - Add temporary internal monkeypatch-able hook to alter module result serialization by splitting serialization from ``_return_formatted`` into ``_record_module_result``.
|
||||
@@ -1513,11 +1513,19 @@ class AnsibleModule(object):
|
||||
# strip no_log collisions
|
||||
kwargs = remove_values(kwargs, self.no_log_values)
|
||||
|
||||
# return preserved
|
||||
# graft preserved values back on
|
||||
kwargs.update(preserved)
|
||||
|
||||
self._record_module_result(kwargs)
|
||||
|
||||
def _record_module_result(self, o: dict[str, t.Any]) -> None:
|
||||
"""
|
||||
Temporary internal hook to enable modification/bypass of module result serialization.
|
||||
|
||||
Monkeypatched by ansible.netcommon for direct in-worker module execution.
|
||||
"""
|
||||
encoder = _json.get_module_encoder(_ANSIBLE_PROFILE, _json.Direction.MODULE_TO_CONTROLLER)
|
||||
print('\n%s' % json.dumps(kwargs, cls=encoder))
|
||||
print('\n%s' % json.dumps(o, cls=encoder))
|
||||
|
||||
def exit_json(self, **kwargs) -> t.NoReturn:
|
||||
""" return from the module, without error """
|
||||
|
||||
@@ -10,7 +10,7 @@ import datetime
|
||||
import typing as t
|
||||
|
||||
import pytest
|
||||
|
||||
import pytest_mock
|
||||
|
||||
EMPTY_INVOCATION: dict[str, dict[str, t.Any]] = {u'module_args': {}}
|
||||
DATETIME = datetime.datetime.strptime('2020-07-13 12:50:00', '%Y-%m-%d %H:%M:%S')
|
||||
@@ -153,3 +153,26 @@ class TestAnsibleModuleExitValuesRemoved:
|
||||
out, err = capfd.readouterr()
|
||||
|
||||
assert json.loads(out) == expected
|
||||
|
||||
def test_record_module_result(self, mocker: pytest_mock.MockerFixture, stdin) -> None:
|
||||
"""Ensure that the temporary _record_module_result hook is called correctly."""
|
||||
recorded_result = None
|
||||
|
||||
expected_result = dict(changed=False, worked="yay")
|
||||
|
||||
def _record_module_result(_self, o: object) -> None:
|
||||
assert isinstance(o, dict)
|
||||
|
||||
nonlocal recorded_result
|
||||
recorded_result = o
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
|
||||
mocker.patch.object(AnsibleModule, '_record_module_result', _record_module_result)
|
||||
|
||||
am = AnsibleModule(argument_spec=dict())
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
am.exit_json(**expected_result)
|
||||
|
||||
assert expected_result.items() <= recorded_result.items()
|
||||
|
||||
Reference in New Issue
Block a user