我有一些testing服务器在Openstack的堡垒后面运行。 testing堆栈经常被删除和创build。 创build堆栈后,我运行一组Ansible脚本来安装和configuration服务器。 我有几乎完全自动化的过程,但我似乎无法让远程主机在堡垒主机后面ssh-keyscan工作。
这是我在我的~/.ssh/config
Host bastion HostName 1.2.3.4 User myuser IdentityFile ~/.ssh/private_key.pem Host remote-host1 HostName 192.168.0.123 User myuser IdentityFile ~/.ssh/private_key.pem ProxyCommand ssh -W %h:%p bastion
如果我尝试运行ssh-keyscan remote-host1我得到
getaddrinfo remote-host1: Name or service not known
运行ssh remote-host1但会提示
The authenticity of host '192.168.0.123 (<no hostip for proxy command>)' can't be established. ECDSA key fingerprint is xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx. Are you sure you want to continue connecting (yes/no)?
我试图避免。
我知道有一个SSH选项-o StrictHostKeyChecking=no ,可以使用ssh_argsconfiguration选项将其传递给Ansible。 我不想使用它。 我也知道,使用ssh-keyscan而不检查指纹可以实现中间人攻击(man-in-the-middle)。 在这个testing环境中,我愿意承担风险,因为只有我的IP被列入白名单进行访问。
快速使用Google 提示 ssh-keyscan不尊重sshconfiguration文件和所有其他的ssh技巧。 (虽然这个线程是相当古老的)。
使用Ansible,您可以将keyscan任务委托给您的堡垒主机,然后在本地追加known_hosts文件:
- hosts: localhost gather_facts: no tasks: - command: "ssh-keyscan {{ new_host }}" register: new_host_fingerprint delegate_to: bastion - lineinfile: dest: /root/ssh/known_hosts line: "{{ item }}" with_items: new_host_fingerprint.stdout_lines
其中new_host是创build主机的IP地址(在本例中为192.168.0.123)。