添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

本文介绍如何在 Azure 资源管理器模板(ARM 模板)中定义和使用变量。 可以使用变量来简化模板。 可以定义一个包含复杂表达式的变量,而不必在整个模板中重复使用复杂表达式。 然后,可以在整个模板中根据需要使用该变量。

资源管理器会在启动部署操作之前解析变量。 只要在模板中使用变量,资源管理器就会将其替换为解析的值。

我们建议使用 Bicep ,因为它提供与 ARM 模板相同的功能,并且该语法更易于使用。 若要了解详情,请参阅 变量

一个模板中最多可以有 256 个变量。 有关详细信息,请参阅 模板限制

定义变量时,你不指定变量的 数据类型 。 而是提供一个值或模板表达式。 变量类型是从解析的值推断的。 以下示例将一个变量设置为字符串。

"variables": {
  "stringVar": "example value"

若要构造此变量,可以使用参数或其他变量的值。

"parameters": {
  "inputValue": {
    "defaultValue": "deployment parameter",
    "type": "string"
"variables": {
  "stringVar": "myVariable",
  "concatToVar": "[concat(variables('stringVar'), '-addtovar') ]",
  "concatToParam": "[concat(parameters('inputValue'), '-addtoparam')]"

你可以使用模板函数来构造变量值。

以下示例创建一个字符串值作为存储帐户名称。 它使用多个模板函数来获取参数值,并将其连接到唯一字符串。

"variables": {
  "storageName": "[concat(toLower(parameters('storageNamePrefix')), uniqueString(resourceGroup().id))]"

不能在变量声明中使用 reference 函数或任何 list 函数。 在解析变量时,这些函数获取资源的运行时状态,不能在部署之前执行。

以下示例演示如何使用资源属性的变量。

若要引用变量的值,请使用 variables 函数。

"variables": {
  "storageName": "[concat(toLower(parameters('storageNamePrefix')), uniqueString(resourceGroup().id))]"
"resources": [
    "type": "Microsoft.Storage/storageAccounts",
    "name": "[variables('storageName')]",

以下模板不部署任何资源。 它显示了声明各种类型的变量的一些方法。

"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "inputValue": { "defaultValue": "deployment parameter", "type": "string" "variables": { "stringVar": "myVariable", "concatToVar": "[concat(variables('stringVar'), '-addtovar') ]", "concatToParam": "[concat(parameters('inputValue'), '-addtoparam')]", "arrayVar": [ "objectVar": { "property1": "value1", "property2": "value2" "copyWithinVar": { "copy": [ "name": "disks", "count": 5, "input": { "name": "[concat('myDataDisk', copyIndex('disks', 1))]", "diskSizeGB": "1", "diskIndex": "[copyIndex('disks')]" "name": "diskNames", "count": 5, "input": "[concat('myDataDisk', copyIndex('diskNames', 1))]" "copy": [ "name": "topLevelCopy1", "count": 5, "input": { "name": "[concat('oneDataDisk', copyIndex('topLevelCopy1', 1))]", "diskSizeGB": "1", "diskIndex": "[copyIndex('topLevelCopy1')]" "name": "topLevelCopy2", "count": 3, "input": { "name": "[concat('twoDataDisk', copyIndex('topLevelCopy2', 1))]", "diskSizeGB": "1", "diskIndex": "[copyIndex('topLevelCopy2')]" "name": "topLevelCopy3", "count": 4, "input": "[concat('stringValue', copyIndex('topLevelCopy3'))]" "name": "topLevelCopy4", "count": 4, "input": "[copyIndex('topLevelCopy4')]" "resources": [], "outputs": { "stringOutput": { "type": "string", "value": "[variables('stringVar')]" "concatToVariableOutput": { "type": "string", "value": "[variables('concatToVar')]" "concatToParameterOutput": { "type": "string", "value": "[variables('concatToParam')]" "arrayOutput": { "type": "array", "value": "[variables('arrayVar')]" "arrayElementOutput": { "type": "int", "value": "[variables('arrayVar')[0]]" "objectOutput": { "type": "object", "value": "[variables('objectVar')]" "copyWithinVariableOutput": { "type": "object", "value": "[variables('copyWithinVar')]" "topLevelCopyOutput1": { "type": "array", "value": "[variables('topLevelCopy1')]" "topLevelCopyOutput2": { "type": "array", "value": "[variables('topLevelCopy2')]" "topLevelCopyOutput3": { "type": "array", "value": "[variables('topLevelCopy3')]" "topLevelCopyOutput4": { "type": "array", "value": "[variables('topLevelCopy4')]"

可以定义变量来保存配置环境所需的相关值。 可以将变量定义为一个包含值的对象。 以下示例演示的对象包含的值适用于两个环境 - testprod。在部署过程中传入这些值之一。

"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "environmentName": { "type": "string", "allowedValues": [ "test", "prod" "metadata": { "description": "Specify either test or prod for configuration values." "variables": { "environmentSettings": { "test": { "instanceSize": "Small", "instanceCount": 1 "prod": { "instanceSize": "Large", "instanceCount": 4 "resources": [], "outputs": { "instanceSize": { "value": "[variables('environmentSettings')[parameters('environmentName')].instanceSize]", "type": "string" "instanceCount": { "value": "[variables('environmentSettings')[parameters('environmentName')].instanceCount]", "type": "int"
  • 若要了解变量的可用属性,请参阅了解 ARM 模板的结构和语法
  • 有关创建变量的建议,请参阅最佳做法 - 变量
  • 有关用于将安全规则分配到网络安全组的示例模板,请参阅网络安全规则参数文件
  •