Skip to content

Commit be65a9f

Browse files
patchback[bot]mirabilosfelixfontein
authored
[PR #11001/eb6337c0 backport][stable-10] omapi_host: fix bytes vs. str confusion (#11021)
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. ansible/ansible#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 <[email protected]> * mention py3k in changelog fragment, too --------- (cherry picked from commit eb6337c) Co-authored-by: mirabilos <[email protected]> Co-authored-by: Felix Fontein <[email protected]>
1 parent 0448319 commit be65a9f

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bugfixes:
2+
- "omapi_host - make return values compatible with ansible-core 2.19 and Python 3 (https://github.com/ansible-collections/community.general/pull/11001)."

plugins/modules/omapi_host.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@
146146
pureomapi_found = False
147147

148148
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
149-
from ansible.module_utils.common.text.converters import to_bytes, to_native
149+
from ansible.module_utils.common.text.converters import to_bytes, to_native, to_text
150150

151151

152152
class OmapiHostManager:
@@ -178,15 +178,18 @@ def get_host(self, macaddr):
178178

179179
@staticmethod
180180
def unpack_facts(obj):
181-
result = dict(obj)
181+
result = {}
182+
for k, v in dict(obj).items():
183+
result[to_text(k)] = v
184+
182185
if 'hardware-address' in result:
183-
result['hardware-address'] = to_native(unpack_mac(result[to_bytes('hardware-address')]))
186+
result['hardware-address'] = to_native(unpack_mac(result['hardware-address']))
184187

185188
if 'ip-address' in result:
186-
result['ip-address'] = to_native(unpack_ip(result[to_bytes('ip-address')]))
189+
result['ip-address'] = to_native(unpack_ip(result['ip-address']))
187190

188191
if 'hardware-type' in result:
189-
result['hardware-type'] = struct.unpack("!I", result[to_bytes('hardware-type')])
192+
result['hardware-type'] = struct.unpack("!I", result['hardware-type'])
190193

191194
return result
192195

@@ -234,8 +237,8 @@ def setup_host(self):
234237
response_obj = self.unpack_facts(host_response.obj)
235238
fields_to_update = {}
236239

237-
if to_bytes('ip-address', errors='surrogate_or_strict') not in response_obj or \
238-
unpack_ip(response_obj[to_bytes('ip-address', errors='surrogate_or_strict')]) != self.module.params['ip']:
240+
if 'ip-address' not in response_obj or \
241+
response_obj['ip-address'] != self.module.params['ip']:
239242
fields_to_update['ip-address'] = pack_ip(self.module.params['ip'])
240243

241244
# Name cannot be changed

0 commit comments

Comments
 (0)