Create asure win11 packer with provisiner installing WSL & ubuntu

Hi all,

I am quite new with the packer/asure things and I am trying to install wsl with an ubuntu in the asure vm with the packer. Unfortunatelly cannot run the wsl.exe file even after installing everything. Other .exe files like curl.exe for dowloading packages is working perfectly and I expected that the wsl.exe should also work.

Does anyone have some idea what can be the problem or is it even possible to run wsl.exe on an asure VM?

.
.
build {
  sources = ["source.azure-arm.win11-aarch"]

  provisioner "powershell" {
    inline = [
    # install chocolatey
    "Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; $env:chocolateyVersion = '1.4.0'; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))",
    # Enable Microsoft-Windows-Subsystem-Linux
    "Enable-WindowsOptionalFeature -Online -FeatureName \"Microsoft-Windows-Subsystem-Linux\" -All -NoRestart",
    # Enable VirtualMachinePlatform
    "Enable-WindowsOptionalFeature -Online -FeatureName \"VirtualMachinePlatform\" -All -NoRestart"
    ]
  }

  provisioner "windows-restart" {
    restart_check_command = "powershell -command \"& {Write-Output 'restarted.'}\""
  }

# install/enable wsl1
provisioner "powershell" {
    inline = [
    "& C:\\ProgramData\\chocolatey\\bin\\choco install -y wsl2 --params \"/Version:1\"",
    "$wslIntalled = $false",
    "if (Get-Command wsl.exe -ErrorAction SilentlyContinue) {$wslIntalled = $true}",
    "$wslIntalled",
    "if (!$wslIntalled) {Write-Error \"WSL not detected! WSL is needed to install...\"}"
    ]
  }

Note: After enable/install the wsl, the wsl.exe is exist but doing anything with it such as running wsl.exe --install <distro> or just check the wsl --version or set-default-version or wsl  --status it is failing at the end of provisioner "powershell" (even if I restart the machine.) In the below example I import (wsl.exe --import....) a linux distro which is also not working probably because of wsl.exe --import command.

  provisioner "powershell" {
    inline = [
    "$wslName = \"devbox\"",
    "$wslInstallationPath = \"$env:USERPROFILE\\WSL2\\$wslName\\\"",
    "if (-Not (Test-Path -Path $env:TEMP\\staging)) { $dir = mkdir $env:TEMP\\staging }",
    "$url = \"https://wslstorestorage.blob.core.windows.net/wslblob/Ubuntu2404-240425.AppxBundle\"",
    "Set-Location $env:TEMP ; curl.exe -O https://wslstorestorage.blob.core.windows.net/wslblob/Ubuntu2404-240425.AppxBundle ; mv $env:TEMP/Ubuntu2404-240425.AppxBundle $env:TEMP\\staging\\$wslName-Temp.zip ",
    "Write-Host \"Expand-Archive1\"",
    "Expand-Archive $env:TEMP\\staging\\$wslName-Temp.zip $env:TEMP\\staging\\$wslName-Temp",
    "Move-Item $env:TEMP\\staging\\$wslName-Temp\\Ubuntu_2404.0.5.0_ARM64.appx $env:TEMP\\staging\\$wslName.zip",
    "Expand-Archive $env:TEMP\\staging\\$wslName.zip $env:TEMP\\staging\\$wslName",
    "Write-Host \"Expand-Archive2\"",
    "if (-Not (Test-Path -Path $wslInstallationPath)) {mkdir $wslInstallationPath}",
    "Write-Host \"wsl.exe --import\"",
    "wsl.exe --import $wslName $wslInstallationPath $env:TEMP\\staging\\$wslName\\install.tar.gz",
    "Write-Host \"Remove-Item\"",
    "Remove-Item -r $env:TEMP\\staging\\",
    "Write-Host \"wsl.exe -d $wslName -u root\"",
    "wsl.exe -d $wslName -u root",
    "Write-Host \"wsl.exe -u root apt-get update\"",
    "wsl.exe -d $wslName -u root apt-get update"
    ]
  }

  provisioner "powershell" {
    inline = [

I have some log about the error, unfortunatelly with not too much details.

.
.
2024/10/01 15:39:40 [INFO] (telemetry) Starting provisioner powershell
2024/10/01 15:39:40 ui: ==> azure-arm.win11-aarch: Provisioning with Powershell…
2024/10/01 15:39:40 packer.exe plugin: Found command: $wslName = “devbox”
2024/10/01 15:39:40 packer.exe plugin: Found command: $wslInstallationPath = "$env:USERPROFILE\WSL2$wslName"
2024/10/01 15:39:40 packer.exe plugin: Found command: if (-Not (Test-Path -Path $env:TEMP\staging)) { $dir = mkdir $env:TEMP\staging }
2024/10/01 15:39:40 packer.exe plugin: Found command: $url = “https://wslstorestorage.blob.core.windows.net/wslblob/Ubuntu2404-240425.AppxBundle
2024/10/01 15:39:40 packer.exe plugin: Found command: Set-Location $env:TEMP ; curl.exe -O https://wslstorestorage.blob.core.windows.net/wslblob/Ubuntu2404-240425.AppxBundle ; mv $env:TEMP/Ubuntu2404-240425.AppxBundle $env:TEMP\staging$wslName-Temp.zip
2024/10/01 15:39:40 packer.exe plugin: Found command: Write-Host “Expand-Archive1”
2024/10/01 15:39:40 packer.exe plugin: Found command: Expand-Archive $env:TEMP\staging$wslName-Temp.zip $env:TEMP\staging$wslName-Temp
2024/10/01 15:39:40 packer.exe plugin: Found command: Move-Item $env:TEMP\staging$wslName-Temp\Ubuntu_2404.0.5.0_ARM64.appx $env:TEMP\staging$wslName.zip
2024/10/01 15:39:40 packer.exe plugin: Found command: Expand-Archive $env:TEMP\staging$wslName.zip $env:TEMP\staging$wslName
2024/10/01 15:39:40 packer.exe plugin: Found command: Write-Host “Expand-Archive2”
2024/10/01 15:39:40 packer.exe plugin: Found command: if (-Not (Test-Path -Path $wslInstallationPath)) {mkdir $wslInstallationPath}
2024/10/01 15:39:40 packer.exe plugin: Found command: Write-Host “wsl.exe --import”
2024/10/01 15:39:40 packer.exe plugin: Found command: dir $wslInstallationPath
2024/10/01 15:39:40 packer.exe plugin: Found command: wsl.exe --import $wslName $wslInstallationPath $env:TEMP\staging$wslName\install.tar.gz
2024/10/01 15:39:40 packer.exe plugin: Found command: Write-Host “Remove-Item”
2024/10/01 15:39:40 packer.exe plugin: Found command: dir $wslInstallationPath
2024/10/01 15:39:40 packer.exe plugin: Found command: Remove-Item -r $env:TEMP\staging
2024/10/01 15:39:40 packer.exe plugin: Found command: Write-Host “wsl.exe -d $wslName -u root”
2024/10/01 15:39:40 packer.exe plugin: Found command: wsl.exe -d $wslName -u root
2024/10/01 15:39:40 packer.exe plugin: Found command: Write-Host “wsl.exe -u root apt-get update”
2024/10/01 15:39:40 packer.exe plugin: Found command: wsl.exe -d $wslName -u root apt-get update
2024/10/01 15:39:40 ui: ==> azure-arm.win11-aarch: Provisioning with powershell script: C:\Users\balu\AppData\Local\Temp\powershell-provisioner3050714510
2024/10/01 15:39:40 packer.exe plugin: Opening C:\Users\balu\AppData\Local\Temp\powershell-provisioner3050714510 for reading
2024/10/01 15:39:40 packer.exe plugin: Uploading env vars to c:/Windows/Temp/packer-ps-env-vars-66fbfb9c-db4f-da08-43e4-0b992e43c172.ps1
2024/10/01 15:39:40 packer.exe plugin: [INFO] 76 bytes written for ‘uploadData’
2024/10/01 15:39:40 [INFO] 76 bytes written for ‘uploadData’
2024/10/01 15:39:40 packer-plugin-azure_v2.1.8_x5.0_windows_386.exe plugin: 2024/10/01 15:39:40 Uploading file to ‘c:/Windows/Temp/packer-ps-env-vars-66fbfb9c-db4f-da08-43e4-0b992e43c172.ps1’
2024/10/01 15:39:41 packer.exe plugin: [INFO] 1423 bytes written for ‘uploadData’
2024/10/01 15:39:41 [INFO] 1423 bytes written for ‘uploadData’
2024/10/01 15:39:41 packer-plugin-azure_v2.1.8_x5.0_windows_386.exe plugin: 2024/10/01 15:39:41 Uploading file to ‘c:/Windows/Temp/script-66fbfb9c-1632-2395-4274-079671cb562c.ps1’
2024/10/01 15:39:43 packer-plugin-azure_v2.1.8_x5.0_windows_386.exe plugin: 2024/10/01 15:39:43 [INFO] starting remote command: powershell -executionpolicy bypass “& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value ‘SilentlyContinue’};. c:/Windows/Temp/packer-ps-env-vars-66fbfb9c-db4f-da08-43e4-0b992e43c172.ps1; &‘c:/Windows/Temp/script-66fbfb9c-1632-2395-4274-079671cb562c.ps1’; exit $LastExitCode }”
2024/10/01 15:39:44 ui error: ==> azure-arm.win11-aarch: % Total % Received % Xferd Average Speed Time Time Time Current
2024/10/01 15:39:44 ui error: ==> azure-arm.win11-aarch: Dload Upload Total Spent Left Speed
2024/10/01 15:40:16 ui: azure-arm.win11-aarch: Expand-Archive1
2024/10/01 15:40:16 ui error: ==> azure-arm.win11-aarch: 100 667M 100 667M 0 0 20.9M 0 0:00:31 0:00:31 --:–:-- 23.6M
2024/10/01 15:40:27 [INFO] 4144 bytes written for ‘stderr’
2024/10/01 15:40:27 [INFO] 570 bytes written for ‘stdout’
2024/10/01 15:40:27 [INFO] RPC client: Communicator ended with: 1
2024/10/01 15:40:27 [INFO] RPC endpoint: Communicator ended with: 1
2024/10/01 15:40:27 packer-plugin-azure_v2.1.8_x5.0_windows_386.exe plugin: 2024/10/01 15:40:27 [INFO] command ‘powershell -executionpolicy bypass “& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value ‘SilentlyContinue’};. c:/Windows/Temp/packer-ps-env-vars-66fbfb9c-db4f-da08-43e4-0b992e43c172.ps1; &‘c:/Windows/Temp/script-66fbfb9c-1632-2395-4274-079671cb562c.ps1’; exit $LastExitCode }”’ exited with code: 1
2024/10/01 15:40:27 packer-plugin-azure_v2.1.8_x5.0_windows_386.exe plugin: 2024/10/01 15:40:27 [INFO] RPC endpoint: Communicator ended with: 1
2024/10/01 15:40:27 packer.exe plugin: [INFO] 4144 bytes written for ‘stderr’
2024/10/01 15:40:27 packer.exe plugin: [INFO] 570 bytes written for ‘stdout’
2024/10/01 15:40:27 packer.exe plugin: [INFO] RPC client: Communicator ended with: 1
2024/10/01 15:40:27 ui: azure-arm.win11-aarch: Expand-Archive2
2024/10/01 15:40:27 ui: azure-arm.win11-aarch:
2024/10/01 15:40:27 ui: azure-arm.win11-aarch:
2024/10/01 15:40:27 ui: azure-arm.win11-aarch: Directory: C:\Users\packer\WSL2
2024/10/01 15:40:27 ui: azure-arm.win11-aarch:
2024/10/01 15:40:27 ui: azure-arm.win11-aarch:
2024/10/01 15:40:27 ui: azure-arm.win11-aarch: Mode LastWriteTime Length Name
2024/10/01 15:40:27 ui: azure-arm.win11-aarch: ---- ------------- ------ ----
2024/10/01 15:40:27 ui: azure-arm.win11-aarch: d----- 10/1/2024 1:40 PM devbox
2024/10/01 15:40:27 ui: azure-arm.win11-aarch: wsl.exe --import
2024/10/01 15:40:27 ui: azure-arm.win11-aarch: Remove-Item
2024/10/01 15:40:27 ui: azure-arm.win11-aarch: wsl.exe -d devbox -u root
2024/10/01 15:40:27 ui: azure-arm.win11-aarch: wsl.exe -u root apt-get update
2024/10/01 15:40:27 ui: azure-arm.win11-aarch: wsl.exe -u root apt-get pkg-config automake
2024/10/01 15:40:27 ui error: ==> azure-arm.win11-aarch:
2024/10/01 15:40:27 ui error: ==> azure-arm.win11-aarch:
2024/10/01 15:40:27 ui error: ==> azure-arm.win11-aarch:
2024/10/01 15:40:27 ui error: ==> azure-arm.win11-aarch:
2024/10/01 15:40:27 ui error: ==> azure-arm.win11-aarch:
2024/10/01 15:40:27 ui error: ==> azure-arm.win11-aarch:
2024/10/01 15:40:27 ui error: ==> azure-arm.win11-aarch:
2024/10/01 15:40:27 ui error: ==> azure-arm.win11-aarch:
2024/10/01 15:40:27 ui error: ==> azure-arm.win11-aarch:
2024/10/01 15:40:27 [INFO] (telemetry) ending powershell
2024/10/01 15:40:27 packer.exe plugin: c:/Windows/Temp/script-66fbfb9c-1632-2395-4274-079671cb562c.ps1 returned with exit code 1
2024/10/01 15:40:27 ui: ==> azure-arm.win11-aarch: Provisioning step had errors: Running the cleanup provisioner, if present…
2024/10/01 15:40:27 ui: ==> azure-arm.win11-aarch:
==> azure-arm.win11-aarch: Deleting Virtual Machine deployment and its attached resources…

Maybe its similar to this issue: azure devops - WSL2 install via Packer - Stack Overflow