教程:将输出添加到 Azure 资源管理器 Bicep 文件Tutorial: Add outputs to Azure Resource Manager Bicep file

本教程介绍如何从部署返回值。In this tutorial, you learn how to return a value from your deployment. 需要已部署资源提供的值时,请使用输出。You use outputs when you need a value from a deployed resource. 完成本教程需要 7 分钟This tutorial takes 7 minutes to complete.

备注

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

先决条件Prerequisites

建议完成有关变量的教程,但这不是必需的。We recommend that you complete the tutorial about variables, but it's not required.

必须具有包含 Bicep 扩展的 Visual Studio Code,以及 Azure PowerShell 或 Azure CLI。You must have Visual Studio Code with the Bicep extension, and either Azure PowerShell or Azure CLI. 有关详细信息,请参阅 Bicep 工具For more information, see Bicep tools.

查看 Bicep 文件Review Bicep file

在上一篇教程的末尾,你的 Bicep 文件有以下内容:At the end of the previous tutorial, your Bicep file had the following contents:

@minLength(3)
@maxLength(11)
param storagePrefix string

@allowed([
  'Standard_LRS'
  'Standard_GRS'
  'Standard_RAGRS'
  'Premium_LRS'
])
param storageSKU string = 'Standard_LRS'

param location string = resourceGroup().location

var uniqueStorageName = '${storagePrefix}${uniqueString(resourceGroup().id)}'

resource stg 'Microsoft.Storage/storageAccounts@2019-04-01' = {
  name: uniqueStorageName
  location: location
  sku: {
    name: storageSKU
  }
  kind: 'StorageV2'
  properties: {
    supportsHttpsTrafficOnly: true
  }
}

它部署一个存储帐户,但不返回有关该存储帐户的任何信息。It deploys a storage account, but it doesn't return any information about the storage account. 可能需要从新资源捕获属性,以便以后引用它们。You might need to capture properties from a new resource so they're available later for reference.

添加输出Add outputs

可以使用输出,以便从部署返回值。You can use outputs to return values from the deployment. 例如,可以获取新存储帐户的终结点。For example, it might be helpful to get the endpoints for your new storage account.

以下示例演示为了添加输出值而对 Bicep 文件做出的更改。The following example shows the change to your Bicep file to add an output value. 请复制整个文件,并将你的 Bicep 文件替换为该文件的内容。Copy the whole file and replace your Bicep file with its contents.

@minLength(3)
@maxLength(11)
param storagePrefix string

@allowed([
  'Standard_LRS'
  'Standard_GRS'
  'Standard_RAGRS'
  'Premium_LRS'
])
param storageSKU string = 'Standard_LRS'

param location string = resourceGroup().location

var uniqueStorageName = '${storagePrefix}${uniqueString(resourceGroup().id)}'

resource stg 'Microsoft.Storage/storageAccounts@2019-04-01' = {
  name: uniqueStorageName
  location: location
  sku: {
    name: storageSKU
  }
  kind: 'StorageV2'
  properties: {
    supportsHttpsTrafficOnly: true
  }
}

output storageEndpoint object = stg.properties.primaryEndpoints

关于添加的输出值,有一些重要的值得注意的项。There are some important items to note about the output value you added.

返回的值的类型设置为 object,这意味着它会返回模板对象。The type of returned value is set to object, which means it returns a template object.

若要从存储帐户获取 primaryEndpoints 属性,请使用存储帐户符号名称。To get the primaryEndpoints property from the storage account, you use the storage account symbolic name. Visual Studio Code 的自动完成功能提供了属性的完整列表:The autocomplete feature of the Visual Studio Code presents you a full list of the properties:

Visual Studio Code Bicep 符号名称对象属性

部署 Bicep 文件Deploy Bicep file

现在可以部署 Bicep 文件并查看返回的值了。You're ready to deploy the Bicep file and look at the returned value.

如果尚未创建资源组,请参阅创建资源组If you haven't created the resource group, see Create resource group. 此示例假定你已按第一篇教程所述将 bicepFile 变量设置为 Bicep 文件的路径。The example assumes you've set the bicepFile variable to the path to the Bicep file, as shown in the first tutorial.

