鍍金池/ 問答/數(shù)據(jù)分析&挖掘  Java  網(wǎng)絡安全/ ansible建立ssh密鑰信任

ansible建立ssh密鑰信任

使用ansible批量部署服務的前提是建立免秘鑰登錄。
服務器較多時,一個一個手動使用ssh-copy-id建立免密鑰登錄也是不小的工作量。
怎樣批量建立免密鑰登錄呢?

想法1
先在本機生成公鑰,寫個腳本使用ssh-copy-id進行,這里需要知道每個ip對應密碼。

想法2
網(wǎng)上找來的

ansible init_hosts -m file -a "path=/root/.ssh owner=root group=root state=directory" -k
ansible init_hosts -m copy -a "src=authorized_keys dest=/root/.ssh/authorized_keys owner=root group=root mode=0644" -k

有如下報錯?
clipboard.png

回答
編輯回答
故林

expect+ssh-copy-id,前提知道每個ip和密碼。

2017年8月5日 15:40
編輯回答
萌小萌

自問自答吧,這里,應該是使用腳本實現(xiàn)的。

2018年2月1日 08:35
編輯回答
耍太極

除了使用 ssh-copy-id 指令進行操作外,還可以使用 authorized_key module 來使用,底下附上先前帶 workshop 時寫的簡易 playbook。

$ vim push_ssh_pub_key.yml
---

# Push the ssh public key to managed nodes.
- name: ==> push the ssh public key ...
  hosts: all
  become: true
  vars:
    username: docker

  tasks:
    - name: create ssh key directory
      file:
        path: '/home/{{ username }}/.ssh/'
        state: directory
        owner: '{{ username }}'
        group: '{{ username }}'
        mode: 0700

    - name: set authorized key took from file
      authorized_key:
        user: '{{ username }}'
        state: present
        key: "{{ lookup('file', 'files/id_rsa.pub') }}"

# vim:ft=ansible:

https://github.com/chusiang/c...

如同 @phil 提到的,在還未傳入 ssh public key 之前,是需要先使用密碼登入的,屆時您第一次需使用類似以下的指令,來覆寫 username 和 password 才行。

$ ansible-playbook push_ssh_pub_key.yml -u <USERNAME> -kK
2017年8月17日 22:12