You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CentOS7使用pip3安装Ansible触发Unicode编码报错如何解决

问题根因

你遇到的UnicodeEncodeError报错本质是CentOS7系统默认字符集为ASCII,pip解压Ansible安装包时遇到包内的非ASCII字符无法编码导致安装中断。

可行解决方案

方案1:临时修改字符集完成安装(无需重启生效)

执行如下命令指定字符集为UTF-8后再安装Ansible:

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
pip3 install ansible

方案2:永久修改系统字符集(解决后续所有同类字符编码问题)

  1. 安装语言包:
sudo yum install -y glibc-langpack-en
  1. 修改字符集配置文件:
echo 'LANG=en_US.UTF-8' | sudo tee /etc/locale.conf
echo 'LC_ALL=en_US.UTF-8' | sudo tee -a /etc/locale.conf
  1. 加载配置生效:
source /etc/locale.conf
  1. 验证配置:执行locale命令,输出中LANG和LC_ALL字段均为en_US.UTF-8即配置正确,之后正常执行pip3 install ansible即可。

备选方案:直接适配原有Ansible 2.9版本

如果不需要升级Ansible版本,仅需解决原有2.9版本默认用Python2的问题,无需重装,直接在Ansible配置中指定Python3解释器即可:
在/etc/ansible/ansible.cfg中添加如下配置:

[defaults]
interpreter_python = /usr/bin/python3

也可以在playbook中针对目标主机指定ansible_python_interpreter: /usr/bin/python3变量,即可解决get_url模块的报错问题。

内容的提问来源于stack exchange,提问作者Cian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 22:24:02