ansible批量推送公钥

官方文档

使用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=22
192.168.56.103 ansible_ssh_user=root ansible_ssh_pass=root ansible_ssh_port=22
192.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.yaml

PLAY [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=0
192.168.56.103             : ok=2    changed=1    unreachable=0    failed=0
192.168.56.104             : ok=2    changed=1    unreachable=0    failed=0

验证

[root@localhost ~]# ssh root@192.168.56.102

Last 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 root@192.168.56.102
Last 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 root@192.168.56.103
Last login: Fri Jul 14 15:56:00 2017 from 192.168.56.101
[root@localhost ~]# exit
登出
Connection to 192.168.56.103 closed

转载于:https://my.oschina.net/careyjike/blog/1456625

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值