packer { required_plugins { windows-update = { version = ">= 0.12.0" source = "github.com/rgl/windows-update" } } } source "vsphere-iso" "srv2019-template" { vcenter_server = var.vcenter_server username = var.vsphere_username password = var.vsphere_password insecure_connection = "true" vm_name = "${var.vm_guestos}-${var.vm_name}-template" datacenter = var.vsphere_datacenter host = var.vsphere_host folder = var.vsphere_folder datastore = var.vsphere_datastore guest_os_type = "windows9Server64Guest" boot_order = "disk,cdrom" boot_command = [""] boot_wait = "5m" communicator = "winrm" winrm_username = "administrator" winrm_password = var.winrm_password winrm_timeout = "10m" RAM = 8192 CPUs = 2 network_adapters { network = var.vsphere_network network_card = "vmxnet3" } storage { disk_size = 20480 disk_thin_provisioned = true } disk_controller_type = ["lsilogic-sas"] usb_controller = ["xhci"] floppy_files = [ "packer/preseed/Server2019/Autounattend.xml", "packer/preseed/Server2019/Sysprep_Unattend.xml", "scripts/Set-NetworkProfile.ps1", "scripts/Disable-WinRM.ps1", "scripts/Enable-WinRM.ps1", "scripts/Install-VMwareTools.cmd" ] iso_url = local.iso_authenticatedurl iso_checksum = var.iso_checksum iso_paths = var.iso_paths shutdown_command = "C:\\Windows\\System32\\Sysprep\\sysprep.exe /generalize /oobe /unattend:A:\\Sysprep_Unattend.xml" shutdown_timeout = "1h" export { images = false output_directory = "/scratch/srv2019-template" } remove_cdrom = true } source "vsphere-iso" "srv2019-virtual" { vcenter_server = var.vcenter_server username = var.vsphere_username password = var.vsphere_password insecure_connection = "true" vm_name = "${var.vm_guestos}-${var.vm_name}-virtual" datacenter = var.vsphere_datacenter host = var.vsphere_host folder = var.vsphere_folder datastore = var.vsphere_datastore guest_os_type = "windows9Server64Guest" boot_order = "disk,cdrom" boot_command = [""] boot_wait = "5m" communicator = "winrm" winrm_password = var.winrm_password winrm_timeout = "10m" winrm_username = "administrator" RAM = 8192 CPUs = 2 network_adapters { network = var.vsphere_network network_card = "vmxnet3" } storage { disk_size = 20480 disk_thin_provisioned = true } disk_controller_type = ["lsilogic-sas"] usb_controller = ["xhci"] floppy_files = [ "packer/preseed/Server2019/Autounattend.xml", "packer/preseed/Server2019/Sysprep_Unattend.xml", "scripts/Set-NetworkProfile.ps1", "scripts/Disable-WinRM.ps1", "scripts/Enable-WinRM.ps1", "scripts/Install-VMwareTools.cmd" ] iso_url = local.iso_authenticatedurl iso_checksum = var.iso_checksum iso_paths = var.iso_paths shutdown_command = "C:\\Windows\\System32\\Sysprep\\sysprep.exe /generalize /oobe /unattend:A:\\Sysprep_Unattend.xml" shutdown_timeout = "1h" export { images = false output_directory = "/scratch/srv2019-virtual" } remove_cdrom = true } source "vsphere-iso" "srv2019-baremetal" { vcenter_server = var.vcenter_server username = var.vsphere_username password = var.vsphere_password insecure_connection = "true" vm_name = "${var.vm_guestos}-${var.vm_name}-baremetal" datacenter = var.vsphere_datacenter host = var.vsphere_host folder = var.vsphere_folder datastore = var.vsphere_datastore guest_os_type = "windows9Server64Guest" boot_order = "disk,cdrom" boot_command = [""] boot_wait = "5m" communicator = "winrm" winrm_username = "administrator" winrm_password = var.winrm_password winrm_timeout = "10m" RAM = 8192 CPUs = 2 network_adapters { network = var.vsphere_network network_card = "vmxnet3" } storage { disk_size = 20480 disk_thin_provisioned = true } disk_controller_type = ["lsilogic-sas"] usb_controller = ["xhci"] floppy_files = [ "packer/preseed/Server2019/Autounattend.xml", "packer/preseed/Server2019/Sysprep_Unattend.xml", "scripts/Set-NetworkProfile.ps1", "scripts/Disable-WinRM.ps1", "scripts/Enable-WinRM.ps1", "scripts/Install-VMwareTools.cmd" ] iso_url = local.iso_authenticatedurl iso_checksum = var.iso_checksum iso_paths = var.iso_paths shutdown_command = "C:\\Windows\\System32\\Sysprep\\sysprep.exe /generalize /oobe /unattend:A:\\Sysprep_Unattend.xml" shutdown_timeout = "1h" export { images = false output_directory = "/scratch/srv2019-baremetal" } remove_cdrom = true } build { sources = [ "source.vsphere-iso.srv2019-template", "source.vsphere-iso.srv2019-virtual", "source.vsphere-iso.srv2019-baremetal" ] provisioner "windows-update" { } provisioner "powershell" { scripts = [ "scripts/Install-WinGet.ps1" ] } provisioner "powershell" { inline = [ "winget install mozilla.firefox", "winget install 7zip.7zip", "winget install microsoft.dotnetframework" ] // valid_exit_codes = [0, 3010] } // provisioner "powershell" { // inline = [ // "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12", // "Invoke-Expression ((New-Object Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" // ] // } // provisioner "powershell" { // inline = [ // "choco config set --name=limit-output --value=LimitOutput", // "choco install -y dotnetfx", // "choco install -y 7zip.install", // "choco install -y sysinternals", // "choco install -y firefox" // ] // valid_exit_codes = [0, 3010] // } provisioner "windows-update" { } provisioner "powershell" { only = ["vsphere-iso.srv2019-virtual"] inline = [ "New-Item -Path 'C:\\Payload\\Scripts' -ItemType 'Directory' -Force:$True -Confirm:$False" ] } provisioner "file" { only = ["vsphere-iso.srv2019-virtual"] destination = "C:\\Payload\\" source = "scripts/Server2019/payload/" } provisioner "powershell" { only = ["vsphere-iso.srv2019-virtual"] scripts = [ "scripts/Server2019/Register-ScheduledTask.ps1" ] } post-processors { post-processor "vsphere" { only = ["vsphere-iso.srv2019-template"] host = var.vcenter_server username = var.vsphere_username password = var.vsphere_password insecure = true vm_name = "Windows-Server-2019-LTSC" datacenter = var.vsphere_datacenter cluster = var.vsphere_host vm_folder = var.vsphere_templatefolder vm_network = var.vsphere_network datastore = var.vsphere_datastore disk_mode = "thin" keep_input_artifact = true overwrite = true } post-processor "vsphere-template" { only = ["vsphere-iso.srv2019-template"] host = var.vcenter_server username = var.vsphere_username password = var.vsphere_password insecure = true datacenter = var.vsphere_datacenter folder = var.vsphere_templatefolder reregister_vm = false } } post-processor "shell-local" { only = ["vsphere-iso.srv2019-virtual"] inline = [ "pwsh -command \"& scripts/Update-OvfConfiguration.ps1 \\", " -OVFFile '/scratch/srv2019-virtual/${var.vm_guestos}-${var.vm_name}-virtual.ovf' \\", " -Parameter @{'appliance.name'='${var.vm_guestos}';'appliance.version'='${var.vm_name}'}\"", "pwsh -file scripts/Update-Manifest.ps1 \\", " -ManifestFileName '/scratch/srv2019-virtual/${var.vm_guestos}-${var.vm_name}-virtual.mf'", "ovftool --acceptAllEulas --allowExtraConfig --overwrite \\", " '/scratch/srv2019-virtual/${var.vm_guestos}-${var.vm_name}-virtual.ovf' \\", " /output/Windows-Server-2019-LTSC.ova" ] } post-processor "shell-local" { only = ["vsphere-iso.srv2019-baremetal"] inline = [ "qemu-img convert -f vmdk -O raw \\", " /scratch/srv2019-baremetal/${var.vm_guestos}-${var.vm_name}-baremetal-disk-0.vmdk \\", " /tmp/${var.vm_guestos}-${var.vm_name}.raw", "gzip -c \\", " /tmp/${var.vm_guestos}-${var.vm_name}.raw \\", " > /output/Windows-Server-2019-LTSC.raw.gz" ] } }