Azure VM Image Builder を使用して Windows VM を作成する

適用対象: ✔️ Windows VM

この記事では、Azure VM Image Builder を使用して、カスタマイズされた Windows イメージを作成する方法について説明します。 この記事の例では、イメージのカスタマイズにカスタマイザーを使用します。

  • PowerShell (ScriptUri): PowerShell スクリプトをダウンロードし、実行します。
  • Windows の再起動: VM を再起動します。
  • PowerShell (インライン): 特定のコマンドを実行します。 この例では、mkdir c:\\buildActions を使用して VM 上にディレクトリを作成します。
  • ファイル: GitHub から VM にファイルをコピーします。 この例では、index.md を VM 上の c:\buildArtifacts\index.html にコピーします。
  • buildTimeoutInMinutes: ビルド時間を分単位で指定します。 既定値は 240 分ですが、実行時間の長いビルドに対応して長くすることができます。 許容される最小値は 6 分です。 6 分より小さい値を指定するとエラーが発生します。
  • vmProfile: vmSize とネットワークのプロパティを指定します。
  • osDiskSizeGB: イメージのサイズを増やすために使用できます。
  • identity ビルド中に使用する VM Image Builder の ID を提供します。

サンプルの JSON テンプレート helloImageTemplateWin.json を使用してイメージを構成します。

注意

Windows ユーザーは、以下に示す Azure CLI の例を Azure Cloud Shell で Bash を使用して実行できます。

プロバイダーを登録する

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

出力に "registered" と表示されない場合は、次のコマンドを実行します。

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'

サブスクリプション ID の変数を作成します。

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

リソース グループの作成

イメージ構成テンプレート成果物およびイメージを格納するには、次のリソース グループを使用します。

az group create -n $imageResourceGroup -l $location

ユーザー割り当て ID を作成し、リソース グループにアクセス許可を設定する

VM Image Builder は、指定されたユーザー ID を使用して、リソース グループにイメージを挿入します。 この例では、イメージを配布するための特定のアクセス許可を持つ Azure ロール定義を作成します。 このロール定義はその後、ユーザー ID に割り当てられます。

ユーザー割り当てマネージド ID を作成してアクセス許可を付与する

ユーザー割り当て ID を作成して、スクリプトが格納されているストレージ アカウントに 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

完了すると、コンソールに成功メッセージが返され、VM Image Builder 構成テンプレートが $imageResourceGroup に作成されます。 リソース グループでこのリソースを表示するには、Azure portal に移動し、[非表示の型の表示] を有効にします。

また、バックグラウンドで、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 を作成します。 次のコードで、<password> を 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:\

イメージのカスタマイズ中に次の 2 つのディレクトリが作成されます。

  • buildActions
  • buildArtifacts

リソースをクリーンアップする

完了したら、作成したリソースを削除します。

  1. VM Image Builder テンプレートを削除します。

    az resource delete \
        --resource-group $imageResourceGroup \
        --resource-type Microsoft.VirtualMachineImages/imageTemplates \
        -n helloImageTemplateWin01
    
  2. ロールの割り当て、ロールの定義、ユーザー ID を削除します。

    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 テンプレートに関するリファレンスを参照してください。