教程:将输出添加到资源管理器模板Tutorial: Add outputs to your Resource Manager template

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

先决条件Prerequisites

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

必须有包含资源管理器工具扩展的 Visual Studio Code,以及 Azure PowerShell 或 Azure CLI。You must have Visual Studio Code with the Resource Manager Tools extension, and either Azure PowerShell or Azure CLI. 有关详细信息,请参阅模板工具For more information, see template tools.

审阅模板Review template

在上一篇教程的结束时,模板包含以下 JSON:At the end of the previous tutorial, your template had the following JSON:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "storagePrefix": {
            "type": "string",
            "minLength": 3,
            "maxLength": 11
        },
        "storageSKU": {
            "type": "string",
            "defaultValue": "Standard_LRS",
            "allowedValues": [
                "Standard_LRS",
                "Standard_GRS",
                "Standard_RAGRS",
                "Premium_LRS",
                "Standard_GZRS",
                "Standard_RAGZRS"
            ]
        },
        "location": {
            "type": "string",
            "defaultValue": "[resourceGroup().location]"
        }
    },
    "variables": {
        "uniqueStorageName": "[concat(parameters('storagePrefix'), uniqueString(resourceGroup().id))]"
    },
    "resources": [
        {
            "type": "Microsoft.Storage/storageAccounts",
            "apiVersion": "2019-04-01",
            "name": "[variables('uniqueStorageName')]",
            "location": "[parameters('location')]",
            "sku": {
                "name": "[parameters('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 template. 例如,可以获取新存储帐户的终结点。For example, it might be helpful to get the endpoints for your new storage account.

以下示例重点介绍添加输出值时需要对模板进行的更改。The following example highlights the change to your template to add an output value. 复制整个文件,将模板替换为该文件的内容。Copy the whole file and replace your template with its contents.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "storagePrefix": {
            "type": "string",
            "minLength": 3,
            "maxLength": 11
        },
        "storageSKU": {
            "type": "string",
            "defaultValue": "Standard_LRS",
            "allowedValues": [
                "Standard_LRS",
                "Standard_GRS",
                "Standard_RAGRS",
                "Premium_LRS",
                "Standard_GZRS",
                "Standard_RAGZRS"
            ]
        },
        "location": {
            "type": "string",
            "defaultValue": "[resourceGroup().location]"
        }
    },
    "variables": {
        "uniqueStorageName": "[concat(parameters('storagePrefix'), uniqueString(resourceGroup().id))]"
    },
    "resources": [
        {
            "type": "Microsoft.Storage/storageAccounts",
            "apiVersion": "2019-04-01",
            "name": "[variables('uniqueStorageName')]",
            "location": "[parameters('location')]",
            "sku": {
                "name": "[parameters('storageSKU')]"
            },
            "kind": "StorageV2",
            "properties": {
                "supportsHttpsTrafficOnly": true
            }
        }
    ],
    "outputs": {
        "storageEndpoint": {
            "type": "object",
            "value": "[reference(variables('uniqueStorageName')).primaryEndpoints]"
        }
    }
}

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

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

它使用 reference 函数获取存储帐户的运行时状态。It uses the reference function to get the runtime state of the storage account. 若要获取资源的运行时状态,请传入资源的名称或 ID。To get the runtime state of a resource, you pass in the name or ID of a resource. 在此示例中,使用的变量与创建存储帐户名称时使用的变量相同。In this case, you use the same variable you used to create the name of the storage account.

最后,它从存储帐户返回 primaryEndpoints 属性Finally, it returns the primaryEndpoints property from the storage account

部署模板Deploy template

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

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

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

在部署命令的输出中,将会看到如下所示的对象:In the output for the deployment command, you'll see an object similar to:

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

回顾所做的工作Review your work

我们在已完成的六个教程中做了很多工作。You've done a lot in the last six tutorials. 让我们抽些时间来回顾所做的工作。Let's take a moment to review what you have done. 我们创建了一个模板,其中的参数很容易提供。You created a template with parameters that are easy to provide. 该模板可以在不同环境中重复使用,因为它允许自定义,并且可以动态创建所需值。The template 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. 可以查看模板。You can review the 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

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