大家好,又见面了,我是你们的朋友全栈君。
管理端:192.168.223.10
被管理端:192.168.223.20
被管理端:192.168.223.30
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum -y install epel-release.noarch
[root@localhost ~]# yum install -y ansible
[root@localhost ~]# yum install -y tree
[root@localhost ~]# tree /etc/ansible/
/etc/ansible/
├── ansible.cfg
├── hosts
└── roles
[root@localhost ~]# vim /etc/ansible/hosts #配置主机清单
#以下内容为添加内容,不是修改内容
[webservers]
192.168.223.20 #被监控端1的IP
[mysql]
192.168.223.30 #被监控端2的IP
#生成密钥对
[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): #输入密码788788
Enter same passphrase again: #确认密码788788
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:WKpIMqbJgc2JyTz2C351d7bzbDJ83wUsyf1oKXaq82c root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| |
| |
| . |
|+= . + . + |
|*B= o S + + |
|==+. o . . o . = |
|oo..o . . + + = o|
|. ... .B.BE o|
| ... .+X=...|
+----[SHA256]-----+
[root@localhost ~]# ls -al
drwx------. 2 root root 57 4月 8 10:31 .ssh
[root@localhost ~]# cd .ssh/
[root@localhost .ssh]# ls
id_rsa id_rsa.pub #id_rsa:私钥;id_rsa.pub:公钥
[root@localhost .ssh]# ssh-copy-id root@192.168.223.20
[root@localhost .ssh]# ssh-copy-id root@192.168.223.30
#在监控端登录被监控端查看日期
[root@ansible ~]# ansible 192.168.223.20 -m command -a 'date'
Enter passphrase for key '/root/.ssh/id_rsa': #输入之前设置的密码:788788
192.168.223.20 | CHANGED | rc=0 >>
2021年 04月 08日 星期四 12:12:19 CST
[root@ansible ~]# ansible mysql -m command -a 'date' #当然也可以用别名进程查看
Enter passphrase for key '/root/.ssh/id_rsa':
192.168.223.30 | CHANGED | rc=0 >>
2021年 04月 08日 星期四 12:14:22 CST
-m:指定模块
-a:指定参数
[root@ansible ~]# ssh-agent bash
[root@ansible ~]# ssh-add
Enter passphrase for /root/.ssh/id_rsa:
Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/126020.html原文链接:https://javaforall.cn