Azure 资源管理器模板中的变量Variables in Azure Resource Manager template

本文介绍如何在 Azure 资源管理器模板中定义和使用变量。This article describes how to define and use variables in your Azure Resource Manager template. 可以使用变量来简化模板。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 reference 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.

定义变量Define variable

以下示例介绍了变量定义。The following example shows a variable definition. 它为存储帐户名称创建字符串值。It 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))]"
},

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

使用变量Use variable

在模板中,可以使用 variables 函数引用参数值。In the template, you reference the value for the parameter by using the variables function. 以下示例演示如何使用资源属性的变量。The following example shows how to use the variable for a resource property.

"resources": [
  {
    "type": "Microsoft.Storage/storageAccounts",
    "name": "[variables('storageName')]",
    ...
  }
]

配置变量Configuration variables

可以定义变量来保存配置环境所需的相关值。You can define variables that hold related values for configuring an environment. 可以将变量定义为一个包含值的对象。You define the variable as an object with the values. 以下示例演示的对象包含的值适用于两个环境 - testprodThe following example shows an object that holds values for two environments - test and prod.

"variables": {
  "environmentSettings": {
    "test": {
      "instanceSize": "Small",
      "instanceCount": 1
    },
    "prod": {
      "instanceSize": "Large",
      "instanceCount": 4
    }
  }
},

在参数中,可以创建一个值,用于指示要使用的配置值。In parameters, you create a value that indicates which configuration values to use.

"parameters": {
  "environmentName": {
    "type": "string",
    "allowedValues": [
      "test",
      "prod"
    ]
  }
},

若要检索指定环境的设置,请将变量和参数一起使用。To retrieve settings for the specified environment, use the variable and parameter together.

"[variables('environmentSettings')[parameters('environmentName')].instanceSize]"

示例模板Example templates

以下示例演示了使用变量的方案。The following examples demonstrate scenarios for using variables.

模板Template 说明Description
变量定义variable definitions 演示不同类型的变量。Demonstrates the different types of variables. 该模板不部署任何资源。The template doesn't deploy any resources. 它构造变量值并返回这些值。It constructs variable values and returns those values.
配置变量configuration variable 演示如何使用定义配置值的变量。Demonstrates the use of a variable that defines configuration values. 该模板不部署任何资源。The template doesn't deploy any resources. 它构造变量值并返回这些值。It constructs variable values and returns those values.
网络安全规则参数文件network security rules and parameter file 以正确的格式构造数组,以便将安全规则分配给网络安全组。Constructs an array in the correct format for assigning security rules to a network security group.

后续步骤Next steps