ansbile 创建 主机之间的信任关系

- hosts: servers
  user: ubuntu
  sudo: yes
  gather_facts: no

  tasks:
   - name: create temp dir
     file: path=/tmp/special state=directory mode=0755
   - name: check private key
     stat: path=~/.ssh/id_rsa
     register: st
   - name: create key pairs
     shell:  ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa -q -b 2048 -C "com"
     when: st.stat.exists == False
   - name: get the public ke
     fetch:
       src: ~/.ssh/id_rsa.pub
       dest: /tmp/special/

- hosts: server1
  user: sdev
  sudo: yes
  gather_facts: no

  tasks:
   - name: get key list
     shell: cat  /tmp/special/*/root/.ssh/id_rsa.pub
     connection: local
     register: key_list
   - name: add keys
     lineinfile:
      dest: /root/.ssh/authorized_keys
      line: "{{ item }}"
     with_items: "{{ key_list.stdout_lines }}"
   - name: remove old keys
     file:  path=/tmp/special state=absent
     connection: local


此playbook 实现了 创建key pairs时候,如果key pair已经创建,就不会再创建新的key pair;

在添加pub key时 ,如果pub key已经在文件 authorized_keys存在,不会重复添加。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值