由于安装kubernetes的过程太过于繁琐与复杂,听说使用ansible可以简化kubernetes的安装流程,所以先学了一下ansible,发现centos8下的ansible安装也非常麻烦,经笔者亲身试验,总结出了一套安装方法:
1.更新yum源
centos本身自带的yum源在centos8中不再提供ansible包的下载,需要更新EPEL库
yum install -y epel -release
2.下载python3
ansible是用python编写的,可由python直接安装,大部分linux系统中自带python,这里提供没有自带python的安装流程
dnf install python3 python3-pip -y
查看python版本
python3 -V
3.安装ansible
大部分教程中会使用官方的源
pip3 install ansible --user
但是国内的网速太慢,常常连接不上或者下载需要三到四个小时之久,这里在提供使用阿里的镜像网站
pip install ansible -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
以下为成功案例
Successfully installed MarkupSafe-2.0.1 ansible-4.10.0 ansible-core-2.11.12 cffi-1.15.1 cryptography-40.0.2 jinja2-3.0.3 packaging-21.3 pycparser-2.21 resolvelib-0.5.4
4.报错分析
1.哈希值不匹配
THESE PACKAGES DO NOT MATCH THE HASHES FROM THE REQUIREMENTS FILE
这是python使用第三方库时的常见报错,原因可能为网速较慢导致的只需要在后面加上--upgrade即可
pip3 install ansible -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com --upgrade
2.在使用--upgrde后仍然报错
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-fvlt8zl4/cryptography/
一般是因为python版本较低导致的,更新一下pip就可以了
我们使用的是pip3,
pip3 install --upgrade pip
完美解决。
5.配置文件
使用pip安装的ansible不会自带配置文件,甚至/etc/ansible文件夹都没有,例如使用ansible --version时:
ansible [core 2.11.12]
config file = None
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/local/bin/ansible
python version = 3.6.8 (default, Sep 10 2021, 09:13:53) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]
jinja version = 3.0.3
libyaml = True
其中的config file对应的值为空,所以我们需要自行创建该文件并创建ansible.cfg和hosts文件(不是文件夹),ansible.cfg是ansible的配置文件,hosts是需要通过ansible管理的节点
mkdir /etc/ansible
cd /etc/ansible
touch ansible.cfg
touch hosts
创建软连接
ansible-config init --disabled -t all > /etc/ansible/ansible.cfg
再次使用ansible --version
ansible [core 2.11.12]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/local/bin/ansible
python version = 3.6.8 (default, Sep 10 2021, 09:13:53) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]
jinja version = 3.0.3
libyaml = True
安装完成,你可以去看知识教程了(喜)
以上为作者自身经验总结,若持有疑问或发现错误,欢迎交流和指正。