CentOS7 installs Ansible2.8.3

Article directory

 

CentOS7 installs Ansible2.8.3

1. Introduction to Ansible

Ansible is an automated operation and maintenance tool that can help us manage hosts in batches. Using it can greatly speed up the efficiency of operation and maintenance. ansible is developed based on Python, so we need to install Python before installing ansible.

2. Preparation work

Based on the template machine built in "CentOS7 Experimental Template Machine Construction" , a CentOS7 host was cloned.

3. Deployment and construction

1. Modify the host name and hosts file

HOSTNAME=ansible
hostnamectl set-hostname "$HOSTNAME"
echo "$HOSTNAME">/etc/hostname
echo "$(grep -E '127|::1' /etc/hosts)">/etc/hosts
echo "$(ip a|grep "inet "|grep -v 127|awk -F'[ /]' '{print $6}') $HOSTNAME">>/etc/hosts

2. Install Python3.7.4 and upgrade pip

cd /tmp
wget https://mirrors.huaweicloud.com/python/3.7.4/Python-3.7.4.tgz
tar -xf Python-3.7.4.tgz
cd Python-3.7.4
yum -y install openssl-devel zlib-devel gcc libffi-devel
./configure --with-ssl --enable-shared \
LDFLAGS="-Wl,-rpath /usr/local/lib"
# 使用--with-ssl参数,很多网络安装源是https协议的
# 提前预装openssl-devel包,否则--with-ssl参数无法生效
# 提前预装libffi-devel,否则报错: No module named '_ctypes',具体请看文末补充说明
make && make altinstall
# 添加软链接
ln -s /usr/local/bin/pip3.7 /usr/local/bin/pip
mkdir -pv ~/.pip/
cat >~/.pip/pip.conf<<EOF
[global]
index-url = https://mirrors.huaweicloud.com/repository/pypi/simple
trusted-host = mirrors.huaweicloud.com
timeout = 120
EOF
pip install --upgrade pip
  •  
  •  

3. Use pip to install virtualenv

pip install virtualenv
# 使用虚拟环境

4. Create a deploy user and configure the pip domestic source

useradd deploy
echo deploy|passwd --stdin deploy
su - deploy
mkdir -pv ~/.pip/
cat >~/.pip/pip.conf<<EOF
[global]
index-url = https://mirrors.huaweicloud.com/repository/pypi/simple
trusted-host = mirrors.huaweicloud.com
timeout = 120
EOF

5. Use the virtualenv command to create a dedicated python3.7 environment to configure and install ansible2.8.3

virtualenv -p /usr/local/bin/python3.7 .py3.7.env
cd .py3.7.env
source ~/.py3.7.env/bin/activate
pip install paramiko PyYAML jinja2
wget https://mirrors.huaweicloud.com/ansible/ansible-2.8.3.tar.gz
tar -xf ansible-2.8.3.tar.gz
ln -s ansible-2.8.3 ansible
cd ansible
python setup.py build
python setup.py install
ansible --version

6. Configure the environment variables of the deploy user

su - deploy
echo 'source ~/.py3.7.env/bin/activate'>>~/.bash_profile

4. Supplementary instructions

1. Reasons for No module named '_ctypes' error:

There is a built-in module in Python3 called ctypes, which is an external function library module of Python3. It provides data types compatible with the C language and calls the shared library under the Linux system through it. This module needs to be used in the CentOS 7 system. Development link library (header file and link library) of the foreign function library. So the reason for the error is: the development link library package of the external function library (package name: libffi) is not installed in the CentOS 7 system.

2. Benefits of using virtualenv environment:

Can avoid conflicts with the Python2.x version used by yum

3. Another installation method of ansible:

You can clone the ansible project on Github, then checkout the version, compile and install. Because pulling the project from Github is slow, this method is not recommended.

Article reproduced from:

https://blog.csdn.net/sonoface/article/details/99474227

Guess you like

Origin blog.csdn.net/liuwkk/article/details/112172356