다음을 통해 공유

教程:将模板函数添加到 ARM 模板

本教程介绍如何将 模板函数 添加到 Azure 资源管理器模板(ARM 模板)。 使用函数动态构造值。 除了这些系统提供的模板函数,还可以创建 用户定义的函数。 本教程需要 7 分钟 才能完成。

先决条件

建议完成 有关参数的教程,但这不是必需的。

需要具有 Visual Studio Code,以及 Azure PowerShell 或 Azure CLI。 有关详细信息,请参阅 模板工具

查看模板

在上一教程结束时,模板具有以下 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"
      ]
    }
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2021-09-01",
      "name": "[parameters('storageName')]",
      "location": "chinaeast",
      "sku": {
        "name": "[parameters('storageSKU')]"
      },
      "kind": "StorageV2",
      "properties": {
        "supportsHttpsTrafficOnly": true
      }
    }
  ]
}

假设将 Azure 存储帐户 的位置硬编码为 chinanorth,但需要将其部署到另一个区域。 需要添加一个参数,以便为模板添加灵活性,并允许其具有其他位置。

使用函数

如果已完成 参数教程,则使用了函数。 添加 "[parameters('storageName')]"后,使用了 参数 函数。 括号指示括号内的语法是 模板表达式。 资源管理器解析语法,而不是将其视为文本值。

函数通过在部署期间动态获取值,为模板增添灵活性。 在本教程中,将使用函数获取资源组部署位置。

以下示例突出显示了添加名为 location 的参数的更改。 参数默认值调用 resourceGroup 函数。 此函数返回一个对象,其中包含有关已部署资源组的信息。 其中一个对象属性是位置属性。 使用默认值时,存储帐户和资源组具有相同的位置。 组中的资源具有不同的位置。

复制整个文件,并用文件内容替换您的模板。

{
  "$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"
      ]
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]"
    }
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2021-09-01",
      "name": "[parameters('storageName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[parameters('storageSKU')]"
      },
      "kind": "StorageV2",
      "properties": {
        "supportsHttpsTrafficOnly": true
      }
    }
  ]
}

部署模板

在前面的教程中,你在“中国东部”创建了存储帐户,但资源组是在“中国北部”创建的。 在本教程中,将在资源组所在的同一区域中创建存储帐户。 使用位置的默认值,因此无需提供该参数值。 需要为存储帐户提供新名称,因为要在不同的位置创建存储帐户。 例如,使用 store2 作为前缀而不是 store1

如果尚未创建资源组,请参阅 “创建资源组”。 本示例假定已将 templateFile 变量设置为模板文件的路径,如 第一个教程所示。

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

注释

如果部署失败,请使用 verbose 开关获取有关所创建资源的信息。 使用 debug 开关获取调试的详细信息。

验证部署

可以通过从 Azure 门户浏览资源组来验证部署。

  1. 登录到 Azure 门户
  2. 在左侧菜单中,选择 “资源组”。
  3. 选中 myResourceGroup 左侧的框,然后选择 myResourceGroup
  4. 选择创建的资源组。 默认名称为 myResourceGroup
  5. 请注意,已部署的存储帐户和资源组的位置相同。

清理资源

若要继续学习下一篇教程,则无需删除资源组。

如果您此时停止操作,您可能希望删除资源组。

  1. 在 Azure 门户中,从左侧菜单中选择 资源组
  2. Filter for any field...文本字段中键入资源组名称。
  3. 选中 myResourceGroup 旁边的框,然后选择 myResourceGroup 或资源组名称。
  4. 在顶部菜单中选择“删除资源组”。

后续步骤

在本教程中,将使用函数来定义参数的默认值。 在本教程系列中,将继续使用函数。 在系列结束时,将函数添加到每个模板部分。