メイン テンプレートと linked template を備えたテンプレート スペックを作成する方法を学びます。 テンプレート スペックを使用して、組織内の他のユーザーと ARM テンプレートを共有します。 この記事では、relativePath
の プロパティを使用して、メイン テンプレートとそのリンクされたテンプレートをパッケージ化するテンプレート スペックを作成する方法について説明します。
[前提条件]
アクティブなサブスクリプションを持つ Azure アカウント。 アカウントを無料で作成します。
注
Azure PowerShell でテンプレート スペックを使用するには、 バージョン 5.0.0 以降をインストールする必要があります。 Azure CLI で使用するには、 バージョン 2.14.2 以降を使用します。
リンクされたテンプレートを作成する
メイン テンプレートとリンクされたテンプレートを作成します。
テンプレートをリンクするには、 デプロイ リソース をメイン テンプレートに追加します。
templateLink
プロパティで、親テンプレートのパスに従って、リンクされたテンプレートの相対パスを指定します。
リンクされたテンプレートは linkedTemplate.jsonと呼ばれ、メイン テンプレートが格納されているパスの 成果物 と呼ばれるサブフォルダーに格納されます。 relativePath には、次のいずれかの値を使用できます。
./artifacts/linkedTemplate.json
/artifacts/linkedTemplate.json
artifacts/linkedTemplate.json
relativePath
プロパティは常に、relativePath
が宣言されているテンプレート ファイルに対して相対的であるため、linkedTemplate.json から呼び出され、linkedTemplate2.json が同じ成果物サブフォルダーに格納されている別の linkedTemplate2.json がある場合、linkedTemplate.json で指定された relativePath はlinkedTemplate2.json
だけです。
次の JSON を使用してメイン テンプレートを作成します。 メイン テンプレートをローカル コンピューター にazuredeploy.json として保存します。 このチュートリアルでは、パス c:\Templates\linkedTS\azuredeploy.jsに 保存したが、任意のパスを使用できることを前提としています。
{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "___location": { "type": "string", "defaultValue": "westus2", "metadata":{ "description": "Specify the ___location for the resources." } }, "storageAccountType": { "type": "string", "defaultValue": "Standard_LRS", "metadata":{ "description": "Specify the storage account type." } } }, "variables": { "appServicePlanName": "[format('plan{0}', uniquestring(resourceGroup().id))]" }, "resources": [ { "type": "Microsoft.Web/serverfarms", "apiVersion": "2022-09-01", "name": "[variables('appServicePlanName')]", "___location": "[parameters('___location')]", "sku": { "name": "B1", "tier": "Basic", "size": "B1", "family": "B", "capacity": 1 }, "kind": "linux", "properties": { "perSiteScaling": false, "reserved": true, "targetWorkerCount": 0, "targetWorkerSizeId": 0 } }, { "type": "Microsoft.Resources/deployments", "apiVersion": "2022-09-01", "name": "createStorage", "properties": { "mode": "Incremental", "templateLink": { "relativePath": "artifacts/linkedTemplate.json" }, "parameters": { "storageAccountType": { "value": "[parameters('storageAccountType')]" } } } } ] }
注
Microsoft.Resources/deployments
の apiVersion は 2020-06-01 以降である必要があります。メイン テンプレートが保存されているフォルダーに artifacts という名前のディレクトリを作成します。
次の JSON を使用して、リンクされたテンプレートを作成します。
{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "storageAccountType": { "type": "string", "defaultValue": "Standard_LRS", "allowedValues": [ "Standard_LRS", "Standard_GRS", "Standard_ZRS", "Premium_LRS" ], "metadata": { "description": "Storage Account type" } }, "___location": { "type": "string", "defaultValue": "[resourceGroup().___location]", "metadata": { "description": "Location for all resources." } } }, "variables": { "storageAccountName": "[format('store{0}', uniquestring(resourceGroup().id))]" }, "resources": [ { "type": "Microsoft.Storage/storageAccounts", "apiVersion": "2022-09-01", "name": "[variables('storageAccountName')]", "___location": "[parameters('___location')]", "sku": { "name": "[parameters('storageAccountType')]" }, "kind": "StorageV2", "properties": {} } ], "outputs": { "storageAccountName": { "type": "string", "value": "[variables('storageAccountName')]" } } }
成果物フォルダーにテンプレートをlinkedTemplate.json として保存します。
テンプレート スペックを作成する
テンプレート スペックはリソース グループに格納されます。 リソース グループを作成し、次のスクリプトを使用してテンプレート スペックを作成します。 テンプレート スペック名は webSpec です。
New-AzResourceGroup `
-Name templateSpecRG `
-Location westus2
New-AzTemplateSpec `
-Name webSpec `
-Version "1.0.0.0" `
-ResourceGroupName templateSpecRG `
-Location westus2 `
-TemplateFile "c:\Templates\linkedTS\azuredeploy.json"
完了したら、Azure portal から、または次のコマンドレットを使用して、テンプレート スペックを表示できます。
Get-AzTemplateSpec -ResourceGroupName templatespecRG -Name webSpec
テンプレート スペックのデプロイ
これで、テンプレート スペックをデプロイできます。テンプレート スペックのデプロイは、テンプレート スペックのリソース ID を渡す点を除き、テンプレート スペックに含まれるテンプレートのデプロイと同じです。同じデプロイ コマンドを使用し、必要に応じてテンプレート スペックのパラメーター値を渡します。
New-AzResourceGroup `
-Name webRG `
-Location westus2
$id = (Get-AzTemplateSpec -ResourceGroupName templateSpecRG -Name webSpec -Version "1.0.0.0").Versions.Id
New-AzResourceGroupDeployment `
-TemplateSpecId $id `
-ResourceGroupName webRG
次のステップ
テンプレート スペックをリンクされたテンプレートとしてデプロイする方法については、「 チュートリアル: テンプレート スペックをリンクされたテンプレートとしてデプロイする」を参照してください。