2017-06-22 111 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