From eb6337c0c9f8c4dce46099ccf3459e3e7cd71113 Mon Sep 17 00:00:00 2001 From: mirabilos Date: Thu, 30 Oct 2025 20:19:06 +0100 Subject: [PATCH] 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 Co-authored-by: Felix Fontein * do not unpack_ip twice Noticed by Felix Fontein * mention py3k in changelog fragment, too --------- 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 ef72a71e39..a03a25ec0d 100644 --- a/plugins/modules/omapi_host.py +++ b/plugins/modules/omapi_host.py @@ -144,7 +144,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: @@ -175,15 +175,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 @@ -231,8 +234,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