Ansible 可以配置为尝试多个 SSH 密钥吗?

时间:2021-02-09 12:43:12

标签: ssh ansible

我知道在运行带有公钥身份验证的 SSH 命令时,客户端将尝试它知道的所有 SSH 密钥,直到主机接受一个 (https://security.stackexchange.com/questions/182804/how-does-ssh-know-which-public-key-to-use-from-authorized-keys)。

在使用 SSH 的主机上运行 Ansible 命令时,似乎没有此功能:Ansible 需要在 ansible.cfg 中明确指定 SSH 私钥文件:

private_key_file = /user/.ssh/id_rsa_mykey

在我的用例中,Ansible 在 Lando 上的 docker 容器内运行。所有 SSH 密钥都从用户的 ssh 配置目录导入到容器中的已知路径。但是,我不一定知道 Ansible 所需的名称,因为这是个人用户配置的内容。

有没有办法让 Ansible 发出的 SSH 命令像 SSH 设计的那样尝试多个密钥?

1 个答案:

答案 0 :(得分:2)

<块引用>

Ansible 需要在 ansible.cfg 中明确指定 SSH 私钥文件:

Ansible 要求您在 ansible.cfg 中提供私钥文件。由于 ansible 只是调用 ssh,因此配置连接凭据的首选位置是在您的 ~/.ssh/config 文件中。在那里,您可以配置多个特定于主机的密钥:

Host host1
  IdentityFile ~/.ssh/key-for-host1

Host host2
  IdentityFile ~/.ssh/key-for-host2

或者您可以将其配置为按顺序尝试多个键:

Host *.example.com
  IdentityFile ~/.ssh/maybe-this-one
  IdentityFile ~/.ssh/okay-how-about-this-instead

当然,ssh 将使用您的 ssh 代理中存在的任何密钥。

相关问题