From 5c768dc6f1ec41d2894671cc7606258af396d669 Mon Sep 17 00:00:00 2001 From: Alexey Masolov Date: Sun, 22 Nov 2020 22:21:18 +1100 Subject: [PATCH] [2.10] Fix omapi_host on python3 (#788) --- .../788-fix_omapi_host_on_python3.yaml | 2 ++ plugins/modules/net_tools/omapi_host.py | 20 +++++++++---------- 2 files changed, 12 insertions(+), 10 deletions(-) create mode 100644 changelogs/fragments/788-fix_omapi_host_on_python3.yaml diff --git a/changelogs/fragments/788-fix_omapi_host_on_python3.yaml b/changelogs/fragments/788-fix_omapi_host_on_python3.yaml new file mode 100644 index 0000000000..08db3620df --- /dev/null +++ b/changelogs/fragments/788-fix_omapi_host_on_python3.yaml @@ -0,0 +1,2 @@ +bugfixes: + - omapi_host - fix compatibility with Python 3 (https://github.com/ansible-collections/community.general/issues/787). diff --git a/plugins/modules/net_tools/omapi_host.py b/plugins/modules/net_tools/omapi_host.py index 92652a22b2..4e6738cdf4 100644 --- a/plugins/modules/net_tools/omapi_host.py +++ b/plugins/modules/net_tools/omapi_host.py @@ -150,7 +150,7 @@ class OmapiHostManager: def connect(self): try: - self.omapi = Omapi(self.module.params['host'], self.module.params['port'], self.module.params['key_name'], + self.omapi = Omapi(self.module.params['host'], self.module.params['port'], to_bytes(self.module.params['key_name']), self.module.params['key']) except binascii.Error: self.module.fail_json(msg="Unable to open OMAPI connection. 'key' is not a valid base64 key.") @@ -173,13 +173,13 @@ class OmapiHostManager: def unpack_facts(obj): result = dict(obj) if 'hardware-address' in result: - result['hardware-address'] = unpack_mac(result['hardware-address']) + result['hardware-address'] = to_native(unpack_mac(result[to_bytes('hardware-address')])) if 'ip-address' in result: - result['ip-address'] = unpack_ip(result['ip-address']) + result['ip-address'] = to_native(unpack_ip(result[to_bytes('ip-address')])) if 'hardware-type' in result: - result['hardware-type'] = struct.unpack("!I", result['hardware-type']) + result['hardware-type'] = struct.unpack("!I", result[to_bytes('hardware-type')]) return result @@ -192,11 +192,11 @@ class OmapiHostManager: # If host was not found using macaddr, add create message if host_response is None: msg = OmapiMessage.open(to_bytes('host', errors='surrogate_or_strict')) - msg.message.append(('create', struct.pack('!I', 1))) - msg.message.append(('exclusive', struct.pack('!I', 1))) - msg.obj.append(('hardware-address', pack_mac(self.module.params['macaddr']))) - msg.obj.append(('hardware-type', struct.pack('!I', 1))) - msg.obj.append(('name', self.module.params['hostname'])) + msg.message.append((to_bytes('create'), struct.pack('!I', 1))) + msg.message.append((to_bytes('exclusive'), struct.pack('!I', 1))) + msg.obj.append((to_bytes('hardware-address'), pack_mac(self.module.params['macaddr']))) + msg.obj.append((to_bytes('hardware-type'), struct.pack('!I', 1))) + msg.obj.append((to_bytes('name'), to_bytes(self.module.params['hostname']))) if self.module.params['ip'] is not None: msg.obj.append((to_bytes("ip-address", errors='surrogate_or_strict'), pack_ip(self.module.params['ip']))) @@ -212,7 +212,7 @@ class OmapiHostManager: self.module.fail_json(msg="Invalid statements found: %s" % to_native(e)) if len(stmt_join) > 0: - msg.obj.append(('statements', stmt_join)) + msg.obj.append((to_bytes('statements'), to_bytes(stmt_join))) try: response = self.omapi.query_server(msg)