ansible中文结果返回乱码
在使用ansible执行查询时,如果有中文结果返回时,会出现乱码,具体如下图:
通过查看官方文档:module-lang,发现可以通过修改/etc/ansible/ansible.cfg中两项设置语言类型进行解决:
1#module_lang = C
2#module_set_locale = False
3module_lang = zh_CN.UTF-8
4module_set_locale = True
增加上面两行,其默认配置为lang为C,locale设置默认是False。如果是使用的ansible api 或者playbook,可以在其调用时,通过environment 进行设置,如下是ansible 1.9以下版本的API,具体调用如下:
1runner = ansible.runner.Runner(
2 module_name = m_name,
3 module_args = m_args,
4 pattern = ip,
5 remote_port = r_port,
6 remote_user = r_user,
7 private_key_file = key_path,
8 forks = forks_num,
9 environment = {'LANG':'zh_CN.UTF-8','LC_CTYPE':'zh_CN.UTF-8'}
10 )
playbook调用时,一样可以通过environment 设置语言或者代理信息等 。
捐赠本站(Donate)
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
- Author: shisekong
- Link: https://blog.361way.com/ansible-garbled/5497.html
- License: This work is under a 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. Kindly fulfill the requirements of the aforementioned License when adapting or creating a derivative of this work.