2019-06-08 · Develop Tools

如何在CentOS7下安装配置Ansible环境

前置准备

1.关闭防火墙

systemctl stop firewalld
systemctl disable firewalld

2.关闭selinux强制访问控制安全策略,修改文件/etc/selinux/config将配置改为如下

SELINUX=disabled

3.安装工具包和相关依赖

yum groupinstall 'Development Tools' -y
yum install vim wget zlib* openssl-devel -y

安装python3

1.下载并解压python3.5安装包

wget http://www.python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz
tar xf Python-3.6.5.tar.xz

2.进入解压目录进行编译安装

cd Python-3.6.5
./configure --prefix=/usr/local/ --with-ensurepip=install --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
make && make altinstall

3.为pip3.6建立软链接

which pip3.6
ln -s /usr/local/bin/pip3.6 /usr/local/bin/pip

安装和配置virtualenv工具环境

1.先使用pip进行安装virtualenv工具

pip install virtualenv

2.创建ansible系统账户

useradd deploy
su - deploy

3.创建python3.6版本的virtualenv实例

virtualenv -p /usr/local/bin/python3.6 .py3-a2.5-env

源码安装ansible实例

1.进入virtualenv实例目录下载ansible源码

cd /home/deploy/.py3-a2.5-env/
git clone https://github.com/ansible/ansible.git
cd ansible
git checkout stable-2.5

2.加载virtualenv实例环境

source /home/deploy/.py3-a2.5-env/bin/activate

3.安装ansible依赖模块

pip install paramiko PyYAML jinja2

4.加载ansible2.5版本

source /home/deploy/.py3-a2.5-env/ansible/hacking/env-setup -q

5.进行安装后的验证

ansible --version

配置SSH免密码密钥认证

由于ansible是通过ssh通道和其他主机进行通信,所以需要配置SSH免密码密钥认证
1.创建ssh本地密钥

ssh-keygen -t rsa -C "your_email@example.com"

2.与目标机器进行密钥认证

ssh-copy-id -i /home/deploy/.ssh/id_rsa.pub root@remote_ip