Last active
March 2, 2024 22:24
-
-
Save giseongeom/586752c5af8c282a29e7ed498afa1dc3 to your computer and use it in GitHub Desktop.
packer template (sysprep-safe)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"variables": { | |
"client_id": "{{env `AZURE_CLIENT_ID`}}", | |
"client_secret": "{{env `AZURE_CLIENT_SECRET`}}", | |
"subscription_id": "{{env `AZURE_SUBSCRIPTION_ID`}}", | |
"tenant_id": "{{env `AZURE_TENANT_ID`}}", | |
"spn_object_id": "{{env `AZURE_SPN_OBJECT_ID`}}", | |
"resource_group": "{{env `AZURE_BUILD_RESOURCE_GROUP`}}", | |
"resource_location": "{{env `AZURE_BUILD_LOCATION`}}", | |
"storage_account": "{{env `AZURE_BUILD_STORAGE_ACCOUNT`}}", | |
"vm_size": "{{env `AZURE_DEFAULT_VM_SIZE`}}", | |
"image_os_sku": "2016-Datacenter-Server-Core" | |
}, | |
"builders": [{ | |
"type": "azure-arm", | |
"client_id": "{{user `client_id`}}", | |
"client_secret": "{{user `client_secret`}}", | |
"subscription_id": "{{user `subscription_id`}}", | |
"tenant_id": "{{user `tenant_id`}}", | |
"object_id": "{{user `spn_object_id`}}", | |
"managed_image_name": "{{user `image_os_sku`}}--{{isotime \"2006.01.02-1504\"}}", | |
"managed_image_resource_group_name": "{{user `resource_group`}}", | |
"managed_image_storage_account_type": "Premium_LRS", | |
"os_type": "Windows", | |
"image_publisher": "MicrosoftWindowsServer", | |
"image_offer": "WindowsServer", | |
"image_sku": "{{user `image_os_sku`}}", | |
"communicator": "winrm", | |
"winrm_use_ssl": "true", | |
"winrm_insecure": "true", | |
"winrm_timeout": "5m", | |
"winrm_username": "packer", | |
"location": "{{user `resource_location`}}", | |
"vm_size": "{{user `vm_size`}}" | |
}], | |
"provisioners": [ | |
{ | |
"type": "windows-shell", | |
"inline": [ | |
"powershell.exe -ExecutionPolicy bypass -windowstyle hidden -Command $PSVersionTable", | |
"systeminfo" | |
] | |
}, | |
{ | |
"type": "powershell", | |
"inline": [ | |
"(gcim win32_service | ? { $_.name -match 'WindowsAzureGuestAgent' }).PathName", | |
"get-service WindowsAzureGuestAgent | ft -autosize" | |
] | |
}, | |
{ | |
"type": "powershell", | |
"inline": [ | |
"if( Test-Path $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml ){ rm $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml -Force}", | |
"& $Env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /quit /quiet", | |
"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 } }" | |
] | |
} | |
] | |
} |
Author
giseongeom
commented
Sep 28, 2017
- build log
- Launch VM using powershell
Select-AzureRmSubscription -SubscriptionId xxxxxxxx
$rgname = 'xxxxxxxx'
$location = 'westcentralus'
$vmSize = "Standard_DS3_v2"
$imagename = '2016-Datacenter-Server-Core--2017.09.28-0127'
$resid = (Get-Random).ToString().Substring(0, 4)
$image = Get-AzureRmImage -ImageName $imageName -ResourceGroupName $rgName
$ipName = "an-pip-$resid"
$pip = New-AzureRmPublicIpAddress -Name $ipName -ResourceGroupName $rgName -Location $location -AllocationMethod Dynamic
$vnet = Get-AzureRmVirtualNetwork -ResourceGroupName $rgName
$nicName = "an-nic-$resid"
$nic = New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $rgName -Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id
$cred = $cred_azure_opsadmin
$vmName = "an-vm-$resid"
$computerName = "myCo-$resid"
$vmconfig = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize
$vmsrcimage = Set-AzureRmVMSourceImage -vm $vmconfig -id $image.Id
$vmosdisk = Set-AzureRmVMOSDisk -VM $vmconfig -StorageAccountType PremiumLRS -DiskSizeInGB 128 -CreateOption FromImage -Caching ReadWrite
$vmos = Set-AzureRmVMOperatingSystem -VM $vmconfig -Windows -ComputerName $computerName -Credential $cred -ProvisionVMAgent -EnableAutoUpdate
$vmbootdiag = Set-AzureRmVMBootDiagnostics -Disable -VM $vmconfig
$vmnic = Add-AzureRmVMNetworkInterface -VM $vmconfig -Id $nic.Id
New-azurermvm -VM $vmconfig -ResourceGroupName $rgName -Location $location -Verbose
- output
Thursday, September 28, 2017 11:37:08 AM
VERBOSE: Performing the operation "New" on target "an-vm-2043".
RequestId IsSuccessStatusCode StatusCode ReasonPhrase
--------- ------------------- ---------- ------------
True OK OK
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment