使用 Azure VM Image Builder 建立 Windows VM

適用於:✔️ Windows VM

在本文中,您將了解如何使用 Azure VM Image Builder 建立自訂的 Windows 映像。 本文中的範例會使用自訂工具來自訂映像:

  • PowerShell (ScriptUri):下載並執行 PowerShell 指令碼
  • Windows Restart:重新啟動 VM。
  • PowerShell (inline):執行特定命令。 在此範例中,會使用 mkdir c:\\buildActions 在 VM 上建立目錄。
  • File:將檔案從 GitHub 複製到 VM。 此範例會將 index.md 複製到 VM 上的 c:\buildArtifacts\index.html
  • buildTimeoutInMinutes:指定建置時間,以分鐘為單位。 預設值為 240 分鐘,您可以再增加以執行時間較長的建置。 允許的最小值為 6 分鐘。 小於 6 分鐘的值會造成錯誤。
  • vmProfile:指定 vmSize 和網路屬性。
  • osDiskSizeGB:可用來增加映像的大小。
  • identity. 提供 VM Image Builder 在建置期間要使用的身分識別。

使用下列範例 JSON 範本來設定映像: helloImageTemplateWin.json

注意

Windows 使用者可以使用 Bash,在 Azure Cloud Shell 上執行下列 Azure CLI 範例。

註冊提供者

若要使用 VM Image Builder,您必須註冊該功能。 執行下列命令來檢查您的註冊:

az provider show -n Microsoft.VirtualMachineImages | grep registrationState
az provider show -n Microsoft.KeyVault | grep registrationState
az provider show -n Microsoft.Compute | grep registrationState
az provider show -n Microsoft.Storage | grep registrationState
az provider show -n Microsoft.Network | grep registrationState
az provider show -n Microsoft.ContainerInstance -o json | grep registrationState

如果輸出未指出已註冊,請執行下列命令:

az provider register -n Microsoft.VirtualMachineImages
az provider register -n Microsoft.Compute
az provider register -n Microsoft.KeyVault
az provider register -n Microsoft.Storage
az provider register -n Microsoft.Network
az provider register -n Microsoft.ContainerInstance

設定變數

由於您會重複使用某些資訊,因此請建立一些變數來儲存這些資訊:

# Resource group name - we're using myImageBuilderRG in this example
imageResourceGroup='myWinImgBuilderRG'
# Region location
location='WestUS2'
# Run output name
runOutputName='aibWindows'
# The name of the image to be created
imageName='aibWinImage'

為訂閱識別碼建立變數:

subscriptionID=$(az account show --query id --output tsv)

建立資源群組

若要儲存映像設定範本成品和映像,請使用下列資源群組:

az group create -n $imageResourceGroup -l $location

建立使用者指派的身分識別,並在資源群組上設定權限

VM Image Builder 會使用所提供的使用者身分識別,將映像插入資源群組。 在此範例中,您會建立具有發布映像之特定權限的 Azure 角色定義。 然後此將角色定義指派給使用者身分識別。

建立使用者指派的受控識別,並授與權限

建立使用者指派的身分識別,讓 VM Image Builder 存取儲存指令碼的儲存體帳戶。

identityName=aibBuiUserId$(date +'%s')
az identity create -g $imageResourceGroup -n $identityName

# Get the identity ID
imgBuilderCliId=$(az identity show -g $imageResourceGroup -n $identityName --query clientId -o tsv)

# Get the user identity URI that's needed for the template
imgBuilderId=/subscriptions/$subscriptionID/resourcegroups/$imageResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/$identityName

# Download the preconfigured role definition example
curl https://raw.githubusercontent.com/azure/azvmimagebuilder/master/solutions/12_Creating_AIB_Security_Roles/aibRoleImageCreation.json -o aibRoleImageCreation.json

imageRoleDefName="Azure Image Builder Image Def"$(date +'%s')

# Update the definition
sed -i -e "s%<subscriptionID>%$subscriptionID%g" aibRoleImageCreation.json
sed -i -e "s%<rgName>%$imageResourceGroup%g" aibRoleImageCreation.json
sed -i -e "s%Azure Image Builder Service Image Creation Role%$imageRoleDefName%g" aibRoleImageCreation.json

# Create role definitions
az role definition create --role-definition ./aibRoleImageCreation.json

# Grant a role definition to the user-assigned identity
az role assignment create \
    --assignee $imgBuilderCliId \
    --role "$imageRoleDefName" \
    --scope /subscriptions/$subscriptionID/resourceGroups/$imageResourceGroup

下載映像設定範本

我們已為您建立可嘗試的參數化映像設定範本。 下載範例 JSON 檔案,然後使用您先前設定的變數進行設定。

curl https://raw.githubusercontent.com/azure/azvmimagebuilder/master/quickquickstarts/0_Creating_a_Custom_Windows_Managed_Image/helloImageTemplateWin.json -o helloImageTemplateWin.json

sed -i -e "s%<subscriptionID>%$subscriptionID%g" helloImageTemplateWin.json
sed -i -e "s%<rgName>%$imageResourceGroup%g" helloImageTemplateWin.json
sed -i -e "s%<region>%$location%g" helloImageTemplateWin.json
sed -i -e "s%<imageName>%$imageName%g" helloImageTemplateWin.json
sed -i -e "s%<runOutputName>%$runOutputName%g" helloImageTemplateWin.json
sed -i -e "s%<imgBuilderId>%$imgBuilderId%g" helloImageTemplateWin.json

您可以使用 vi 之類的文字編輯器,在終端機中修改此範例。

vi helloImageTemplateWin.json

注意

針對來源映像,請一律指定版本。 您無法將 latest 指定為版本。

如果您新增或變更將映像發佈至其中的資源群組,請確定您已在資源群組上設定權限

建立映像

執行下列命令,將映像設定提交至 VM Image Builder 服務:

az resource create \
    --resource-group $imageResourceGroup \
    --properties @helloImageTemplateWin.json \
    --is-full-object \
    --resource-type Microsoft.VirtualMachineImages/imageTemplates \
    -n helloImageTemplateWin01

完成後,系統會將成功訊息傳回主控台,並在 $imageResourceGroup 中建立 VM Image Builder 設定範本。 若要在資源群組中檢視此資源,請到 Azure 入口網站,然後啟用 [顯示隱藏的類型]

VM Image Builder 也會在背景中為您的訂閱建立暫存資源群組。 此資源群組可用來以下列格式建置映像:IT_<DestinationResourceGroup>_<TemplateName>

注意

請勿直接刪除暫存資源群組。 先刪除映像範本成品,會導致暫存資源群組遭到刪除。

如果服務在映像設定範本提交期間回報失敗,請執行下列動作:

az resource delete \
    --resource-group $imageResourceGroup \
    --resource-type Microsoft.VirtualMachineImages/imageTemplates \
    -n helloImageTemplateWin01

啟動映像建置

使用 az resource invoke-action 啟動映像建置程序。

az resource invoke-action \
     --resource-group $imageResourceGroup \
     --resource-type  Microsoft.VirtualMachineImages/imageTemplates \
     -n helloImageTemplateWin01 \
     --action Run

等待建置完成。

如果您遇到任何錯誤,請參閱為 Azure VM Image Builder 服務疑難排解

建立 VM

使用您建置的映像建立 VM。 在下列程式碼中,將<密碼>取代為您 VM 上 aibuser 的密碼。

az vm create \
  --resource-group $imageResourceGroup \
  --name aibImgWinVm00 \
  --admin-username aibuser \
  --admin-password <password> \
  --image $imageName \
  --location $location

確認自訂

使用您在建立 VM 時所設定的使用者名稱與密碼,建立與該 VM 的遠端桌面連線。 在 VM 中,開啟 [命令提示字元] 視窗,然後輸入:

dir c:\

映像自訂期間會建立以下兩個目錄:

  • buildActions
  • buildArtifacts

清除資源

當您完成時,請刪除您建立的資源群組。

  1. 刪除 VM Image Builder 範本。

    az resource delete \
        --resource-group $imageResourceGroup \
        --resource-type Microsoft.VirtualMachineImages/imageTemplates \
        -n helloImageTemplateWin01
    
  2. 刪除角色指派、角色定義和使用者身分識別。

    az role assignment delete \
        --assignee $imgBuilderCliId \
        --role "$imageRoleDefName" \
        --scope /subscriptions/$subscriptionID/resourceGroups/$imageResourceGroup
    
    az role definition delete --name "$imageRoleDefName"
    
    az identity delete --ids $imgBuilderId
    
  3. 刪除映像資源群組。

    az group delete -n $imageResourceGroup
    

下一步

若要深入了解本文中使用的 JSON 檔案元件,請參閱 VM Image Builder 範本參考