0

I would like to use Ansible to create a Linux vm from a template and then run Ansible to configure the box. The template is just a base install without anything else. Once the VM is built the next task should be to configure the newly created VM.

My question is about the host. The VM is not created yet so what host do I run this script against? If I use the vcenter as the host then, it won't be able to configure the VM. But, if I use the IP of the host Ansible won't find it cause it is not created yet.

What would be the best approach for this?

- hosts: ???????

  tasks:

 - name:  Clone a virtual machine from Linux template and customize
    vmware_guest:
      hostname: "{{ vcenter_host }}"
      username: "{{ vcenter_user }}"
      password: "{{ vcenter_pass }}"
      validate_certs: no
      datacenter: "{{ datacenter }}"
      state: present
      #folder: /DC1/vm
      template: "{{ template }}"
      name: "{{ vm_name }}"
      cluster: "{{ cluster }}"
      networks:
        - name: VM Network
          ip: 192.168.10.11
          netmask: 255.255.255.0
      wait_for_ip_address: True
      customization:
        domain: "{{ guest_domain }}"
        dns_servers:
          - 8.9.9.9
          - 7.8.8.9
    delegate_to: localhost

 - name: configure VM
0

You can add the new VM to the inventory during the play, with a wait_for to make sure the VM is ready:

---
- hosts: localhost
  vars:
    vm: ubuntutest
  tasks:
  - name:  Clone a virtual machine from Linux template and customize
      vmware_guest:
      ... etc

  - add_host:
      name: "{{ vm }}"
      groups: examplegroup
      somecustomvar: wowamazing

  - wait_for:
      host: "{{ vm }}"
      port: 22
      delay: 3
      timeout: 320
      state: started

- hosts: examplegroup
  tasks:
  - debug:
      msg: "it works!!"

See https://docs.ansible.com/ansible/latest/modules/add_host_module.html for details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.