---

- name: provision a new vm
  ovirt_vm:
    auth: "{{ ovirt_auth }}"
    name: "{{ template.name }}"
    cluster: "{{ providers.ovirt.cluster|default('Default') }}"
    state: present
    wait: yes
    memory: "{{ template.memory }}MiB"
    cpu_sockets: "{{ template.cpu }}"
    boot_devices:
      - hd
      - cdrom
    cd_iso: "{{ template.cd_iso }}"
    type: server
    high_availability: true
    nics:
      - name: nic1
        profile_name: "{{ template.networks[0].name }}"
        network: "{{ template.networks[0].name }}"
    custom_properties: "{{ custom_properties | default(omit) }}"
    operating_system: "{{ template_vm_guest_id | default(omit) }}"
    timezone: "{{ template_timezone | default(omit) }}"
  async: 7200
  poll: 0
  register: deploy

- name: wait for instance creation to complete
  async_status: jid="{{ deploy.ansible_job_id }}"
  register: instance
  until: instance.finished
  retries: "{{ instance_wait_retry_limit }}"
  delay: 10

- name: create a disk
  ovirt_disk:
    auth: "{{ ovirt_auth }}"
    name: "{% if item.name_prefix | default(false) %}{{ template.name }}_{% endif %}{{ item.name }}"
    vm_name: "{{ template.name }}"
    size: "{{ item.size | default(omit) }}"
    format: "{{ item.format | default(omit) }}"
    interface: "{{ item.interface | default(omit) }}"
    bootable: "{{ item.bootable | default(omit) }}"
    storage_domain: "{{ item.storage_domain | default(omit) }}"
    activate: yes
    state: present
    wait: yes
  async: 7200
  poll: 0
  register: create_disks
  loop: "{{ template.disks }}"
  when:
    - template is defined
    - template.disks is defined

- name: wait for disk creation to complete
  async_status:
    jid: "{{ item.ansible_job_id }}"
  register: disks_creation
  until: disks_creation.finished
  retries: "{{ instance_wait_retry_limit }}"
  delay: 10
  loop: "{{ create_disks.results }}"
  when:
    - template is defined
    - create_disks.results is defined
    - item.ansible_job_id is defined

- include_tasks: wait_disk_unlock_pre29.yml
  when:
    - ansible_version.full is version('2.9', '<')
    - template is defined
    - template.disks is defined
    - disks_creation.results is defined

- include_tasks: wait_disk_unlock.yml
  when:
    - ansible_version.full is version('2.9', '>=')
    - template is defined
    - template.disks is defined
    - disks_creation.results is defined

- name: assign tags to provisioned vms
  ovirt_tag:
    name: "{{ item }}_{{ instance.item.item[item] }}"
    vms: ["{{ instance.item.item.name }}"]
    state: attached
  loop:
    - app_name
    - role
  when:
    - template is defined
    - instance is defined
    - instance.vm is defined
    - instance.item.item[item] is defined

- name: start vm
  ovirt_vm:
    auth: "{{ ovirt_auth }}"
    name: "{{ template.name }}"
    cluster: "{{ providers.ovirt.cluster|default('Default') }}"
    state: running
  async: 7200
  poll: 0
  register: start

- name: wait for instance creation to complete
  async_status: jid="{{ start.ansible_job_id }}"
  register: instance
  until: instance.finished
  retries: "{{ instance_wait_retry_limit }}"
  delay: 10

- name: waiting for server to come online
  wait_for:
    host: "{{ template.networks[0].ip }}"
    port: "{{ template.ansible_port | default(vm_ansible_port) | default(ansible_port) | default('5986') }}"
    timeout: "{{ instance_wait_connection_timeout }}"
  when:
    - instance is changed
    - template is defined
  ignore_errors: yes