2017-04-18 33 views
0

如何將SSH公鑰從每個節點複製到安全服務器?Ansible:如何將公鑰從節點複製到本地?

換句話說:make:ssh-copy-id user @ ansible_server?

我想讓它自動使用劇本文件。

我提出了一些解決方案:

- name: ssh-copy-id 
    hosts: builder 
    become: true 
    tasks: 
    - name: generate ssh keys on node 
     user: 
     name: user 
     generate_ssh_key: yes 
     ssh_key_bits: 2048 
     ssh_key_file: .ssh/id_rsa 

    - name: get content of public key (node) 
     shell: cat "/home/user/.ssh/id_rsa.pub" 
     changed_when: false 
     register: public_key 

    - name: append public key from node to local authorized_keys 
     lineinfile: 
     line: "{{ public_key.stdout }}" 
     insertafter: EOF 
     dest: /local 
     delegate_to: 127.0.0.1 
+0

開始[獲取](HTTP:// docs.ansible.com/ansible/fetch_module.html)。但說實話,你的任務有點奇怪。爲什麼你需要遠程節點的公鑰? –

+0

你爲什麼要那樣做? – techraf

+0

從節點使用rsync(複製日誌到服務器) – themis

回答

0

如果產生在同一劇本SSH密鑰,只需捕獲的結果,並使用它:

- name: generate ssh keys on node 
    user: 
    name: user 
    generate_ssh_key: yes 
    ssh_key_bits: 2048 
    ssh_key_file: .ssh/id_rsa 
    register: user_res 

- name: append public key from node to local authorized_keys 
    lineinfile: 
    line: "{{ user_res.ssh_public_key }}" 
    insertafter: EOF 
    dest: /local 
    delegate_to: 127.0.0.1 
+0

但它不會工作,因爲ssh-copy-id也會使文件:known_hosts – themis

+0

您可以隨時添加'ssh-keyscan'任務。 –

相關問題