一、下载安装包

1、下载ansible安装包
https://releases.ansible.com/ansible/
下载:
ansible-2.9.5.tar.gz
ansible-2.9.9.tar.gz
###下载两个是为了后续升级ansible操作

2、下载python
https://www.python.org/downloads/
Python-3.8.12.tar.xz

二、安装编译

1、安装python,配置pip

1、gcc --version #查看gcc是否安装

2、安装python
tar -xvf Python-3.8.12.tar.xz
cd Python-3.8.12
./configure --prefix=/usr/local/python3.8
make && make install
linux默认自带的python是2.7.5

3、添加软连接
rm -f /usr/bin/python /usr/bin/pip
ln -s /usr/local/python3.8/bin/python3.8 /usr/bin/python
ln -s /usr/local/python3.8/bin/pip3.8 /usr/bin/pip

4、配置pip源
mkdir /root/.pip
[root@k8s01 bin]# cat /root/.pip/pip.conf
[global]
index-url = https://mirrors.huaweicloud.com/repository/pypi/simple
trusted-host = mirrors.huaweicloud.com
timeout = 120

2、安装ansible

1、解压并安装
tar -xvf ansible-2.9.5.tar.gz
cd ansible-2.9.5
python setup.py install
报错:
Ansible now needs setuptools in order to build. Install it using your package manager
(usually python-setuptools) or via pip (pip install setuptools).
安装依赖:
更新pip
python -m pip install --upgrade pip
卸载并重装setuptools
pip uninstall setuptools ##需要输入Y确定
pip install setuptools

2、继续安装
python setup.py install
##如果缺少依赖就会卡住,可以ctrl +c停掉,使用pip安装完依赖,再重新执行安装命令

3、添加软连接
ansible安装完默认都安装到/usr/local/python3.8/bin下面
ln -s /usr/local/python3.8/bin/ansible* /usr/bin/

4、创建默认配置文件
二进制安装的,配置文件在解压包里面有个模板
mkdir /etc/ansible
cp /root/ansible-2.9.5/examples/ansible.cfg /etc/ansible

5、验证
ansible localhost -m ping