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

**URL:** <https://forum.ansible.com/t/how-to-execute-commands-on-a-vm-running-on-a-remote-host/35378>\
**Category:** Ansible Project\
**Created:** [June 19, 2021, 5:29pm UTC](https://forum.ansible.com/t/how-to-execute-commands-on-a-vm-running-on-a-remote-host/35378 "2021-06-19T17:29:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Gebirgslok](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@Peter\_Gebirgslok](https://forum.ansible.com/u/Peter_Gebirgslok)\
**Post date:** [June 19, 2021, 5:29pm UTC](https://forum.ansible.com/t/how-to-execute-commands-on-a-vm-running-on-a-remote-host/35378/1 "2021-06-19T17:29:59Z")

</div>

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 🙂

Kind regards,  
Peter

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [June 20, 2021, 4:32am UTC](https://forum.ansible.com/t/how-to-execute-commands-on-a-vm-running-on-a-remote-host/35378/2 "2021-06-20T04:32:13Z")

</div>

[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](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.

---

<div class="post-metadata">

**Author:** ![Peter\_Gebirgslok](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@Peter\_Gebirgslok](https://forum.ansible.com/u/Peter_Gebirgslok)\
**Post date:** [June 20, 2021, 9:13am UTC](https://forum.ansible.com/t/how-to-execute-commands-on-a-vm-running-on-a-remote-host/35378/3 "2021-06-20T09:13:31Z")

</div>

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](http://gateway.example.com)” and loop through that variable?

Best regards  
Peter

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [June 20, 2021, 6:26pm UTC](https://forum.ansible.com/t/how-to-execute-commands-on-a-vm-running-on-a-remote-host/35378/4 "2021-06-20T18:26:03Z")

</div>

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"'

---

<div class="post-metadata">

**Author:** ![Peter\_Gebirgslok](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@Peter\_Gebirgslok](https://forum.ansible.com/u/Peter_Gebirgslok)\
**Post date:** [June 21, 2021, 9:00pm UTC](https://forum.ansible.com/t/how-to-execute-commands-on-a-vm-running-on-a-remote-host/35378/5 "2021-06-21T21:00:50Z")

</div>

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: (?)
