Hey guys, racking my brain trying to work out why my packer build is timing out at the winrm step, here is my HCL file config:
packer {
required_plugins {
azure = {
version = ">= 2.0.0"
source = "github.com/hashicorp/azure"
}
}
}
source “azure-arm” “autogenerated_1” {
azure_tags = {
dept = "Build"
task = "Image deployment"
created-by = "Packer"
OS_Version = "Windows 2019"
Release = "Latest"
}
# Azure Details
build_resource_group_name = “RESOURCE NAME”
client_id = “ID”
client_secret = “SECRET”
subscription_id = “SUB”
tenant_id = “TENANT”
vm_size = “Standard_D2_v2”
# Source Image Details
os_type = “Windows”
image_offer = “WindowsServer”
image_publisher = “MicrosoftWindowsServer”
image_sku = “2019-Datacenter”
# Managed Image Details
managed_image_name = “Win-2019-{{timestamp}}”
managed_image_resource_group_name = “RESOURCE GROUP”
# Network Settings
virtual_network_name = “VNET”
virtual_network_resource_group_name = “VNET_GROUP”
virtual_network_subnet_name = “SUBNET”
# Communicator Settings
communicator = “winrm”
winrm_insecure = true
winrm_timeout = “30m”
winrm_use_ssl = true
winrm_username = “packer”
winrm_password = “ComplexP@ssw0rd123!” "#This is an example#
custom_data = base64encode(<<-EOF
<powershell>
\# Configure WinRM
Write-Host "Configuring WinRM..."
\# Create self-signed certificate for HTTPS
$cert = New-SelfSignedCertificate -DnsName $env:COMPUTERNAME -CertStoreLocation Cert:\\LocalMachine\\My
$thumbprint = $cert.Thumbprint
\# Configure WinRM HTTPS listener
winrm create winrm/config/Listener?Address=\*+Transport=HTTPS "@{Hostname=\`"$env:COMPUTERNAME\`"; CertificateThumbprint=\`"$thumbprint\`"}"
\# Configure WinRM service
winrm set winrm/config/service '@{AllowUnencrypted="false"}'
winrm set winrm/config/service/auth '@{Basic="true"}'
winrm set winrm/config/winrs '@{MaxMemoryPerShellMB="2048"}'
\# Configure firewall
New-NetFirewallRule -DisplayName "WinRM HTTPS" -Direction Inbound -Protocol TCP -LocalPort 5986 -Action Allow
\# Restart WinRM service
Restart-Service WinRM
</powershell>
EOF
)
}
build {
sources = [“source.azure-arm.autogenerated_1”]
provisioner “powershell” {
inline = \[
"Write-Host 'Installing IIS...'",
"Add-WindowsFeature Web-Server",
"Write-Host 'Waiting for Azure services...'",
"while ((Get-Service RdAgent -ErrorAction SilentlyContinue).Status -ne 'Running') { Start-Sleep -s 5 }",
"while ((Get-Service WindowsAzureGuestAgent -ErrorAction SilentlyContinue).Status -ne 'Running') { Start-Sleep -s 5 }"
\]
}
provisioner “powershell” {
inline = \[
"Write-Host 'Running Sysprep...'",
"& $env:SystemRoot\\\\System32\\\\Sysprep\\\\Sysprep.exe /oobe /generalize /quiet /quit /mode:vm",
"while($true) { $imageState = Get-ItemProperty HKLM:\\\\SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Setup\\\\State | Select ImageState; if($imageState.ImageState -ne 'IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE') { Write-Output $imageState.ImageState; Start-Sleep -s 10 } else { break } }"
\]
}
}
The output get to just Waiting for VM to respond via WinRM. I have also tried via SSH, tried with and without SSL.
I tried with the “Administrator” account but that’s the default admin and can’t be used. Going to have gone grey at the end of this project.
Any insights would be greatly appreciated..