> For the complete documentation index, see [llms.txt](https://close.gitbook.io/yun-wei-bi-ji/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://close.gitbook.io/yun-wei-bi-ji/centos/ansible/ansible-ru-he-tong-guo-tiao-ban-ji-lian-jie-mu-biao-ji-qi.md).

# Ansible如何通过跳板机连接目标机器

#### Ansible如何通过跳板机连接目标机器

**修改本地机器ssh的配置文件\~/.ssh/config**

> 没有就手动创建一个

**本地IP: 192.255.235.20**

```bash
# 方法一
Host bastion
    HostName 192.255.235.63      # 跳板机IP
    BatchMode yes
    User ubuntu

Host prod1
    HostName 172.16.105.45
    ServerAliveInterval 60
    TCPKeepAlive        yes
    IdentityFile ~/.ssh/keys/bastion_id_rsa    # 跳板机上的 免密 文件
    ProxyCommand ssh bastion 'nc -w 14400 %h %p' # or ProxyCommand ssh -W %h:%p bastion 
    User ubuntu
    Port 22

# 方法二
Host prod2
    HostName 150.138.xx.xxx
    IdentityFile ~/.ssh/id_rsa  # 跳板机上的 免密 文件
    ProxyCommand   ssh -qaY -i ~/.ssh/id_rsa -p 22 跳板机用户@跳板机IP  'nc -w 14400ms %h %p'
    Port 10666
    User root
```

**测试**

```ssh
ssh prod1
ssh prod2
```

```bash
# hosts
[prod]
prod2


# 配置完成之后测试 ansible 是否能连接到机器
ansible prod -m ping
```

### 另一种方式

```base
[prod]
ansible_ssh_common_args='-o ProxyCommand="/usr/bin/ncat --proxy ip:port --proxy-type http %h %p"'
```
