教程:将输出添加到 ARM 模板

本教程介绍如何从 Azure 资源管理器模板(ARM 模板)返回值。 需要所部署资源的值时,请使用输出。 完成本教程需要 7 分钟

先决条件

建议完成有关变量的教程,但这不是必需的。

需要具有 Visual Studio Code 和资源管理器工具扩展,以及 Azure PowerShell 或 Azure 命令行接口 (CLI)。 有关详细信息,请参阅模板工具

审阅模板

在上一篇教程的结束时,模板包含以下 JSON:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-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"
      ]
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]"
    }
  },
  "variables": {
    "uniqueStorageName": "[concat(parameters('storagePrefix'), uniqueString(resourceGroup().id))]"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2021-09-01",
      "name": "[variables('uniqueStorageName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[parameters('storageSKU')]"
      },
      "kind": "StorageV2",
      "properties": {
        "supportsHttpsTrafficOnly": true
      }
    }
  ]
}

它部署一个存储帐户,但不返回任何相关信息。 可能需要从新资源中捕获属性,以便以后可以引用它们。

添加输出

可以使用输出,这样就可以从模板返回值。 例如,获取新存储帐户的终结点可能会有所帮助。

以下示例重点介绍添加输出值时需要对模板进行的更改。 复制整个文件,将模板替换为该文件的内容。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-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"
      ]
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]"
    }
  },
  "variables": {
    "uniqueStorageName": "[concat(parameters('storagePrefix'), uniqueString(resourceGroup().id))]"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2021-09-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]"
    }
  }
}

关于添加的输出值,有一些重要的值得注意的项。

返回的值的类型设置为 object,这意味着它会返回 JSON 对象。

它使用 reference 函数获取存储帐户的运行时状态。 若要获取资源的运行时状态,请传递资源的名称或 ID。 在此示例中,使用的变量与创建存储帐户名称时使用的变量相同。

最后,它从存储帐户返回 primaryEndpoints 属性。

部署模板

现在可以部署模板并查看返回的值了。

如果尚未创建资源组,请参阅创建资源组。 此示例假设已根据第一篇教程中所述,将 templateFile 变量设置为模板文件的路径。

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

在部署命令的输出中,只有当输出为 JSON 格式时,才能看到类似于以下示例的对象:

{
    "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 开关获取有关正在创建的资源的信息。 使用 debug 开关获取调试的详细信息。

回顾所做的工作

我们在已完成的六个教程中做了很多工作。 让我们花点时间回顾一下你所完成的事情。 我们创建了一个模板,其中的参数很容易提供。 该模板可以在不同环境中重复使用,因为它允许自定义,并且可以动态创建所需值。 它还返回有关存储帐户的信息,这些信息可以用在脚本中。

现在,让我们来看一下资源组和部署历史记录。

  1. 登录 Azure 门户

  2. 在左侧菜单中选择“资源组”。

  3. 选择已部署到的资源组。

  4. 我们会在资源组中有至少一个(也可能有多个)存储帐户,具体取决于所执行的步骤。

  5. 此外还会在历史记录中列出多个成功的部署。 选择该链接。

    Screenshot of the Azure portal showing the deployments link.

  6. 可以在历史记录中看到所有部署。 选择名为 addoutputs 的部署。

    Screenshot of the Azure portal showing the deployment history.

  7. 可以查看输入。

    Screenshot of the Azure portal showing the deployment inputs.

  8. 可以查看输出。

    Screenshot of the Azure portal showing the deployment outputs.

  9. 可以查看模板。

    Screenshot of the Azure portal showing the deployment template.

清理资源

若要继续学习下一篇教程,则不需删除该资源组。

如果就此停止学习,请删除该资源组。

  1. 在 Azure 门户上的左侧菜单中选择“资源组” 。
  2. 在“筛选任何字段…”文本字段中键入资源组名称。
  3. 选中“myResourceGroup”旁边的框,然后选择“myResourceGroup”或资源组名称。
  4. 在顶部菜单中选择“删除资源组”。

后续步骤

在本教程中,我们向模板添加了一个返回值。 在下一教程中,你将了解如何导出模板,以及如何在你的模板中使用该导出的模板的部件。