教程:使用链接模板创建模板规格

了解如何使用主模板和链接模板创建模板规格。 使用模板规格与组织中的其他用户共享 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 的模板文件相关,因此,如果有另一个 linkedTemplate2.json 从 linkedTemplate.json 调用,并且 linkedTemplate2.json 存储在相同的 artifacts 子文件夹中,则在 linkedTemplate.json 中指定的 relativePath 仅仅是 linkedTemplate2.json

  1. 使用以下 JSON 创建主模板。 将主模板另存为azuredeploy.json到本地计算机。 本教程假定你已经保存到路径 c:\Templates\linkedTS\azuredeploy.json,但您可以使用任何路径。

    {
      "$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 或更高版本。

  2. 在保存主模板的文件夹中创建名为 项目 的目录。

  3. 使用以下 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')]"
        }
      }
    }
    
  4. 将模板另存为项目文件夹中 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

部署模板规格

现在可以部署模板规格。部署模板规格就像部署它包含的模板一样,只是传入模板规格的资源 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

后续步骤

若要了解如何将模板规格作为链接模板进行部署,请参阅教程:将模板规格作为链接模板进行部署