From cd50836977e608cf8cf64c14e4fa1188009ef3ff Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Thu, 30 Oct 2025 20:30:00 +0100 Subject: [PATCH] [PR #11001/eb6337c0 backport][stable-11] omapi_host: fix bytes vs. str confusion (#11022) omapi_host: fix bytes vs. str confusion (#11001) * omapi_host: fix bytes vs. str confusion After an update of the control node from Debian bookworm to trixie, the omapi_host module fails to work with the error message: Key of type 'bytes' is not JSON serializable by the 'module_legacy_m2c' profile. https://github.com/ansible/ansible/issues/85937 had the same error, but the fix is a bit more intricate here because the result dict is dynamically generated from an API response object. This also fixes unpacking the MAC and IP address and hardware type, which were broken for Python3. * Merge suggestion for changelog fragment * do not unpack_ip twice Noticed by Felix Fontein * mention py3k in changelog fragment, too --------- (cherry picked from commit eb6337c0c9f8c4dce46099ccf3459e3e7cd71113) Co-authored-by: mirabilos Co-authored-by: Felix Fontein --- changelogs/fragments/11001-omapi.yml | 2 ++ plugins/modules/omapi_host.py | 17 ++++++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) create mode 100644 changelogs/fragments/11001-omapi.yml diff --git a/changelogs/fragments/11001-omapi.yml b/changelogs/fragments/11001-omapi.yml new file mode 100644 index 0000000000..5131f763c0 --- /dev/null +++ b/changelogs/fragments/11001-omapi.yml @@ -0,0 +1,2 @@ +bugfixes: + - "omapi_host - make return values compatible with ansible-core 2.19 and Python 3 (https://github.com/ansible-collections/community.general/pull/11001)." diff --git a/plugins/modules/omapi_host.py b/plugins/modules/omapi_host.py index 36c5434fd5..c424f4c004 100644 --- a/plugins/modules/omapi_host.py +++ b/plugins/modules/omapi_host.py @@ -146,7 +146,7 @@ except ImportError: pureomapi_found = False from ansible.module_utils.basic import AnsibleModule, missing_required_lib -from ansible.module_utils.common.text.converters import to_bytes, to_native +from ansible.module_utils.common.text.converters import to_bytes, to_native, to_text class OmapiHostManager: @@ -178,15 +178,18 @@ class OmapiHostManager: @staticmethod def unpack_facts(obj): - result = dict(obj) + result = {} + for k, v in dict(obj).items(): + result[to_text(k)] = v + if 'hardware-address' in result: - result['hardware-address'] = to_native(unpack_mac(result[to_bytes('hardware-address')])) + result['hardware-address'] = to_native(unpack_mac(result['hardware-address'])) if 'ip-address' in result: - result['ip-address'] = to_native(unpack_ip(result[to_bytes('ip-address')])) + result['ip-address'] = to_native(unpack_ip(result['ip-address'])) if 'hardware-type' in result: - result['hardware-type'] = struct.unpack("!I", result[to_bytes('hardware-type')]) + result['hardware-type'] = struct.unpack("!I", result['hardware-type']) return result @@ -234,8 +237,8 @@ class OmapiHostManager: response_obj = self.unpack_facts(host_response.obj) fields_to_update = {} - if to_bytes('ip-address', errors='surrogate_or_strict') not in response_obj or \ - unpack_ip(response_obj[to_bytes('ip-address', errors='surrogate_or_strict')]) != self.module.params['ip']: + if 'ip-address' not in response_obj or \ + response_obj['ip-address'] != self.module.params['ip']: fields_to_update['ip-address'] = pack_ip(self.module.params['ip']) # Name cannot be changed