Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSH to remote server using ansible

Tags:

ssh

ansible

I'm using ansible to automate some tasks. One of those requires me to ssh to server A, then to B from A and then to C from B. I can't seem to find any way to get ansible to do that. Any suggestions?

like image 644
K G Avatar asked Dec 26 '14 20:12

K G


People also ask

How do I connect Ansible to SSH?

Setting up SSH keys By default, Ansible assumes you are using SSH keys to connect to remote machines. SSH keys are encouraged, but you can use password authentication if needed with the --ask-pass option. If you need to provide a password for privilege escalation (sudo, pbrun, and so on), use --ask-become-pass .

How do I connect to Ansible remote server?

For authenticating while connecting to the remote hosts we have two options, either we need to specify the userId and the password in the ansible command . or we can connect via SSH key. The above command simply pings all the target machine that we have specified in the hosts file.

How does Ansible work with SSH?

This connection plugin allows Ansible to communicate to the target machines through normal SSH command line. Ansible does not expose a channel to allow communication between the user and the SSH process to accept a password manually to decrypt an SSH key when using this connection plugin (which is the default).


1 Answers

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_connection]
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
  HostName real-host-name-A.com

Host HostB
  HostName real-host-name-B.com
  ProxyCommand ssh -q HostA nc %h %p

Host HostC
  HostName real-host-name-C.com
  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
like image 59
udondan Avatar answered Oct 19 '22 09:10

udondan