只在一台机器上生成秘钥:
ssh-keygen -t rsa -b 2048 -P '' -f /home/log4x/.ssh/id_rsa
在所有主机上生成秘钥:
ansible all -m shell -a " ssh-keygen -t rsa -b 2048 -P '' -f /home/log4x/.ssh/id_rsa"
# cat ssh.yml
---
- hosts: all
gather_facts: no
tasks:
- name: install ssh key
authorized_key: user=log4x
key="{{ lookup('file','/home/log4x/.ssh/id_rsa.pub')}}"
state=present
ansible-playbook -i /etc/ansible/hosts ssh.yml
PLAY [all] *******************************************************************************************************************************************************
TASK [install ssh key] *******************************************************************************************************************************************
changed: [log4x172.20.xxx]
changed: [log4x172.20.xxx]
changed: [log4x172.20.xxx]
changed: [log4x172.20.xxx]
changed: [log4x172.20.xxx]
PLAY RECAP *******************************************************************************************************************************************************
log4x172.20.xxx : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
log4x172.20.xxx : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
log4x172.20.xxx : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
log4x172.20.xxx : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
log4x172.20.xxx : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[log4x@slcj-log4x sk]$ ssh 172.20.xxx
Last login: Fri Sep 2 12:21:13 2022 from 172.20.xxx
[log4x@slcj-rzcli-filk ~]$
ansible all -m shell -a "cat /home/log4x/.ssh/id_rsa.pub" >> 33.log
cat 33.log
[log4x@slcj-log4x .ssh]$ cat 33.log
log4x172.20.xxx | CHANGED | rc=0 >>
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDQLdcU2RogKifSSvzIYbfxl3rhC2pLO1xpUrq4CNRoCQ3ee9ZfDB4FXZLMdnoEqHVYhurFF3ApaHEISLW01gl6p+RinOUt+x8HLa6tdegpYvB5BjxPWThuiYzmhvf4uGEoWsUnowUnhvNpTdkQcHFI3AdXcSAqU/F4zZf4d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AAAADAQABAAABAQDPBa910vWDrLGc/jE3FoWeCtGCg7MjDDfGMQUCVIyUBp0NpfiPwXvhBn1PCEXAF9X0V/WHSJw+B2QH/Q7529x1PEelMMOJIUS+lv5OEBG0RkVUZ66VFKOg/XlW/CDklhKHp0yzcm4MSb0QRN9GzMXIw8cqG89xa/+yL41XrtIBnKxD5AdJGzJr5P9f9h5rBbSKh6yoJc7S5m4Tls8QnLYQo/RuojLwjkf5yV7z4Kg6NAZIfOpJy4+r7iYZotYDxRaUOwnprtLa9vN9bbd4UbEVGMLvuVkLAiDcW0EnaUzcf0XjHthK+LXRRmFbWtBD1j3ABZuwy5bWU6ZSKhEMelHJ log4x@slcj-rzsj
log4x172.20.xxx | CHANGED | rc=0 >>
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCmmt1FjfCHYq5DjsrWpzhgAOl2vAtm+7NVDWLFlvU4OCHMX/3D1raDTIZIfTiyRJCP/iPOGys5eY7IEYgpmrxQQivKZJDGbjflgN8lFGv2OOzWdu4LFJZaHU3oAS1Ok7BgTXM790p2N1K0lNSrh+jOl1spMV7guyZ1zsHC9jj9heWyGv13PysCd77ebawsYWmgzAMVL5UraPmFWrC9S/hE+1HAa7L2ABPbwQGF7mblL/WGguVrkuodk8x6AAXEP73J/PgVgw9wTvE6s+qLHKLXubRXMrYFdKTv8pNO5V1hPTlZ755+Jlovq6yDGNty9LrjopvXn5RPNW+PuYLXsXdp log4x@slcj
将33.log中的密码,都加入到authorized_keys文件中,将authorized_keys文件分发到所有机器
[log4x@slcj-log4x .ssh]$ ansible all -m copy -a "src=/home/log4x/.ssh/authorized_keys dest=/home/log4x/.ssh/"
log4x172.20.xxx | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "f441b1c06255a4a7a1ee23a4fd44bd480e8addef",
"dest": "/home/log4x/.ssh/authorized_keys",
"gid": 1000,
"group": "log4x",
"md5sum": "2619273ad981f944a12b9571de6eb779",
"mode": "0600",
"owner": "log4x",
"size": 2008,
"src": "/home/log4x/.ansible/tmp/ansible-tmp-1662094582.66-75144-66062671490455/source",
"state": "file",
"uid": 1000
}
log4x172.20.xxx | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "f441b1c06255a4a7a1ee23a4fd44bd480e8addef",
"dest": "/home/log4x/.ssh/authorized_keys",
"gid": 1000,
"group": "log4x",
"md5sum": "2619273ad981f944a12b9571de6eb779",
"mode": "0600",
"owner": "log4x",
"size": 2008,
"src": "/home/log4x/.ansible/tmp/ansible-tmp-1662094582.67-75146-280348871791696/source",
"state": "file",
"uid": 1000
}
log4x172.20.xxx | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
[log4x@slcj .ssh]$ ssh 172.20.xxx
Last login: Fri Sep 2 12:56:23 2022 from 172.20.xxx
[log4x@slcj-log5x ~]$ ssh 172.20.xxx
The authenticity of host '172.20.xxx (172.20.251.196)' can't be established.
ECDSA key fingerprint is SHA256:4hdboxixvwfoHJBPA9lIpyaqNGodSLqsXuf8K44a3J8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '172.20.xxx' (ECDSA) to the list of known hosts.
Last login: Fri Sep 2 12:56:25 2022 from 172.20.xxx
[log4x@slcj-rzsj ~]$
[log4x@slcj-rzsj ~]$
如果不想输入yes,可将known_hosts文件一同copy过去。
这下,集群中各主机间也是相互免密