Ansible可以部署公共SSH密钥只需要一次密码吗?

oli*_*bre 10 ssh-keys ansible ansible-2.x

我想知道如何使用Ansible将我的SSH公钥复制到许多主机.

第一次尝试:

ansible all -i inventory -m local_action -a "ssh-copy-id {{ inventory_hostname }}" --ask-pass
Run Code Online (Sandbox Code Playgroud)

但我有错误The module local_action was not found in configured module paths.

第二次尝试使用剧本:

- hosts: all
  become: no
  tasks:
  - local_action: command ssh-copy-id {{ inventory_hostname }}
Run Code Online (Sandbox Code Playgroud)

最后,我为每个托管主机输入了密码:

ansible all -i inventory --list-hosts | while read h ; do ssh-copy-id "$h" ; done
Run Code Online (Sandbox Code Playgroud)

在将公共SSH密钥部署到多台主机时,如何只填写一次密码?



编辑:   我已成功使用Konstantin Suvorov的答案中的以下剧本将我的SSH公钥复制到多个远程主机.

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

user根据文档,该字段应该是强制性的,但似乎没有.因此,当与此命令行一起使用时,上述通用playbook可用于任何用户:

ansible-playbook -i inventory authorized_key.yml -u "$USER" -k
Run Code Online (Sandbox Code Playgroud)

Kon*_*rov 13

为什么不使用authorized_key模块?

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

并运行playbook -u remote_user_name -k