你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

教程:创建具有链接模板的模板规格

了解如何创建具有主模板和链接模板模板规格。 使用模板规格与组织中的其他用户共享 ARM 模板。 本文介绍如何使用部署资源relativePath 属性创建模板规格以打包主模板及其关联的模板。

先决条件

具有活动订阅的 Azure 帐户。 免费创建帐户

注意

若要将模板规格与 Azure PowerShell 一起使用,必须安装版本 5.0.0 或更高版本。 若要将其与 Azure CLI 一起使用,请使用版本 2.14.2 或更高版本

创建链接模板

创建主模板和链接模板。

若要链接某个模板,请向主模板中添加一个部署资源。 在 templateLink 属性中,根据父模板的路径指定链接模板的相对路径。

该链接模板名为 linkedTemplate.json,存储在主模板所在的路径中名为 artifacts 的子文件夹中 。 你可以使用 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. 在保存主模板的文件夹中创建名为 artifacts 的目录。

  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 保存在 artifacts 文件夹中 。

创建模板规格

模板规格存储在资源组中。 创建资源组,然后使用以下脚本创建模板规格。 模板规格的名称为 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

后续步骤

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