running commands on guest VM from Ansible Controller

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"” ’ -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.

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,

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.