--- # Helper playbook to create an ISO containing autounattend.xml # This ISO can be mounted to the VM during installation # # Variables: # vm_name: Name of the VM (required, must match provision-vm.yml) # # Usage: # ansible-playbook create-autounattend-iso.yml -e vm_name=WEB01 - name: Create AutoUnattend ISO hosts: hyperv gather_facts: false vars: vm_path: "{{ vm_storage_path }}\\{{ vm_name }}" autounattend_path: "{{ vm_storage_path }}\\{{ vm_name }}\\autounattend.xml" autounattend_iso_path: "{{ vm_storage_path }}\\{{ vm_name }}\\autounattend.iso" tasks: - name: Validate required variables ansible.builtin.assert: that: - vm_name is defined - vm_name | length > 0 fail_msg: "vm_name is required" - name: Check if autounattend.xml exists ansible.windows.win_stat: path: "{{ autounattend_path }}" register: autounattend_file - name: Verify autounattend.xml exists ansible.builtin.assert: that: - autounattend_file.stat.exists fail_msg: "AutoUnattend.xml not found at {{ autounattend_path }}. Run provision-vm.yml first." - name: Create ISO from autounattend.xml using oscdimg ansible.windows.win_shell: | # Use oscdimg from Windows ADK (if installed) # Otherwise, use PowerShell alternative $adkPath = "C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\amd64\Oscdimg" $oscdimg = "$adkPath\oscdimg.exe" if (Test-Path $oscdimg) { # Create ISO using oscdimg & $oscdimg -m -n -b"$adkPath\efisys.bin" ` "{{ vm_path }}" "{{ autounattend_iso_path }}" } else { # Alternative: Use PowerShell to create ISO (requires ImDisk or similar) # For now, provide instructions Write-Host "oscdimg not found. Windows ADK required for automated ISO creation." Write-Host "" Write-Host "Alternative methods:" Write-Host "1. Install Windows ADK: https://docs.microsoft.com/en-us/windows-hardware/get-started/adk-install" Write-Host "2. Use ImgBurn or similar tool manually" Write-Host "3. Mount autounattend.xml as secondary CD drive during install" Write-Host "" Write-Host "To mount as secondary drive:" Write-Host 'Add-VMDvdDrive -VMName "{{ vm_name }}" -Path "path\to\autounattend.iso"' exit 1 } register: iso_create failed_when: iso_create.rc != 0 and iso_create.rc != 1 - name: Check if ISO was created ansible.windows.win_stat: path: "{{ autounattend_iso_path }}" register: iso_file when: iso_create.rc == 0 - name: Mount AutoUnattend ISO to VM ansible.windows.win_shell: | # Add a second DVD drive with the autounattend ISO Add-VMDvdDrive -VMName "{{ vm_name }}" -Path "{{ autounattend_iso_path }}" when: - iso_file is defined - iso_file.stat.exists | default(false) - name: Display results ansible.builtin.debug: msg: - "AutoUnattend ISO creation results:" - "Source: {{ autounattend_path }}" - "ISO: {{ autounattend_iso_path }}" - "Status: {{ 'Created and mounted' if (iso_file.stat.exists | default(false)) else 'Manual intervention required' }}" - "" - "If automatic creation failed:" - "1. Copy {{ autounattend_path }} to the root of a blank CD/ISO" - "2. Mount the ISO as a second DVD drive to the VM" - "3. Windows Setup will automatically detect and use it"