ansible api执行远程脚本:sh nfts/test/test.sh
报错:ERROR Internal Server Error
查看日志:docker logs -f --tail=100 ansible-api
File "/opt/py3/lib/python3.8/site-packages/rest_framework/renderers.py",line 110,in render return ret.encode() UnicodeEncodeError:'utf-8' codec can't encode characters in position 323-334: surrogates not allowed Task ansible_ui.tasks.run_adhoc_raw[] received Start task: Ansible Ad-hoc
原因:执行脚本的远程服务器是gb2312编码,而ansible需要utf-8编码,所以在脚本执行完返回结果时ansible接收的结果是乱码,乱码转Unicode时报错。
解决:
在执行的脚本后 加
| iconv -f GBK -t UTF-8
(转脚本编码)
- 命令行执行:
docker exec -it ansible-api bash cd /opt/py3/bin ./ansible all -i 36.xx.xx.211, -u login_name(远程服务器) -k -m shell -a "cat /nfts/test/test.sh | iconv -f GBK -t UTF-8;date; sh nfts/test/test.sh | iconv -f GBK -t UTF-8" # -k 提示输入密码(目标机)
- 任务和脚本执行
cat /nfts/test/test.sh | iconv -f GBK -t UTF-8 date sh nfts/test/test.sh | iconv -f GBK -t UTF-8