本文介绍怎么安装ansibe以及展示怎么用其来对其他主机进行连接管理
本文需要用到6台主机
第一台 ip 192.168.4.10 用于安装ansible
第二台 ip 192.168.4.11 web1服务器
第三台 ip 192.168.4.12 web2服务器
第四台 ip 192.168.4.21 db1服务器
第五台 ip 192.168.4.21 db2服务器
第六台 ip 192.168.4.33 cache服务器
第一台主机进行ansible安装
需要到结合自己的系统到官网上下载ansible需要的以下6个包
ansible-2.4.2.0-2.el7.noarch.rpm
python-passlib-1.6.5-2.el7.noarch.rpm
python2-jmespath-0.9.0-3.el7.noarch.rpm repodata
python-httplib2-0.9.2-1.el7.noarch.rpm
sshpass-1.06-2.el7.x86_64.rpm
python-paramiko-2.1.1-4.el7.noarch.rpm
[root@ansible ~]# yum -y install ansible ---安装ansible
[root@ansible ~]# ansible --version ----查看安ansible的版本信息
ansible 2.4.2.0
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)]
[root@ansible ~]# ssh-keygen -t rsa ----生成密钥的主要用途在于传送给其他的主机
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:bjDjwdV7SXSsKih29dZ10mbcMTNOhg6Tb4h1y5QYsU8 root@ansible
The key's randomart image is:
+---[RSA 2048]----+
| o*.+ |
| . B.*.B |
| . +.@E=o*|
| . ... ++Bo.B|
| *oS..o+o = |
| o.o*. +.. |
| . o. oo |
| . |
| |
+----[SHA256]-----+
[root@ansible ~]# ssh-keyscan web1 web2 db1 db2 cache >>/root/.ssh/known_hosts 把其他主机的域名也设置免密钥登录
[root@ansible ~]# vim /etc/ansible/hosts ----在最后添加需要批量管理的的主机
[web] ----设置web批量管理的组名
web1 ----可以设置域名或者IP名
web2
[db]
192.168.6.21
192.168.6.22
[other]
192.168.6.33
[root@ansible ~]# ansible other --list-hosts ---查看ansible的other组管理的主机
hosts (1):
192.168.6.33
[root@ansible ~]# ansible db -m ping ----查看是否成功管理相应的主机
192.168.6.22 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.6.21 | SUCCESS => {
"changed": false,
"ping": "pong"
}