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



我明白,当运行带有公钥身份验证的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需要的那个的名字,因为这是个人用户配置的东西。

是否有一种方法,使SSH命令由Ansible尝试多个密钥像SSH的设计做?

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代理中存在的任何密钥。