解决 Ansible 报错 Failed to connect via bastion 跳板机代理不可达修复
排查 Ansible 跨 Bastion 跳板机访问私网主机抛出 UNREACHABLE! ProxyCommand failed,分析 ssh_common_args 与 ProxyJump 配置。
问题现象
通过跳板机连接隔离子网内部机器时 Ansible 报错中断:
fatal: [10.0.2.15]: UNREACHABLE! => {"msg": "Failed to connect to the host via ssh: kex_exchange_identification: Connection closed by remote host\r\nssh_exchange_identification: Connection closed by remote host"}
触发根因
- 跳板机(Bastion Host)拒绝了 SSH 端口转发或缺少对私网主机的网络路由;
- Ansible 中配置的
ProxyJump语法不正确,或跳板机与目标主机需要不同的 SSH 私钥。
解决步骤
在 ansible.cfg 中配置健壮的 ProxyJump 参数
编辑配置文件:
[ssh_connection]
ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o ProxyJump=jumpuser@bastion.example.com:22
为不同节点分配独立的跳板机连接参数
在 group_vars/all.yml 中配置:
ansible_ssh_common_args: '-o ProxyJump=jumpuser@bastion.example.com:22 -o IdentityFile=~/.ssh/internal_rsa'
验证确认
使用 ad-hoc 探测:
ansible internal_nodes -m ping
私网节点秒级返回 "ping": "pong",双跳连接稳定建立。


