一、主机规划
创建4台虚拟机作为演示 Ansible 的环境,安装操作系统为 CentOS 7.4。其中1台作为管理主机,其他3台作为被管理主机。IP及主机名分别为:
IP 主机名 备注
192.168.128.10 ansible-manager 管理主机
192.168.128.11 ansible-demo1 被管理主机1
192.168.128.12 ansible-demo2 被管理主机2
192.168.128.13 ansible-demo3 被管理主机3
二、安装 Ansible
在管理主机上面使用 yum 安装 Ansible。因为安装 Ansible 需要 epel 源,这里配置了阿里的 yum 源和 epel 源。
cd /etc/yum.repos.d/
yum源
vi CentOS-Base.repo
[base]
name=CentOS-$releasever - Base
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
released updates
[updates]
name=CentOS-$releasever - Updates
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
epel源
vi epel.repo
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
baseurl=http://mirrors.aliyun.com/epel/7/$basearch
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
安装 Ansible:
[root@ansible-manager ~]# yum install ansible
安装完成,查看版本:
[root@ansible-manager ~]# ansible --version
ansible 2.5.1
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Aug 4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
三、Ansible配置文件
安装完毕,可以修改默认配置,一般不需要修改。
vi /etc/ansible/ansible.cfg
[defaults]