From 41d75783b5d0f1e81133e49c35bc80dcc61e286d Mon Sep 17 00:00:00 2001 From: Ganesh Nalawade Date: Thu, 1 Mar 2018 12:09:11 +0530 Subject: [PATCH] Fix junos_command py3 related issues (#36782) Fixes #36204 * tostring() input string shoulb be in byte string format * to_ele() input is required in unicode format --- lib/ansible/module_utils/network/common/netconf.py | 6 +++--- lib/ansible/modules/network/junos/junos_command.py | 3 ++- lib/ansible/plugins/netconf/__init__.py | 2 +- lib/ansible/plugins/netconf/junos.py | 2 +- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/ansible/module_utils/network/common/netconf.py b/lib/ansible/module_utils/network/common/netconf.py index a2d6b5427a5..8227325d1e9 100644 --- a/lib/ansible/module_utils/network/common/netconf.py +++ b/lib/ansible/module_utils/network/common/netconf.py @@ -27,7 +27,7 @@ # import sys -from ansible.module_utils._text import to_text, to_native +from ansible.module_utils._text import to_text, to_bytes from ansible.module_utils.connection import Connection, ConnectionError try: @@ -67,9 +67,9 @@ class NetconfConnection(Connection): response = self._exec_jsonrpc(name, *args, **kwargs) if 'error' in response: rpc_error = response['error'].get('data') - return self.parse_rpc_error(to_native(rpc_error, errors='surrogate_then_replace')) + return self.parse_rpc_error(to_bytes(rpc_error, errors='surrogate_then_replace')) - return fromstring(to_native(response['result'], errors='surrogate_then_replace')) + return fromstring(to_bytes(response['result'], errors='surrogate_then_replace')) def parse_rpc_error(self, rpc_error): if self.check_rc: diff --git a/lib/ansible/modules/network/junos/junos_command.py b/lib/ansible/modules/network/junos/junos_command.py index 3a026fa0610..cc8ae319019 100644 --- a/lib/ansible/modules/network/junos/junos_command.py +++ b/lib/ansible/modules/network/junos/junos_command.py @@ -171,6 +171,7 @@ import re import shlex from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils._text import to_text from ansible.module_utils.network.common.netconf import exec_rpc from ansible.module_utils.network.junos.junos import junos_argument_spec, get_configuration, get_connection, get_capabilities from ansible.module_utils.network.common.parsing import Conditional, FailedConditionalError @@ -240,7 +241,7 @@ def rpc(module, items): if fetch_config: reply = get_configuration(module, format=xattrs['format']) else: - reply = exec_rpc(module, tostring(element), ignore_warning=False) + reply = exec_rpc(module, to_text(tostring(element), errors='surrogate_then_replace'), ignore_warning=False) if xattrs['format'] == 'text': if fetch_config: diff --git a/lib/ansible/plugins/netconf/__init__.py b/lib/ansible/plugins/netconf/__init__.py index 518fbee300c..73a99ea2095 100644 --- a/lib/ansible/plugins/netconf/__init__.py +++ b/lib/ansible/plugins/netconf/__init__.py @@ -102,7 +102,7 @@ class NetconfBase(with_metaclass(ABCMeta, object)): """RPC to be execute on remote device :name: Name of rpc in string format""" try: - obj = to_ele(to_bytes(name, errors='surrogate_or_strict')) + obj = to_ele(name) resp = self.m.rpc(obj) return resp.data_xml if hasattr(resp, 'data_xml') else resp.xml except RPCError as exc: diff --git a/lib/ansible/plugins/netconf/junos.py b/lib/ansible/plugins/netconf/junos.py index 504e10ce0b0..f3b4f0e63c1 100644 --- a/lib/ansible/plugins/netconf/junos.py +++ b/lib/ansible/plugins/netconf/junos.py @@ -50,7 +50,7 @@ class Netconf(NetconfBase): device_info['network_os'] = 'junos' ele = new_ele('get-software-information') data = self.execute_rpc(to_xml(ele)) - reply = to_ele(to_bytes(data, errors='surrogate_or_strict')) + reply = to_ele(data) sw_info = reply.find('.//software-information') device_info['network_os_version'] = self.get_text(sw_info, 'junos-version')