您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

Azure 资源管理器模板中的参数Parameters in Azure Resource Manager templates

本文介绍如何在 Azure 资源管理器模板中定义和使用参数。This article describes how to define and use parameters in your Azure Resource Manager template. 为参数提供不同的值即可针对不同环境重复使用模板。By providing different values for parameters, you can reuse a template for different environments.

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

定义参数Define parameter

以下示例展示了一个简单的参数定义。The following example shows a simple parameter definition. 它定义名为 storageSKU 的参数。It defines a parameter named storageSKU. 此参数为字符串值,仅接受适合其预期用途的值。The parameter is a string value, and only accepts values that are valid for its intended use. 如果在部署过程中未提供任何值时,则此参数使用默认值。The parameter uses a default value when no value is provided during deployment.

"parameters": {
  "storageSKU": {
    "type": "string",
    "allowedValues": [
      "Standard_LRS",
      "Standard_ZRS",
      "Standard_GRS",
      "Standard_RAGRS",
      "Premium_LRS"
    ],
    "defaultValue": "Standard_LRS",
    "metadata": {
      "description": "The type of replication to use for the storage account."
    }
  }
}

使用参数Use parameter

在模板中,可以使用 parameters 函数引用参数值。In the template, you reference the value for the parameter by using the parameters function. 在以下示例中,参数值用于设置存储帐户的 SKU。In the following example, the parameter value is used to set SKU for the storage account.

"resources": [
  {
    "type": "Microsoft.Storage/storageAccounts",
    "sku": {
      "name": "[parameters('storageSKU')]"
    },
    ...
  }
]

模板函数Template functions

为参数指定默认值时,可以使用大多数模板函数。When specifying the default value for a parameter, you can use most template functions. 可以使用另一个参数值来生成默认值。You can use another parameter value to build a default value. 以下模板演示了如何以默认值的方式使用函数。The following template demonstrates the use of functions in the default value. 如果没有为站点提供名称,它会创建唯一的字符串值并将其追加到 siteWhen no name is provided for the site, it creates a unique string value and appends it to site. 如果没有为主机计划提供名称,它会采用站点的值,并追加 -planWhen no name is provided for the host plan, it takes the value for the site, and appends -plan.

"parameters": {
  "siteName": {
    "type": "string",
    "defaultValue": "[concat('site', uniqueString(resourceGroup().id))]",
    "metadata": {
      "description": "The site name. To use the default value, do not specify a new value."
    }
  },
  "hostingPlanName": {
    "type": "string",
    "defaultValue": "[concat(parameters('siteName'),'-plan')]",
    "metadata": {
      "description": "The host name. To use the default value, do not specify a new value."
    }
  }
}

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

对象作为参数Objects as parameters

通过将相关值作为对象传入,可以更轻松地对这些值进行组织。It can be easier to organize related values by passing them in as an object. 此方式还可以减少模板中的参数的数量。This approach also reduces the number of parameters in the template.

以下示例显示的参数是一个对象。The following example shows a parameter that is an object. 默认值显示对象的预期属性。The default value shows the expected properties for the object.

"parameters": {
  "VNetSettings": {
    "type": "object",
    "defaultValue": {
      "name": "VNet1",
      "location": "eastus",
      "addressPrefixes": [
        {
          "name": "firstPrefix",
          "addressPrefix": "10.0.0.0/22"
        }
      ],
      "subnets": [
        {
          "name": "firstSubnet",
          "addressPrefix": "10.0.0.0/24"
        },
        {
          "name": "secondSubnet",
          "addressPrefix": "10.0.1.0/24"
        }
      ]
    }
  }
},

可以使用点运算符引用对象的属性。You reference the properties of the object by using the dot operator.

"resources": [
  {
    "apiVersion": "2015-06-15",
    "type": "Microsoft.Network/virtualNetworks",
    "name": "[parameters('VNetSettings').name]",
    "location": "[parameters('VNetSettings').location]",
    "properties": {
      "addressSpace":{
        "addressPrefixes": [
          "[parameters('VNetSettings').addressPrefixes[0].addressPrefix]"
        ]
      },
      "subnets":[
        {
          "name":"[parameters('VNetSettings').subnets[0].name]",
          "properties": {
            "addressPrefix": "[parameters('VNetSettings').subnets[0].addressPrefix]"
          }
        },
        {
          "name":"[parameters('VNetSettings').subnets[1].name]",
          "properties": {
            "addressPrefix": "[parameters('VNetSettings').subnets[1].addressPrefix]"
          }
        }
      ]
    }
  }
]

示例模板Example templates

以下示例演示了使用参数的方案。The following examples demonstrate scenarios for using parameters.

模板Template 说明Description
包含用于默认值的函数的参数parameters with functions for default values 演示了为参数定义默认值时如何使用模板函数。Demonstrates how to use template functions when defining default values for parameters. 该模板不部署任何资源。The template doesn't deploy any resources. 它构造参数值并返回这些值。It constructs parameter values and returns those values.
参数对象parameter object 演示了将对象用于参数。Demonstrates using an object for a parameter. 该模板不部署任何资源。The template doesn't deploy any resources. 它构造参数值并返回这些值。It constructs parameter values and returns those values.

后续步骤Next steps