From d484885762bc9680a13746abc69d64904018b021 Mon Sep 17 00:00:00 2001 From: ibuler Date: Mon, 1 Nov 2021 18:12:45 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BF=AE=E5=A4=8Dansible=20stdout=20?= =?UTF-8?q?=E4=B8=AD=E9=9D=9Eutf8=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/common/utils/strings.py | 4 ++++ apps/ops/ansible/callback.py | 6 ++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/apps/common/utils/strings.py b/apps/common/utils/strings.py index f6dcdef18..045263a12 100644 --- a/apps/common/utils/strings.py +++ b/apps/common/utils/strings.py @@ -3,3 +3,7 @@ import re def no_special_chars(s): return bool(re.match(r'\w+$', s)) + + +def safe_str(s): + return s.encode('utf-8', errors='ignore').decode('utf-8') diff --git a/apps/ops/ansible/callback.py b/apps/ops/ansible/callback.py index 3264e59c2..cb42350b7 100644 --- a/apps/ops/ansible/callback.py +++ b/apps/ops/ansible/callback.py @@ -10,6 +10,8 @@ from ansible.plugins.callback import CallbackBase from ansible.plugins.callback.default import CallbackModule from ansible.plugins.callback.minimal import CallbackModule as CMDCallBackModule +from common.utils.strings import safe_str + class CallbackMixin: def __init__(self, display=None): @@ -84,7 +86,7 @@ class AdHocResultCallback(CallbackMixin, CallbackModule, CMDCallBackModule): detail = { 'cmd': cmd, 'stderr': task_result.get('stderr'), - 'stdout': task_result.get('stdout'), + 'stdout': safe_str(str(task_result.get('stdout', ''))), 'rc': task_result.get('rc'), 'delta': task_result.get('delta'), 'msg': task_result.get('msg', '') @@ -216,7 +218,7 @@ class CommandResultCallback(AdHocResultCallback): if t == "ok": cmd['cmd'] = res._result.get('cmd') cmd['stderr'] = res._result.get('stderr') - cmd['stdout'] = res._result.get('stdout') + cmd['stdout'] = safe_str(str(res._result.get('stdout', ''))) cmd['rc'] = res._result.get('rc') cmd['delta'] = res._result.get('delta') else: