通过堡垒ssh-keyscan

Ste*_*eve 5 ssh ansible bastion

我有一些测试服务器在 Openstack 上的堡垒后面运行。测试堆栈经常被删除和创建。创建堆栈后,我运行一组 Ansible 脚本来安装和配置服务器。我的过程几乎完全自动化,但是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
Run Code Online (Sandbox Code Playgroud)

如果我尝试跑步,ssh-keyscan remote-host1我会得到

getaddrinfo remote-host1: Name or service not known
Run Code Online (Sandbox Code Playgroud)

运行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)?
Run Code Online (Sandbox Code Playgroud)

我试图避免。

我知道有一个 SSH 选项-o StrictHostKeyChecking=no,可以使用ssh_args配置选项将其传递给 Ansible 。虽然我不想使用它。我也知道在不检查指纹的情况下使用 ssh-keyscan 会允许中间人攻击。在这个测试环境场景中,我愿意承担风险,因为只有我的 IP 被列入访问白名单。

Kon*_*rov 5

快速谷歌搜索表明ssh-keyscan 不支持 ssh 配置文件和所有其他 ssh 技巧。(虽然这个线程已经很老了)。

使用 Ansible,您可以将密钥扫描任务委派给堡垒主机,然后在本地附加 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 }}"
Run Code Online (Sandbox Code Playgroud)

new_host创建的主机的 IP 地址在哪里(在您的示例中为 192.168.0.123)。