Skip to main content

oVirt - Windows Template

To create an Ansible playbook that deploys a Windows VM from a template on oVirt, customizes the OS with sysprep, sets unique hostname and static IP, and performs other specified configurations, follow the structure below. This example assumes you have a sysprep file ready for Windows customization and your oVirt environment is properly set up for Ansible integration.

First, ensure you have the ovirt.ovirt collection installed, which includes modules for interacting with oVirt. If not, you can install it using Ansible Galaxy:

ansible-galaxy collection install ovirt.ovirt

Here's an example playbook that meets your requirements. You'll need to adjust variables and possibly the paths to files (like the sysprep file) to match your environment.

- name: Deploy and customize a Windows VM on oVirt
  hosts: localhost
  gather_facts: no
    - ovirt.ovirt

    ovirt_engine_username: admin@internal
    ovirt_engine_password: your_password
    ovirt_engine_cafile: /path/to/your/
    vm_domain: ""
    vm_subnet: ""
    vm_gateway: ""
    vm_dns: ""    
    additional_disk_size: 20GB
      - { name: dc01, memory: 4GiB, cluster: kvm_worker, template: Windows2022Core, datasize: 90, storage: data-kvm2, tag: lab, ip:}
      - { name: dc02, memory: 4GiB, cluster: kvm_worker, template: Windows2022Core, datasize: 90, storage: data-kvm2, tag: lab, ip:}
      - { name: wadm01, memory: 8GiB, cluster: kvm_worker,  template: Windows2022, datasize: 90, storage: data-kvm2, tag: lab, ip:}

    - name: Log into oVirt
        url: "{{ ovirt_engine_url }}"
        username: "{{ ovirt_engine_username }}"
        password: "{{ ovirt_engine_password }}"
        ca_file: "{{ ovirt_engine_cafile }}"
        state: present

    - name: Deploy VMs
        auth: "{{ ovirt_auth }}"
        name: "{{ }}.{{ vm_domain }}"
        template: "{{ item.template }}"
        cluster: "{{ item.cluster }}"
        cpu_cores: 2
        cpu_sockets: 1
        memory: "{{ item.memory }}"
          hostname: "{{ | upper}}"
          ip: "{{ item.ip }}"
          netmask: "{{ vm_subnet }}"
          gateway: "{{ vm_gateway }}"
          dns_servers: "{{ vm_dns }}"
          domain: "{{ vm_domain }}"
          root_password: "{{ vm_admin }}"  
        state: present
        - "{{ machines }}"

    - name: Add Software Storage
        auth: "{{ ovirt_auth }}"
        name: "{{ }}-Disk2"
        vm_name: "{{ }}.{{ vm_domain }}"
        size: "{{ item.datasize }}GiB"
        format: cow
        interface: virtio_scsi
        storage_domain: "{{ }}"
        - "{{ machines }}"

    - name: Start VMs
        auth: "{{ ovirt_auth }}"
        name: "{{ }}.{{ vm_domain }}"
        state: running
        - "{{ machines }}"

    - name: Tag machines
        auth: "{{ ovirt_auth }}"
        name: "{{ item.tag }}"
        state: attached
          - "{{ }}.{{ vm_domain }}"
        - "{{ machines }}"

    # Assuming the VM is to be powered on after setup
    - name: VMs should be running
        auth: "{{ ovirt_auth }}"
        name: "{{ vm_hostname }}"
        state: running

    - name: Logout from oVirt
        state: absent
        auth: "{{ ovirt_auth }}"

Remember to replace placeholders (like URLs, credentials, paths, domain names, and the storage domain) with your actual data. Also, ensure your sysprep file is correctly set up in your template or specified directly in the playbook if needed.

This playbook performs the following actions:

  1. Logs into the oVirt engine.
  2. Creates a VM from a specified template with a unique hostname and configures it with sysprep.
  3. Adds an additional 100GB disk to the VM.
  4. Configures the VM's network interface.
  5. Powers on the VM after setup.
  6. Logs out from the oVirt engine.

Test this playbook in a development environment before using it in production. Adjustments may be necessary based on your specific oVirt setup, Windows template, and network configuration.