다음을 통해 공유

教程:将变量添加到 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"
      ]
    },
    "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",
        "Premium_LRS"
      ]
    },
    "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的变量。 此变量使用四个函数来创建字符串值。

你已经熟悉 参数 函数,因此我们不会检查它。

你还熟悉 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. 请注意,您部署的存储帐户名称是 存储,加上一串随机字符。

清理资源

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

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

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

后续步骤

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