New-AzResourceGroupDeployment `
  -Name addoutputs `
  -ResourceGroupName myResourceGroup `
  -TemplateFile $bicepFile `
  -storagePrefix "store" `
  -storageSKU Standard_LRS

在部署命令的输出中,将会看到类似于以下示例的对象,但前提是输出采用 JSON 格式:In the output for the deployment command, you'll see an object similar to the following example only if the output is in JSON format:

{
  "dfs": "https://storeluktbfkpjjrkm.dfs.core.chinacloudapi.cn/",
  "web": "https://storeluktbfkpjjrkm.z19.web.core.chinacloudapi.cn/",
  "blob": "https://storeluktbfkpjjrkm.blob.core.chinacloudapi.cn/",
  "queue": "https://storeluktbfkpjjrkm.queue.core.chinacloudapi.cn/",
  "table": "https://storeluktbfkpjjrkm.table.core.chinacloudapi.cn/",
  "file": "https://storeluktbfkpjjrkm.file.core.chinacloudapi.cn/"
}

备注

如果部署失败,请使用 verbose 开关获取有关正在创建的资源的信息。If the deployment failed, use the verbose switch to get information about the resources being created. 使用 debug 开关获取调试的详细信息。Use the debug switch to get more information for debugging.

回顾所做的工作Review your work

我们在已完成的六个教程中做了很多工作。You've done a lot in the last six tutorials. 让我们抽些时间来回顾所做的工作。Let's take a moment to review what you have done. 你使用易于提供的参数创建了 Bicep 文件。You created a Bicep file with parameters that are easy to provide. 该 Bicep 文件可以在不同环境中重复使用,因为它允许自定义,并且会动态创建所需值。The Bicep file is reusable in different environments because it allows for customization and dynamically creates needed values. 它还返回有关存储帐户的信息,这些信息可以用在脚本中。It also returns information about the storage account that you could use in your script.

现在,让我们来看一下资源组和部署历史记录。Now, let's look at the resource group and deployment history.

  1. 登录 Azure 门户Sign in to the Azure portal.

  2. 在左侧菜单中选择“资源组”。 From the left menu, select Resource groups.

  3. 选择已部署到的资源组。Select the resource group you deployed to.

  4. 我们会在资源组中有至少一个(也可能有多个)存储帐户,具体取决于所执行的步骤。Depending on the steps you did, you should have at least one and perhaps several storage accounts in the resource group.

  5. 此外还会在历史记录中列出多个成功的部署。You should also have several successful deployments listed in the history. 选择该链接。Select that link.

    选择部署

  6. 可以在历史记录中看到所有部署。You see all of your deployments in the history. 选择名为 addoutputs 的部署。Select the deployment called addoutputs.

    显示部署历史记录

  7. 可以查看输入。You can review the inputs.

    显示输入

  8. 可以查看输出。You can review the outputs.

    显示输出

  9. 可以查看 JSON 模板。You can review the JSON template.

    显示模板

清理资源Clean up resources

若要继续学习下一篇教程,则不需删除该资源组。If you're moving on to the next tutorial, you don't need to delete the resource group.

如果你不打算继续学习,请删除该资源组以清理部署的资源。If you're stopping now, you might want to clean up the resources you deployed by deleting the resource group.

  1. 在 Azure 门户上的左侧菜单中选择“资源组” 。From the Azure portal, select Resource group from the left menu.
  2. 在“按名称筛选”字段中输入资源组名称。Enter the resource group name in the Filter by name field.
  3. 选择资源组名称。Select the resource group name.
  4. 在顶部菜单中选择“删除资源组”。Select Delete resource group from the top menu.

后续步骤Next steps

在本教程中,你将返回值添加到了 Bicep 文件。In this tutorial, you added a return value to the Bicep file. 下一个教程将介绍如何导出 JSON 模板,以及如何在 Bicep 文件中使用该导出的模板的部件。In the next tutorial, you'll learn how to export a JSON template and use parts of that exported template in your Bicep file.