ARM 模板中的变量Variables in ARM templates

本文介绍了如何在 Azure 资源管理器模板(ARM 模板)或 Bicep 文件中定义和使用变量。This article describes how to define and use variables in your Azure Resource Manager template (ARM template) or Bicep file. 可以使用变量来简化模板。You use variables to simplify your template. 可以定义一个包含复杂表达式的变量,而不必在整个模板中重复使用复杂表达式。Rather than repeating complicated expressions throughout your template, you define a variable that contains the complicated expression. 然后,可以在整个模板中根据需要使用该变量。Then, you use that variable as needed throughout your template.

资源管理器会在启动部署操作之前解析变量。Resource Manager resolves variables before starting the deployment operations. 只要在模板中使用变量,资源管理器就会将其替换为解析的值。Wherever the variable is used in the template, Resource Manager replaces it with the resolved value.

备注

本文包含 Bicep 示例。This article contains Bicep examples. Bicep 目前为预览版。Bicep is currently in preview. 有关详细信息,请参阅 Bicep 项目For more information, see Project Bicep.

定义变量Define variable

定义变量时,你不指定变量的数据类型When defining a variable, you don't specify a data type for the variable. 而是提供一个值或模板表达式。Instead provide a value or template expression. 变量类型是从解析的值推断的。The variable type is inferred from the resolved value. 以下示例将一个变量设置为字符串。The following example sets a variable to a string.

"variables": {
  "stringVar": "example value"
},

构造变量时,你可以使用参数或其他变量中的值。You can use the value from a parameter or another variable when constructing the variable.

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

你可以使用模板函数来构造变量值。You can use template functions to construct the variable value.

以下示例创建一个字符串值作为存储帐户名称。The following example creates a string value for a storage account name. 它使用多个模板函数来获取参数值,并将其连接到唯一字符串。It uses several template functions to get a parameter value, and concatenates it to a unique string.

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

在 JSON 模板中,不能在 variables 声明中使用 reference 函数或任何 list 函数。In JSON templates, you can't use the reference function or any of the list functions in the variable declaration. 在解析变量时,这些函数获取资源的运行时状态,不能在部署之前执行。These functions get the runtime state of a resource, and can't be executed before deployment when variables are resolved.

在 Bicep 文件中,reference 和 list 函数在声明变量时有效。In Bicep files, the reference and list functions are valid when declaring a variable.

使用变量Use variable

以下示例演示如何使用资源属性的变量。The following example shows how to use the variable for a resource property.

在 JSON 模板中,可以使用 variables 函数来引用变量的值。In a JSON template, you reference the value for the variable by using the variables function.

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

示例模板Example template

以下模板不部署任何资源。The following template doesn't deploy any resources. 它显示了声明各种类型的变量的一些方法。It shows some ways of declaring variables of different types.

{
    "$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": [
            1,
            2,
            3,
            4
        ],
        "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')]"
        },
        "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')]"
        }
    }
}

配置变量Configuration variables

可以定义变量来保存配置环境所需的相关值。You can define variables that hold related values for configuring an environment. 可以将变量定义为一个包含值的对象。You define the variable as an object with the values. 以下示例演示的对象包含的值适用于两个环境 - testprod。在部署过程中传入这些值之一。The following example shows an object that holds values for two environments - test and prod. Pass in one of these values during deployment.

{
    "$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"
        }
    }
}

后续步骤Next steps