准备俩台虚拟机即可
主机master:192.168.1.238
从机slave: 192.168.1.48
安装ansible
Ansible库默认不在yum库中,因此我们需要启用epel库安装epel
安装epel
[root@localhost ~]# yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
安装python-jinja2-2.7.2-4.el7
在网址下载Python安装包:https://centos.pkgs.org/7/centos-x86_64/python-jinja2-2.7.2-4.el7.noarch.rpm.html
因为本地有这个安装包直接rz上传安装
[root@localhost ~]# yum install -y python-jinja2-2.7.2-4.el7.noarch.rpm
安装ansible
[root@localhost ~]# yum install -y ansible
检查安装版本
[root@localhost ~]# ansible --version
ansible 2.9.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, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
生产公钥/私钥
[root@localhost ~]# 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:
c5:34:eb:cd:be:ef:bd:ed:4e:b6:0f:be:a9:25:85:7a root@localhost
The key's randomart image is:
+--[ RSA 2048]----+
| o |
| o o |
| + |
| o o . |
| S . + . |
| o . |
| . E o o|
| . = *o|
| o+**O|
+-----------------+
默认生成在root家目录.ssh目录下
[root@localhost ~]# ls -ad .ssh
.ssh
私钥设置,下次登录就不需要密码了
[root@localhost ~]# ssh-copy-id root@192.168.1.48
The authenticity of host '192.168.1.48 (192.168.1.48)' can't be established.
ECDSA key fingerprint is 12:51:2a:2c:e7:8b:e7:9c:b4:c3:25:ab:b7:e5:af:bd.
Are you sure you want to continue connecting (yes/no)? y
Please type 'yes' or 'no': y
Please type 'yes' or 'no': 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
root@192.168.1.48's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.1.48'"
and check to make sure that only the key(s) you wanted were added.
验证登录不需要密码
[root@localhost ~]# ssh 192.168.1.38
ssh: connect to host 192.168.1.38 port 22: No route to host
[root@localhost ~]# ssh 192.168.1.48
Last login: Sat Dec 28 16:38:52 2019 from 192.168.1.113
[root@localhost ~]# exit
logout
Connection to 192.168.1.48 closed.
编辑配置文件
[root@localhost ~]# vim /etc/ansible/hosts
ping自己的组名
[root@localhost ~]# ansible -m ping "ljzy"
验证
[root@localhost ~]# ansible ljzy -u root -a ls