ansible 批量推送公钥

1、环境如下  ansible 主控端 192.168.33.239    被控端  192.168.11.148  192.168.11.149

 

1.使用 ssh-keygen -t rsa生成密钥对  (后面用opsadmin用户来操作)

 ssh-keygen -t rsa 

 

2.推送单个公钥到远程机器

格式: ssh-copy-id -i ~/.ssh/id_rsa.pub username@[ip,hostname]

ssh-copy-id -i ~/.ssh/id_rsa.pub "-p 10022 user@server"

 

3.添加ansible hosts

编辑/etc/ansible/hosts,没有则创建些文件。

 

格式:【主机名】 【主机地址】 【主机密码】  默认是root用户来进行的

[tomcat-servers]
1 ansible_ssh_user="root"  ansible_ssh_host=192.168.11.148 ansible_ssh_pass="test"
2 ansible_ssh_user="root"  ansible_ssh_host=192.168.11.149 ansible_ssh_pass="test"

 

新版的ansible(2.4) hosts有更新, 用以下方式:

[nginx]
192.168.11.148   ansible_user=opsadmin  ansible_ssh_pass="test"  ansible_ssh_port=4958
192.168.11.149   ansible_user=opsadmin  ansible_ssh_pass="test"  ansible_ssh_port=4958

 

4.批量推送公钥到远程机器

机器多的情况下,使用ssh-copy-id方法有些费时,使用ansible-playbook推送ymal,这里使用到了authoried_keys模块,可以参考 http://docs.ansible.com/authorized_key_module.html

将以下文件命名为:push.ssh.ymal 

[root@sz]# cat /home/opsadmin/push_ssh.yaml 
- hosts: nginx
  user: opsadmin
  tasks:
    - name: ssh-copy
      authorized_key:
        user: opsadmin
        state: present
        key: "{{ lookup('file','/home/opsadmin/.ssh/id_rsa.pub') }}"

 

[root@mail opt]# ansible-playbook --syntax-check push_ssh.yaml#检测语法

[root@mail opt]# ansible-playbook -C push_ssh.yaml #运行测试 并不实际运行

 

 

5.执行命令,检测结果 

 

先修改 /etc/ansible/hosts文件

192.168.11.148 ansible_ssh_port=4958
192.168.11.149 ansible_ssh_port=4958

 

 

 

7.如若报错,解决

Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this.  Please add this host's fingerprint to your known_hosts file to manage this host.

修改host_key_checking(默认是check的):

vi /home/xiangdong/ansible/ansible.cfg
# uncomment this to disable SSH key host checking
host_key_checking = False

转载于:https://www.cnblogs.com/hellojackyleon/p/9481927.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值