教程:将模板函数添加到 ARM 模板Tutorial: Add template functions to your ARM template

本教程介绍如何向 Azure 资源管理器 (ARM) 模板添加模板函数In this tutorial, you learn how to add template functions to your Azure Resource Manager (ARM) template. 我们使用函数来动态构造值。You use functions to dynamically construct values. 除了这些系统提供的模板函数,还可以创建用户定义的函数In addition to these system-provided template functions, you can also create user-defined functions. 完成本教程需要 7 分钟This tutorial takes 7 minutes to complete.

先决条件Prerequisites

建议完成有关参数的教程,但这不是必需的。We recommend that you complete the tutorial about parameters, but it's not required.

必须已安装带有资源管理器工具扩展的 Visual Studio Code,以及 Azure PowerShell 或 Azure CLI。You must have Visual Studio Code with the Resource Manager Tools extension, and either Azure PowerShell or Azure CLI. 有关详细信息,请参阅模板工具For more information, see template tools.

审阅模板Review template

在上一篇教程的结束时,模板包含以下 JSON:At the end of the previous tutorial, your template had the following JSON:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageName": {
      "type": "string",
      "minLength": 3,
      "maxLength": 24
    },
    "storageSKU": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Standard_RAGRS",
        "Premium_LRS",
        "Standard_RAGZRS"
      ]
    }
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2019-04-01",
      "name": "[parameters('storageName')]",
      "location": "chinaeast",
      "sku": {
        "name": "[parameters('storageSKU')]"
      },
      "kind": "StorageV2",
      "properties": {
        "supportsHttpsTrafficOnly": true
      }
    }
  ]
}

存储帐户的位置硬编码为“中国东部”。 The location of the storage account is hard-coded to China East. 但是,可能需要将存储帐户部署到其他区域。However, you may need to deploy the storage account to other regions. 你会再次面对一个问题:模板缺乏灵活性。You're again facing an issue of your template lacking flexibility. 可以添加一个位置参数,但理想的情况是默认值更理性,而不仅仅是一个硬编码值。You could add a parameter for location, but it would be great if its default value made more sense than just a hard-coded value.

使用函数Use function

如果已完成本系列的上一教程,则你已使用了函数。If you've completed the previous tutorial in this series, you've already used a function. 在添加 "[parameters('storageName')]" 时,你使用了 parameters 函数。When you added "[parameters('storageName')]", you used the parameters function. 括号表示其中的语法是一个模板表达式The brackets indicate that the syntax inside the brackets is a template expression. 资源管理器会对语法进行解析,而不是将其视为文本值。Resource Manager resolves the syntax rather than treating it as a literal value.

函数可以在部署过程中动态获取值,为模板增加了灵活性。Functions add flexibility to your template by dynamically getting values during deployment. 在本教程中,我们使用函数来获取用于部署的资源组的位置。In this tutorial, you use a function to get the location of the resource group you're using for deployment.

以下示例重点介绍添加名为 location 的参数需要做的更改。The following example highlights the changes to add a parameter called location. 参数默认值调用 resourceGroup 函数。The parameter default value calls the resourceGroup function. 此函数返回的对象描述用于部署的资源组。This function returns an object with information about the resource group being used for deployment. 对象上的一个属性是位置属性。One of the properties on the object is a location property. 使用默认值时,存储帐户位置与资源组位置相同。When you use the default value, the storage account location has the same location as the resource group. 资源组中的资源不需共享同一位置。The resources inside a resource group don't have to share the same location. 也可根据需要提供不同的位置。You can also provide a different location when needed.

复制整个文件,将模板替换为该文件的内容。Copy the whole file and replace your template with its contents.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageName": {
      "type": "string",
      "minLength": 3,
      "maxLength": 24
    },
    "storageSKU": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Standard_RAGRS",
        "Premium_LRS",
        "Standard_RAGZRS"
      ]
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]"
    }
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2019-04-01",
      "name": "[parameters('storageName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[parameters('storageSKU')]"
      },
      "kind": "StorageV2",
      "properties": {
        "supportsHttpsTrafficOnly": true
      }
    }
  ]
}

部署模板Deploy template

在前面的教程中,你创建的存储帐户位于“中国东部”,但资源组创建于“中国北部”。In the previous tutorials, you created a storage account in China East, but your resource group was created in China North. 在本教程中,存储帐户与资源组在同一区域创建。For this tutorial, your storage account is created in the same region as the resource group. 请使用位置的默认值,这样就不需提供该参数值。Use the default value for location, so you don't need to provide that parameter value. 必须为存储帐户提供新名称,因为是在另一位置创建存储帐户。You must provide a new name for the storage account because you're creating a storage account in a different location. 例如,使用 store2 而不是 store1 作为前缀。For example, use store2 as the prefix instead of store1.

如果尚未创建资源组,请参阅创建资源组If you haven't created the resource group, see Create resource group. 此示例假设已根据第一篇教程中所述,将 templateFile 变量设置为模板文件的路径。The example assumes you've set the templateFile variable to the path to the template file, as shown in the first tutorial.

New-AzResourceGroupDeployment `
  -Name addlocationparameter `
  -ResourceGroupName myResourceGroup `
  -TemplateFile $templateFile `
  -storageName "{new-unique-name}"

备注

如果部署失败,请将 debug 开关和部署命令配合使用来显示调试日志。If the deployment failed, use the debug switch with the deployment command to show the debug logs. 还可以使用 verbose 开关来显示完整的调试日志。You can also use the verbose switch to show the full debug logs.

验证部署Verify deployment

可以通过在 Azure 门户中浏览资源组来验证部署。You can verify the deployment by exploring the resource group from the Azure portal.

  1. 登录到 Azure 门户Sign in to the Azure portal.
  2. 在左侧菜单中选择“资源组”。 From the left menu, select Resource groups.
  3. 选择已部署到的资源组。Select the resource group you deployed to.
  4. 可以看到一项存储帐户资源已部署,且其位置与资源组一样。You see that a storage account resource has been deployed and has the same location as the resource group.

清理资源Clean up resources

若要继续学习下一篇教程,则不需删除该资源组。If you're moving on to the next tutorial, you don't need to delete the resource group.

如果你不打算继续学习,请删除该资源组以清理部署的资源。If you're stopping now, you might want to clean up the resources you deployed by deleting the resource group.

  1. 在 Azure 门户上的左侧菜单中选择“资源组” 。From the Azure portal, select Resource group from the left menu.
  2. 在“按名称筛选”字段中输入资源组名称。 Enter the resource group name in the Filter by name field.
  3. 选择资源组名称。Select the resource group name.
  4. 在顶部菜单中选择“删除资源组”。 Select Delete resource group from the top menu.

后续步骤Next steps

在本教程中,我们在为参数定义默认值时使用了一个函数。In this tutorial, you used a function when defining the default value for a parameter. 在本系列教程中,我们将继续使用函数。In this tutorial series, you'll continue using functions. 到本系列完成时,我们会将函数添加到模板的每个部分。By the end of the series, you'll add functions to every section of the template.