How to use ansible to run commands through a remote server to another remote server

Viewed 1222

In our work setup , there is a remote server B which is accessible only via a remote server A .

  • How can i run ansible commands/playbooks on remote server B through remote server A from my local system where ansible runs ,ie, local system --> remote server A --> remote server B
  • The remote server B is accessible via remote server A through ssh . But i do not have access to the ssh keys to remote server B

this is what i tried to do in my inventory.yaml file based on the answer below

  hosts:
    remote-serverB:
  vars:
    ansible_connection: "ssh"
    ansible_user: "userB"
    ansible_ssh_common_args: '-o ProxyCommand="sshpass -p <password> ssh -W %h:%p -q userA@remote-serverA"' 

but i get the following error from ansible

UNREACHABLE  {"changed": false, "msg": "EOF on stream; last 100 lines received:\nssh_exchange_identification: Connection closed by remote host\r", "unreachable": true}
1 Answers

This could be done via the ansible_ssh_common_args.

ansible_ssh_common_args
This setting is always appended to the default command line for sftp, scp, and ssh. Useful to configure a ProxyCommand for a certain host (or group).

Source: https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#connecting-to-hosts-behavioral-inventory-parameters

Here is an example of inventory using this configuration, given that my two servers have, as FQDN

  • remote-server-A
  • remote-server-B

and that the user to login to remote-server-A is user-to-A.

all:
  servers-needing-jump:
    hosts:
      remote-server-B:
  vars:
    ansible_ssh_common_args: '-o ProxyCommand="ssh -W %h:%p -q user-to-A@remote-server-A"'
Related