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,

On Friday, November 8, 2019 at 11:42:24 AM UTC+5:30, Sandeep Sridhar wrote:
>
> 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 [email protected] <javascript:>\"" ' -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.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/cf69754e-7406-489f-a8a9-80803c320918%40googlegroups.com.

Reply via email to