From 0b5b5ec50a51740288ce2d9cf826e262135bd291 Mon Sep 17 00:00:00 2001 From: David Newswanger Date: Wed, 19 Jul 2017 12:37:20 -0400 Subject: [PATCH] fix unicode errors in iosxr --- lib/ansible/module_utils/netcfg.py | 7 ++++--- lib/ansible/plugins/action/iosxr_config.py | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/ansible/module_utils/netcfg.py b/lib/ansible/module_utils/netcfg.py index e9bcc10035c..9a037ade9ce 100644 --- a/lib/ansible/module_utils/netcfg.py +++ b/lib/ansible/module_utils/netcfg.py @@ -29,7 +29,7 @@ import re import hashlib from ansible.module_utils.six.moves import zip -from ansible.module_utils._text import to_bytes +from ansible.module_utils._text import to_bytes, to_native from ansible.module_utils.network_common import to_list DEFAULT_COMMENT_TOKENS = ['#', '!', '/*', '*/', 'echo'] @@ -205,6 +205,7 @@ class NetworkConfig(object): def parse(self, lines, comment_tokens=None): toplevel = re.compile(r'\S') childline = re.compile(r'^\s*(.+)$') + entry_reg = re.compile(r'([{};])') ancestors = list() config = list() @@ -212,8 +213,8 @@ class NetworkConfig(object): curlevel = 0 prevlevel = 0 - for linenum, line in enumerate(str(lines).split('\n')): - text = str(re.sub(r'([{};])', '', line)).strip() + for linenum, line in enumerate(to_native(lines, errors='surrogate_or_strict').split('\n')): + text = entry_reg.sub('', line).strip() cfg = ConfigLine(line) diff --git a/lib/ansible/plugins/action/iosxr_config.py b/lib/ansible/plugins/action/iosxr_config.py index c68a18fcc9b..8d7ccaffedf 100644 --- a/lib/ansible/plugins/action/iosxr_config.py +++ b/lib/ansible/plugins/action/iosxr_config.py @@ -25,7 +25,7 @@ import time import glob from ansible.plugins.action.iosxr import ActionModule as _ActionModule -from ansible.module_utils._text import to_text +from ansible.module_utils._text import to_text, to_bytes from ansible.module_utils.six.moves.urllib.parse import urlsplit from ansible.utils.vars import merge_hash @@ -74,7 +74,7 @@ class ActionModule(_ActionModule): os.remove(fn) tstamp = time.strftime("%Y-%m-%d@%H:%M:%S", time.localtime(time.time())) filename = '%s/%s_config.%s' % (backup_path, host, tstamp) - open(filename, 'w').write(contents) + open(filename, 'w').write(to_bytes(contents)) return filename def _handle_template(self):