SSH to remote server using ansible

Given that you do not use Paramiko for ssh (transport = ssh), Ansible will fully use your ~/.ssh/config. Therefore you can globally define all connection rules in your ssh configuration.

If for some reason you want Ansible to not use your default ssh config but provide an separate configuration, you can define this in your ansible.cfg:

ssh_args= -F "/path/to/ssh/config/specifically/for/ansible"

In your ssh config then set up the connection rules. To stick with your example:

Host HostA

Host HostB
  ProxyCommand ssh -q HostA nc %h %p

Host HostC
  ProxyCommand ssh -q HostB nc %h %p
  • Connections to A are direct
  • Connections to B go through A
  • Connections to C go through B, which goes through A

Leave a Comment