---

- name: obtain SSO token with using username/password credentials
  ovirt_auth:
    url: "{{ lookup('env', 'OVIRT_URL')|default(ovirt.url) }}"
    username: "{{ lookup('env', 'OVIRT_USERNAME')|default(ovirt.username) }}"
    password: "{{ lookup('env', 'OVIRT_PASSWORD')|default(ovirt.password) }}"
    insecure: yes

- include_tasks: preflight_check_pre29.yml
  when: ansible_version.full is version('2.9', '<')

- include_tasks: preflight_check.yml
  when: ansible_version.full is version('2.9', '>=')

# remove existing template
- block:

    - include_tasks: remove_template.yml

  when:
    - template_force|bool
    - template_found|bool

- block:
    - include_tasks: make_iso.yml

    - include_tasks: provision_vm.yml

    - name: refresh inventory
      meta: refresh_inventory

    - name: clear gathered facts
      meta: clear_facts

    - name: clear any host errors
      meta: clear_host_errors

    - name: add host
      add_host:
        hostname: template_host
        ansible_host: "{{ template_vm_ip_address }}"
        ansible_user: "{{ unattend.local_accounts[0].name }}"
        ansible_password: "{{ unattend.local_accounts[0].password }}"
        ansible_port: "{{ vm_ansible_port | default('5986') }}"
        ansible_connection: winrm
        ansible_winrm_transport: "{{ vm_ansible_winrm_transport | default('credssp') }}"
        ansible_winrm_server_cert_validation: ignore
        ansible_winrm_operation_timeout_sec: 250
        ansible_winrm_read_timeout_sec: 280
        ansible_win_async_startup_timeout: 60

    - include_role:
        name: "{{ windows_build_role }}"
        apply:
          vars:
            target_ovirt: yes
            install_updates: yes
            remove_apps: yes
            clean_up_components: yes
            upgrade_powershell: "{{ vm_upgrade_powershell | default('no') }}"
          delegate_to: template_host

    - name: refresh SSO credentials
      ovirt_auth:
        url: "{{ lookup('env', 'OVIRT_URL')|default(ovirt.url) }}"
        username: "{{ lookup('env', 'OVIRT_USERNAME')|default(ovirt.username) }}"
        password: "{{ lookup('env', 'OVIRT_PASSWORD')|default(ovirt.password) }}"
        insecure: yes

    - include_tasks: stop_vm.yml

    - include_tasks: convert_to_template.yml

    - include_tasks: export_ovf.yml
      when: export_ovf|bool

  rescue:
    - name: refresh SSO credentials
      ovirt_auth:
        url: "{{ lookup('env', 'OVIRT_URL')|default(ovirt.url) }}"
        username: "{{ lookup('env', 'OVIRT_USERNAME')|default(ovirt.username) }}"
        password: "{{ lookup('env', 'OVIRT_PASSWORD')|default(ovirt.password) }}"
        insecure: yes

    - include_tasks: remove_template.yml
      when: remove_vm_on_error|bool

    - name: set vm_failed variable
      set_fact:
        vm_failed: yes

  always:
    - name: refresh SSO credentials
      ovirt_auth:
        url: "{{ lookup('env', 'OVIRT_URL')|default(ovirt.url) }}"
        username: "{{ lookup('env', 'OVIRT_USERNAME')|default(ovirt.username) }}"
        password: "{{ lookup('env', 'OVIRT_PASSWORD')|default(ovirt.password) }}"
        insecure: yes

    - include_tasks: remove_vm.yml
      when: remove_vm_on_error|bool or (not remove_vm_on_error|bool and not vm_failed|bool)

    - include_tasks: datastore_iso_remove.yml

    - name: remove temporary directory
      file:
        path: "{{ temp_directory }}"
        state: absent

    - name: logout from oVirt
      ovirt_auth:
        state: absent
        ovirt_auth: "{{ ovirt_auth }}"

- name: fail if needed
  fail:
    msg: "fail to create a template."
  when: vm_failed|bool