How to execute commands on a VM running on a remote host?

Hello Guys,

I have Ansible installed on a jumphost. From the jumphost I can reach several servers (with different IPs), each running a virtual machine (that VM has the same IP on each server).
I need to read the information of a certain file that is located on the virtual machines (not on the servers).
That means Ansible have to establish a SSH connection to the server and from there it have to establish another SSH connection to the VM.

(Jumphost [Ansible Controller] → Server → VM)

I assume that the realization of this use case is possible with Ansible (?)
However, I don’t really know how to google for this problem.

Is there some kind of best practice for this use case?

Maybe you can provide me a link, which explains how to solve that problem or a similar problem?

I am grateful for any help :slight_smile:

Kind regards,
Peter

https://docs.ansible.com/ansible/latest/reference_appendices/faq.html#how-do-i-configure-a-jump-host-to-access-servers-that-i-have-no-direct-access-to

From your story it seems that what you call jumphost is the ansible controller and each server is a jumphost.

Hello,

thank you for your reply. I think you can say it like you wrote it, yes.

So if I understand the given link correctly:

I need to specify my servers in this line:
#ansible_ssh_common_args: ‘-o ProxyCommand=“ssh -W %h:%p -q user@gateway.example”’

While the inventory file contains the IP of the virtual machine located on the servers
#[VM] foo ansible_host=192.168.1.1

The problem is that my Servers (the jumhosts as you’ve called it) have different IPs, but the VM located on the server always have the same IP.
This is like a reverse use case (?)

So I somehow need to parse my (different) servers to “user@gateway.example”.

Any idea how I can realize this? Probably use a variable for “gateway.example.com” and loop through that variable?

Best regards
Peter

I think in this case you'd have to set a custom
ansible_ssh_common_args for each host in your inventory, for instance:

foo ansible_host=192.168.1.1 ansible_ssh_common_args='-o
ProxyCommand="ssh -W %h:%p -q user@VMhost1"'
bar ansible_host=192.168.1.1 ansible_ssh_common_args='-o
ProxyCommand="ssh -W %h:%p -q user@VMhost2"'
baz ansible_host=192.168.1.1 ansible_ssh_common_args='-o
ProxyCommand="ssh -W %h:%p -q user@VMhost3"'

Hello,

thank you again for your Input.
I have about different 100 servers using this VM

So the Inventory file would somehow look like this: (?)