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

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

先决条件

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

需要具有 Visual Studio Code,以及 Azure PowerShell 或 Azure Command-Line 接口(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 对象。

它使用 引用 函数获取存储帐户的运行时状态。 若要获取资源的运行时状态,请传递资源的名称或 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. 还应在历史记录中列出多个成功的部署。 选择该链接。

    Azure 门户的屏幕截图,其中显示了部署链接。

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

    Azure 门户的屏幕截图,其中显示了部署历史记录。

  7. 可以查看输入。

    Azure 门户的屏幕截图,其中显示了部署输入。

  8. 可以查看系统输出。

    Azure 门户的屏幕截图,其中显示了部署输出。

  9. 可以查看模板。

    Azure 门户的屏幕截图,其中显示了部署模板。

清理资源

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

如果您此时停止操作,您可能希望删除资源组。

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

后续步骤

在本教程中,你向模板添加了返回值。 在下一教程中,你将了解如何导出模板,并在模板中使用该导出模板的一部分。