【发布时间】:2017-11-26 05:52:35
【问题描述】:
我想知道如何使用 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密钥部署到多个主机时只填写一次密码?
编辑: 我已使用来自Konstantin Suvorov's answer 的以下剧本成功地将我的 SSH 公钥复制到多个远程主机。
- hosts: all
tasks:
- authorized_key:
key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
根据documentation,字段user 应该是强制性的,但它似乎没有。因此,当与此命令行一起使用时,任何用户都可以使用上述通用剧本:
ansible-playbook -i inventory authorized_key.yml -u "$USER" -k
【问题讨论】:
-
对我来说,
user变量是必需的。我已将其设置为"{{ ansible_user_id }}"以使用当前用户名,但如果需要,可以将其设置为其他名称。 -
失败了! => {"changed": false, "msg": "missing required arguments: user"}
标签: ansible ssh-keys ansible-2.x