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

教程:将变量添加到 ARM 模板

本教程介绍如何将变量添加到 Azure 资源管理器模版(ARM 模板)。 变量可以简化模板。 有了变量,你只需编写一次表达式,然后即可在模板中重复使用该表达式。 完成本教程需要 7 分钟

先决条件

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

需要安装 Visual Studio Code 并使用 Azure 资源管理器工具扩展,以及 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",
        "Standard_ZRS",
        "Premium_LRS",
        "Premium_ZRS",
        "Standard_GZRS",
        "Standard_RAGZRS"
      ]
    },
    "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
      }
    }
  ]
}

Azure 存储帐户名称必须是唯一的,才能轻松地继续生成 ARM 模板。 如果已完成本系列中的前述教程,你会已经厌倦了想出一个唯一名称。 若要解决此问题,可以添加一个变量,以便为存储帐户创建唯一名称。

使用变量

以下示例突出显示了在将变量添加到模板时所做的更改,该模板用于创建唯一的存储帐户名称。 复制整个文件,将模板替换为该文件的内容。

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

请注意,它包含一个名为 uniqueStorageName 的变量。 此变量使用四个函数来创建一个字符串值。

你已熟悉 parameters 函数,因此我们不详细介绍它。

你也熟悉 resourceGroup 函数。 在此示例中,你获得 id 属性而不是 location 属性,如上一教程所示。 id 属性返回资源组的完整标识符,包括订阅 ID 和资源组名称。

uniqueString 函数创建一个 13 个字符的哈希值。 传递的参数确定返回值。 在本教程中,我们使用资源组 ID 作为哈希值的输入。 这意味着,我们可以将该模板部署到不同的资源组,获取不同的唯一字符串值。 如果部署到同一资源组,则获得同一值。

concat 函数采用多个值并对其进行组合。 就此变量来说,它采用参数中的字符串和 uniqueString 函数中的字符串,并将二者合并成一个字符串。

可以通过 storagePrefix 参数传入一个用于标识存储帐户的前缀。 可以创建你自己的命名约定,以便在从全面的资源列表完成部署后,通过该约定轻松地标识存储帐户。

最后请注意,存储帐户名称现在设置为变量而非参数。

部署模板

让我们部署该模板。 部署此模板比部署上述模板容易,因为你只需提供存储帐户名称的前缀。

如果尚未创建资源组,请参阅创建资源组。 此示例假设已根据第一篇教程中所述,将 templateFile 变量设置为模板文件的路径。

New-AzResourceGroupDeployment `
  -Name addnamevariable `
  -ResourceGroupName myResourceGroup `
  -TemplateFile $templateFile `
  -storagePrefix "store" `
  -storageSKU Standard_LRS

注意

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

验证部署

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

  1. 登录 Azure 门户
  2. 在左侧菜单中选择“资源组”。
  3. 选择你的资源组。
  4. 请注意,已部署的存储帐户名称为 store 加上一个由随机字符组成的字符串。

清理资源

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

如果就此停止学习,请删除该资源组。

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

后续步骤

在本教程中,我们添加了一个变量,用于创建唯一的存储帐户名称。 在下一教程中,我们从已部署的存储帐户返回一个值。