perf: 修复ansible stdout 中非utf8的问题

pull/7134/head
ibuler 3 years ago committed by 老广
parent 8071f45f92
commit d484885762

@ -3,3 +3,7 @@ import re
def no_special_chars(s): def no_special_chars(s):
return bool(re.match(r'\w+$', s)) return bool(re.match(r'\w+$', s))
def safe_str(s):
return s.encode('utf-8', errors='ignore').decode('utf-8')

@ -10,6 +10,8 @@ from ansible.plugins.callback import CallbackBase
from ansible.plugins.callback.default import CallbackModule from ansible.plugins.callback.default import CallbackModule
from ansible.plugins.callback.minimal import CallbackModule as CMDCallBackModule from ansible.plugins.callback.minimal import CallbackModule as CMDCallBackModule
from common.utils.strings import safe_str
class CallbackMixin: class CallbackMixin:
def __init__(self, display=None): def __init__(self, display=None):
@ -84,7 +86,7 @@ class AdHocResultCallback(CallbackMixin, CallbackModule, CMDCallBackModule):
detail = { detail = {
'cmd': cmd, 'cmd': cmd,
'stderr': task_result.get('stderr'), 'stderr': task_result.get('stderr'),
'stdout': task_result.get('stdout'), 'stdout': safe_str(str(task_result.get('stdout', ''))),
'rc': task_result.get('rc'), 'rc': task_result.get('rc'),
'delta': task_result.get('delta'), 'delta': task_result.get('delta'),
'msg': task_result.get('msg', '') 'msg': task_result.get('msg', '')
@ -216,7 +218,7 @@ class CommandResultCallback(AdHocResultCallback):
if t == "ok": if t == "ok":
cmd['cmd'] = res._result.get('cmd') cmd['cmd'] = res._result.get('cmd')
cmd['stderr'] = res._result.get('stderr') 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['rc'] = res._result.get('rc')
cmd['delta'] = res._result.get('delta') cmd['delta'] = res._result.get('delta')
else: else:

Loading…
Cancel
Save