官方文档使用ssh-keygen -t rsa生成密钥对ssh-keygen -t rsa添加ansible hosts$ cat /etc/ansible/hosts[web]192.168.56.
官方文档
使用ssh-keygen -t rsa生成密钥对
ssh-keygen -t rsa
添加ansible hosts
$ cat /etc/ansible/hosts[web]192.168.56.102 ansible_ssh_user=root ansible_ssh_pass=root ansible_ssh_port=22192.168.56.103 ansible_ssh_user=root ansible_ssh_pass=root ansible_ssh_port=22192.168.56.104 ansible_ssh_user=root ansible_ssh_pass=root ansible_ssh_port=22
修改ansible.cfg
host_key_checking=False# 不用检查host key
编写yaml文件
注意文件格式,不要用tab
$ cat push-ssh.yaml- hosts: web user: root tasks: - name: ssh-key-copy authorized_key: user=root key="{{ lookup('file','/root/.ssh/id_rsa.pub')}}" tags: - sshkey
批量推送
$ ansible-playbook push-ssh.yamlPLAY [web] ***************************************************************************************************************************************************TASK [Gathering Facts] ***************************************************************************************************************************************ok: [192.168.56.104]ok: [192.168.56.103]ok: [192.168.56.102]TASK [ssh-key-copy] ******************************************************************************************************************************************changed: [192.168.56.104]changed: [192.168.56.102]changed: [192.168.56.103]PLAY RECAP ***************************************************************************************************************************************************192.168.56.102 : ok=2 changed=1 unreachable=0 failed=0192.168.56.103 : ok=2 changed=1 unreachable=0 failed=0192.168.56.104 : ok=2 changed=1 unreachable=0 failed=0
验证
[root@localhost ~]# ssh [email protected] login: Fri Jul 14 15:56:00 2017 from 192.168.56.101[root@localhost ~]#[root@localhost ~]# exit登出Connection to 192.168.56.102 closed.[root@localhost ~]# ssh [email protected] login: Fri Jul 14 15:56:10 2017 from 192.168.56.101[root@localhost ~]# exit登出Connection to 192.168.56.102 closed.[root@localhost ~]# ssh [email protected] login: Fri Jul 14 15:56:00 2017 from 192.168.56.101[root@localhost ~]# exit登出Connection to 192.168.56.103 closed