瞭解如何使用主要範本和連結的範本來建立範本規格。 您可以使用範本規格與組織中的其他使用者共用 ARM 範本。 本文說明如何使用relativePath 屬性,建立範本規格來封裝主要範本及其連結的範本。
先決條件
具有有效訂閱的 Azure 帳戶。 免費建立帳戶。
備註
若要搭配 Azure PowerShell 使用範本規格,您必須安裝 5.0.0 版或更新版本。 若要與 Azure CLI 搭配使用,請使用 2.14.2 版或更新版本。
建立連結的範本
建立主要範本和連結的範本。
若要連結範本,請將部署資源新增至主要範本。 在屬性中 templateLink ,根據父範本的路徑指定連結範本的相對路徑。
連結的範本稱為 linkedTemplate.json,並儲存在儲存主要範本之路徑中稱為 成品 的子資料夾中。 您可以針對 relativePath 使用下列其中一個值:
./artifacts/linkedTemplate.json/artifacts/linkedTemplate.jsonartifacts/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": "2025-03-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": "2025-04-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": "2025-06-01", "name": "[variables('storageAccountName')]", "location": "[parameters('location')]", "sku": { "name": "[parameters('storageAccountType')]" }, "kind": "StorageV2", "properties": {} } ], "outputs": { "storageAccountName": { "type": "string", "value": "[variables('storageAccountName')]" } } }將範本儲存為 artifacts 資料夾中的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 入口網站或使用下列 Cmdlet 來檢視範本規格:
Get-AzTemplateSpec -ResourceGroupName templatespecRG -Name webSpec
部署範本規格
您現在可以部署範本規格。部署範本規格就像部署它所包含的範本,不同之處在於您傳入範本規格的資源識別碼。您可以使用相同的部署命令,並視需要傳入範本規格的參數值。
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
後續步驟
若要了解如何將範本規格部署為連結的範本,請參閱 教學課程:將範本規格部署為連結的範本。