一、实现从管理机m01到其他机器的密钥认证
[root@m01 ~]# ssh-keygen
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:
SHA256:rKu2og75VVHnsOPn/Lh9YP/2Yi26PwD9wAzMihol4e8 root@m01
The key's randomart image is:
+---[RSA 2048]----+
| .. oo. |
| .. .. =+ |
| .o..o..= |
| ...+... = |
| oo S .. o |
| . .o . + + . |
|o . E o. + . |
|..... . + B o|
|oo.ooo. o.=*.*+|
+----[SHA256]-----+
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub -p 22 root@172.16.1.7
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.7
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.61
二、安装ansible
[root@m01 ~]# yum install ansible -y
三、配置ansible的主机列表
[root@m01 ~]# vim /etc/ansible/hosts
[server]
172.16.1.61
[client]
172.16.1.7
四、验证ansible
ansible是通过ssh端口探测通信
[root@m01 /etc/ansible]# ansible server -m ping
10.0.0.61 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@m01 /etc/ansible]# ansible client -m ping
10.0.0.7 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@m01 /etc/ansible]# ansible server -m command -a "df -h"
10.0.0.61 | CHANGED | rc=0 >>
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos_oldboyedu-root 17G 3.2G 14G 19% /
devtmpfs 979M 0 979M 0% /dev
tmpfs 991M 128K 991M 1% /dev/shm
tmpfs 991M 9.7M 981M 1% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/sda1 1014M 194M 821M 20% /boot
tmpfs 199M 0 199M 0% /run/user/0