# running commands on guest VM from Ansible Controller

**URL:** <https://forum.ansible.com/t/running-commands-on-guest-vm-from-ansible-controller/31622>\
**Category:** Ansible Project\
**Created:** [November 8, 2019, 6:12am UTC](https://forum.ansible.com/t/running-commands-on-guest-vm-from-ansible-controller/31622 "2019-11-08T06:12:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sandeep\_Sridhar](https://avatars.discourse-cdn.com/v4/letter/s/ba9def/32.png) [@Sandeep\_Sridhar](https://forum.ansible.com/u/Sandeep_Sridhar)\
**Post date:** [November 8, 2019, 6:12am UTC](https://forum.ansible.com/t/running-commands-on-guest-vm-from-ansible-controller/31622/1 "2019-11-08T06:12:24Z")

</div>

I have an Ansible controller and a remote host. On the remote host, we have created a guest VM instance. All of this is achieved through a playbook running on ansible controller.

The guest VM does not have network connectivity to the ansible controller. The guest VM can only be accessed through the remote host.

This is my hosts file on the ansible controller:

[root@mltr mltr\_source\_code]# cat hosts  
[server]  
10.204.74.245

[localserver]  
127.0.0.1

[vms:vars]  
ansible\_connection=ssh  
ansible\_ssh\_common\_args='“-oStrictHostKeyChecking=no -oProxyCommand="ssh -A -W %h:%p [root@10.204.74.245](mailto:root@10.204.74.245)"” ’ -e ansible\_ssh\_user=root -e ansible\_ssh\_pass=contrail

The IP of the guest VM is dynamic in nature (it may differ each time you run the playbook as the playbook creates it), Therefore, I am using add\_host module to add the IP address to the in-memory inventory.

- name: Add the ucloud\_vm created into in-memory of ansible hosts.  
vars:  
user\_data: “{{ lookup(‘file’, ‘/root/mltr\_source\_code/mini.ip’) }}”  
add\_host:  
name: ‘{{ user\_data }}’  
groups: ucloud\_vm\_group  
debug:  
var: hostvars[ucloud\_vm\_group]  
verbosity: 4

Now, I would like to execute the next set of tasks/commands on the guest VM. How do I do it?

A sample example of a task to run commands on guest VM will be really helpful.

Greetings,  
Sandeep.

---

<div class="post-metadata">

**Author:** ![Akasurde](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akasurde/32/230_2.png) [@Akasurde](https://forum.ansible.com/u/Akasurde)\
**Post date:** [November 8, 2019, 9:02am UTC](https://forum.ansible.com/t/running-commands-on-guest-vm-from-ansible-controller/31622/2 "2019-11-08T09:02:29Z")

</div>

Hi Sandeep,

You can write two plays to handle this situation. First play will add the host and the second play will use the in-memory inventory generated by first play to execute commands on newly added host.

So basically, your playbook will look like -

- name: Deploy VM and add in ucloud\_vm\_group  
hosts: localhost  
tasks:

- add\_host:  
name: ‘{{ user\_data }}’  
groups: ucloud\_vm\_group  
…

- name: Configure newly added hosts  
hosts: ucloud\_vm\_group  
tasks:

- apt:  
name: git  
state: latest

I have removed details for the sake simplicity. Let me know if you have any questions.

Thanks,

---

<div class="post-metadata">

**Author:** ![Sandeep\_Sridhar](https://avatars.discourse-cdn.com/v4/letter/s/ba9def/32.png) [@Sandeep\_Sridhar](https://forum.ansible.com/u/Sandeep_Sridhar)\
**Post date:** [November 8, 2019, 10:27am UTC](https://forum.ansible.com/t/running-commands-on-guest-vm-from-ansible-controller/31622/3 "2019-11-08T10:27:18Z")

</div>

Hi Abhijeet,

Great help again. Thanks. This is what I have now:

- name: Add the ucloud\_vm created into in-memory of ansible hosts.  
vars:  
user\_data: “{{ lookup(‘file’, ‘/root/mltr\_source\_code/mini.ip’) }}”  
tasks:
- add\_host:  
name: ‘{{ user\_data }}’  
groups: ucloud\_vm\_group  
…
- name: Configure newly added hosts  
hosts: ucloud\_vm\_group  
tasks:  
shell: touch /root/hello.txt

I get this when I run the playbook:

ansible-playbook 2.8.0  
config file = /root/mltr\_source\_code/ansible.cfg  
configured module search path = [u’/root/.ansible/plugins/modules’, u’/usr/share/ansible/plugins/modules’]  
ansible python module location = /usr/lib/python2.7/site-packages/ansible  
executable location = /usr/bin/ansible-playbook  
python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]  
Using /root/mltr\_source\_code/ansible.cfg as config file  
host\_list declined parsing /root/mltr\_source\_code/hosts as it did not pass it’s verify\_file() method  
script declined parsing /root/mltr\_source\_code/hosts as it did not pass it’s verify\_file() method  
auto declined parsing /root/mltr\_source\_code/hosts as it did not pass it’s verify\_file() method  
toml declined parsing /root/mltr\_source\_code/hosts as it did not pass it’s verify\_file() method  
[WARNING]: \* Failed to parse /root/mltr\_source\_code/hosts with yaml plugin: Syntax Error while loading YAML. did not find  
expected The error appears to be in ‘/root/mltr\_source\_code/hosts’: line 2, column 1, but may be elsewhere in  
the file depending on the exact syntax problem. The offending line appears to be: [server] 10.204.74.245 ^ here

File “/usr/lib/python2.7/site-packages/ansible/inventory/manager.py”, line 267, in parse\_source  
plugin.parse(self.\_inventory, self.\_loader, source, cache=cache)  
File “/usr/lib/python2.7/site-packages/ansible/plugins/inventory/yaml.py”, line 105, in parse  
raise AnsibleParserError(e)

[WARNING]: \* Failed to parse /root/mltr\_source\_code/hosts with ini plugin: /root/mltr\_source\_code/hosts:7: Section [vms:vars]  
not valid for undefined group: vms

File “/usr/lib/python2.7/site-packages/ansible/inventory/manager.py”, line 267, in parse\_source  
plugin.parse(self.\_inventory, self.\_loader, source, cache=cache)  
File “/usr/lib/python2.7/site-packages/ansible/plugins/inventory/ini.py”, line 138, in parse  
raise AnsibleParserError(e)

[WARNING]: Unable to parse /root/mltr\_source\_code/hosts as an inventory source

[WARNING]: No inventory was parsed, only implicit localhost is available

[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match ‘all’

ERROR! Syntax Error while loading YAML.  
could not find expected ‘:’

The error appears to be in ‘/root/mltr\_source\_code/provision\_undercloud.yml’: line 255, column 4, but may  
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

- name: Configure newly added hosts  
^ here

Greetings,  
Sandeep.
