Browse Source

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

pull/7134/head
ibuler 3 years ago committed by 老广
parent
commit
d484885762
  1. 4
      apps/common/utils/strings.py
  2. 6
      apps/ops/ansible/callback.py

4
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')

6
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:

Loading…
Cancel
Save