Ansible 可以部署 public SSH 密钥只询问一次密码吗?

Can Ansible deploy public SSH key asking password only once?

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

第一次尝试:

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

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



编辑: 我已经使用 .[=20= 中的以下剧本成功地将我的 SSH public 密钥复制到多个远程主机]

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

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

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

你为什么不使用 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