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?
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 .
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.
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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With