一、ansible环境准备
1.1 创建6台主机
序号 | 主机名 | ip地址 | 主机类型 |
1 | ansible | 192.168.0.10 | 管理主机 |
2 | web1 | 192.168.0.11 | 托管主机 |
3 | web2 | 192.168.0.12 | 托管主机 |
4 | db1 | 192.168.0.21 | 托管主机 |
5 | db2 | 192.168.0.22 | 托管主机 |
6 | cache | 192.168.0.33 | 托管主机 |
1.2 按要求配置6台主机(这里以管理主机为例)
1.2.1 主机名配置
[root@localhost ~]# echo ansible > /etc/hostname
1.2.2 ip配置
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
# Generated by dracut initrd
DEVICE="eth0"
ONBOOT="yes"
IPV6INIT="no"
BOOTPROTO="static"
IPADDR="192.168.0.10"
NETMASK="255.255.255.0"
GATEWAY="192.168.0.254"
TYPE="Ethernet"
[root@localhost ~]# ifdown ifcfg-eth0 ;ifup ifcfg-eth0
*其余主机参照以上配置
1.2.3 hosts文件的配置
ansible:主机上配置ip与主机名对应关系,配置文件:/etc/hosts
[root@host2 ~]# vim /etc/hosts
192.168.0.10 ansible
192.168.0.11 web1
192.168.0.12 web2
192.168.0.21 db1
192.168.0.22 db2
192.168.0.33 cache
1.2.4 免密码登录
管理主机可以登录其他托管主机,无需密码
[root@ansible ~]# ssh-keygen -t rsa -b 2048 -N ' '
[root@ansible .ssh]# cd /root/.ssh/
[root@ansible .ssh]# for i in web1 web2 db1 db2 cache; do ssh-copy-id -i id_rsa.pub ${i}; done