Azure 资源管理器模板中的输出Outputs in Azure Resource Manager template

本文介绍如何在 Azure 资源管理器模板中定义输出值。This article describes how to define output values in your Azure Resource Manager template. 需要从部署的资源返回值时,可以使用输出。You use outputs when you need to return values from the deployed resources.

定义输出值Define output values

以下示例演示如何返回公共 IP 地址的资源 ID:The following example shows how to return the resource ID for a public IP address:

"outputs": {
  "resourceID": {
    "type": "string",
    "value": "[resourceId('Microsoft.Network/publicIPAddresses', parameters('publicIPAddresses_name'))]"
  }
}

条件输出Conditional output

在“输出”部分中,可以有条件地返回值。In the outputs section, you can conditionally return a value. 通常,有条件地部署资源时,可以在输出中使用条件。Typically, you use condition in the outputs when you've conditionally deployed a resource. 以下示例展示了如何根据是否部署了新的公共 IP 地址,有条件地返回公共 IP 地址的资源 ID:The following example shows how to conditionally return the resource ID for a public IP address based on whether a new one was deployed:

"outputs": {
  "resourceID": {
    "condition": "[equals(parameters('publicIpNewOrExisting'), 'new')]",
    "type": "string",
    "value": "[resourceId('Microsoft.Network/publicIPAddresses', parameters('publicIPAddresses_name'))]"
  }
}

有关条件输出的简单示例,请参阅条件输出模板For a simple example of conditional output, see conditional output template.

动态输出数量Dynamic number of outputs

在某些情况下,创建模板时你不知道需要返回的值的实例数量。In some scenarios, you don't know the number of instances of a value you need to return when creating the template. 可以使用 copy 元素返回可变的值数。You can return a variable number of values by using the copy element.

"outputs": {
  "storageEndpoints": {
    "type": "array",
    "copy": {
      "count": "[parameters('storageCount')]",
      "input": "[reference(concat(copyIndex(), variables('baseName'))).primaryEndpoints.blob]"
    }
  }
}

有关详细信息,请参阅 Azure 资源管理器模板中的输出迭代For more information, see Outputs iteration in Azure Resource Manager templates.

链接模板Linked templates

若要从链接模板中检索输出值,请在父模板中使用 reference 函数。To retrieve the output value from a linked template, use the reference function in the parent template. 父模板中的语法为:The syntax in the parent template is:

"[reference('<deploymentName>').outputs.<propertyName>.value]"

从链接模板获取输出属性时,属性名称不能包含短划线。When getting an output property from a linked template, the property name can't include a dash.

以下示例演示如何通过从链接模板检索值,在负载均衡器上设置 IP 地址。The following example shows how to set the IP address on a load balancer by retrieving a value from a linked template.

"publicIPAddress": {
  "id": "[reference('linkedTemplate').outputs.resourceID.value]"
}

不能在嵌套模板的 outputs 节中使用 reference 函数。You can't use the reference function in the outputs section of a nested template. 若要返回嵌套模板中部署的资源的值,请将嵌套模板转换为链接模板。To return the values for a deployed resource in a nested template, convert your nested template to a linked template.

获取输出值Get output values

部署成功时,将在部署结果中自动返回输出值。When the deployment succeeds, the output values are automatically returned in the results of the deployment.

若要从部署历史记录中获取输出值,可以使用脚本。To get output values from the deployment history, you can use script.

(Get-AzResourceGroupDeployment `
  -ResourceGroupName <resource-group-name> `
  -Name <deployment-name>).Outputs.resourceID.value

示例模板Example templates

以下示例演示了使用输出的方案。The following examples demonstrate scenarios for using outputs.

模板Template 说明Description
复制变量Copy variables 创建复杂变量,并输出这些值。Creates complex variables and outputs those values. 不部署任何资源。Doesn't deploy any resources.
公共 IP 地址Public IP address 创建公共 IP 地址并输出资源 ID。Creates a public IP address and outputs the resource ID.
负载均衡器Load balancer 链接到前面的模板。Links to the preceding template. 创建负载均衡器时,请使用输出中的资源 ID。Uses the resource ID in the output when creating the load balancer.

后续步骤Next steps