2017-06-22 110 views
0

我想知道如何將我的SSH公鑰複製到使用Ansible的許多主機上。可以Ansible只公開SSH密鑰詢問密碼一次嗎?

第一次嘗試:

ansible all -i inventory -m local_action -a "ssh-copy-id {{ inventory_hostname }}" --ask-pass 

但我有錯誤The module local_action was not found in configured module paths

使用劇本

第二次嘗試:

- hosts: all 
    become: no 
    tasks: 
    - local_action: command ssh-copy-id {{ inventory_hostname }} 

最後,我已經進入了我的密碼,每個託管主機:

ansible all -i inventory --list-hosts | while read h ; do ssh-copy-id "$h" ; done 

如何填寫密碼只有一次,同時部署SSH公鑰到多臺主機?



編輯:  我已經成功了我的SSH公鑰複製到使用從Konstantin Suvorov's answer以下劇本多個遠程主機。

- hosts: all 
    tasks: 
    - authorized_key: 
     key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}" 

領域user應根據documentation是強制性的,但它似乎沒有工作。

ansible-playbook -i inventory authorized_key.yml -u "$USER" -k 

回答

3

你爲什麼不使用authorized_key模塊:因此使用此命令行中使用上述通用的劇本可以被用於任何用戶?

- hosts: all 
    tasks: 
    - authorized_key: 
     user: remote_user_name 
     state: present 
     key: "{{ lookup('file', '/local/path/.ssh/id_rsa.pub') }}" 

和運行劇本與-u remote_user_name -k