1、準備三臺虛擬機
master 192.168.1.211
slave 192.168.1.210
slave 192.168.1.195
2、安裝ansible
Ansible倉庫默認不在yum倉庫中,因此我們需要啓用epel倉庫
安裝epel
yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
安裝python-jinja2-2.7.2-4.el7
網址爲https://centos.pkgs.org/7/centos-x86_64/python-jinja2-2.7.2-4.el7.noarch.rpm.html
然後使用rz上傳
[root@localhost ~]# rz
yum安裝
[root@localhost ~]# yum -y install python-jinja2-2.7.2-4.el7.noarch.rpm
[root@localhost ~]# yum install -y ansible
生成公鑰/私鑰
[root@localhost ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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:
93:e6:21:1a:2a:c4:10:21:fe:01:57:c9:5e:0e:2c:0d root@localhost
The key's randomart image is:
+--[ RSA 2048]----+
|+o E*.. |
|o.o. * . |
|.. .o + |
|o . .. . . |
| o .. . S |
|. . o + o |
|. . . . |
| . |
| |
+-----------------+
默認生成在root家目錄.ssh目錄下
[root@localhost ~]# ls -ad .ssh
.ssh
私鑰設置,下次登錄就不需要密碼了
[root@localhost ~]# ssh-copy-id -i [email protected] //輸入yes和密碼
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
[root@localhost ~]# ssh-copy-id -i [email protected] //輸入yes和密碼
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
驗證登錄不需要密碼
[root@localhost ~]# ssh 192.168.1.210
Last login: Fri Dec 27 10:52:26 2019 from 192.168.1.211
[root@localhost ~]# exit
logout
Connection to 192.168.1.210 closed.
配置文件
[root@localhost ~]# vim /etc/ansible/hosts
加組名ip,保存退出
ping自己的組名
[root@localhost ~]# ansible -m ping "test-servers"
服務就搭建完成了
然後就可以通過以下命令對這兩臺機器進行操作
[root@localhost ~]# ansible test-servers -u root -a ls