Ansible:如何将公钥从节点复制到本地?

时间:2017-04-18 13:24:12

标签: ansible

如何将SSH公钥从每个节点复制到ansible服务器?

换句话说make:ssh-copy-id user @ ansible_server?

我想使用playbook文件自动制作。

我做了一些解决方案:

- 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

1 个答案:

答案 0 :(得分: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