本文介绍如何为 Azure 资源管理器模板(ARM 模板)中的输出创建多个值。 通过将复制循环添加到模板的 outputs 部分,可以在部署期间动态返回多个项。
将 copy
元素添加到模板的输出部分以返回多个项。 copy 元素具有以下常规格式:
"copy": {
"count": <number-of-iterations>,
"input": <values-for-the-output>
}
该 count
属性指定输出值所需的迭代数。
该 input
属性指定要重复的属性。 创建一个由 input
属性中的值构造的元素数组。 它可以是单个属性(如字符串),也可以是具有多个属性的对象。
计数不能超过 800。
计数不能为负数。 如果使用最新版本的 Azure CLI、PowerShell 或 REST API 部署模板,则为零。 具体而言,必须使用:
- Azure PowerShell 2.6 或更高版本
- Azure CLI 2.0.74 或更高版本
- REST API 版本 2019-05-10 或更高版本
- 链接部署的部署资源类型必须使用 API 版本 2019-05-10或更高版本
早期版本的 PowerShell、CLI 和 REST API 不支持将零作为有效计数。
以下示例创建一个可变数量的存储帐户,并为每个存储帐户返回一个终结点:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"storageCount": {
"type": "int",
"defaultValue": 2
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]"
}
},
"variables": {
"baseName": "[format('storage{0}', uniqueString(resourceGroup().id))]"
},
"resources": [
{
"copy": {
"name": "storagecopy",
"count": "[parameters('storageCount')]"
},
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2022-09-01",
"name": "[format('{0}{1}', copyIndex(), variables('baseName'))]",
"location": "[parameters('location')]",
"sku": {
"name": "Standard_LRS"
},
"kind": "Storage",
"properties": {}
}
],
"outputs": {
"storageEndpoints": {
"type": "array",
"copy": {
"count": "[parameters('storageCount')]",
"input": "[reference(format('{0}{1}', copyIndex(), variables('baseName'))).primaryEndpoints.blob]"
}
}
}
}
上述模板返回具有以下值的数组:
[
"https://0storagecfrbqnnmpeudi.blob.core.chinacloudapi.cn/",
"https://1storagecfrbqnnmpeudi.blob.core.chinacloudapi.cn/"
]
下一个示例返回新存储帐户中的三个属性。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"storageCount": {
"type": "int",
"defaultValue": 2
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]"
}
},
"variables": {
"baseName": "[format('storage{0}', uniqueString(resourceGroup().id))]"
},
"resources": [
{
"copy": {
"name": "storagecopy",
"count": "[length(range(0, parameters('storageCount')))]"
},
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2022-09-01",
"name": "[format('{0}{1}', range(0, parameters('storageCount'))[copyIndex()], variables('baseName'))]",
"location": "[parameters('location')]",
"sku": {
"name": "Standard_LRS"
},
"kind": "Storage",
"properties": {}
}
],
"outputs": {
"storageInfo": {
"type": "array",
"copy": {
"count": "[length(range(0, parameters('storageCount')))]",
"input": {
"id": "[resourceId('Microsoft.Storage/storageAccounts', format('{0}{1}', copyIndex(), variables('baseName')))]",
"blobEndpoint": "[reference(format('{0}{1}', copyIndex(), variables('baseName'))).primaryEndpoints.blob]",
"status": "[reference(format('{0}{1}', copyIndex(), variables('baseName'))).statusOfPrimary]"
}
}
}
}
}
前面的示例返回具有以下值的数组:
[
{
"id": "/subscriptions/00000000/resourceGroups/demoGroup/providers/Microsoft.Storage/storageAccounts/0storagecfrbqnnmpeudi",
"blobEndpoint": "https://0storagecfrbqnnmpeudi.blob.core.chinacloudapi.cn/",
"status": "available"
},
{
"id": "/subscriptions/00000000/resourceGroups/demoGroup/providers/Microsoft.Storage/storageAccounts/1storagecfrbqnnmpeudi",
"blobEndpoint": "https://1storagecfrbqnnmpeudi.blob.core.chinacloudapi.cn/",
"status": "available"
}
]
- 若要完成教程,请参阅 教程:使用 ARM 模板创建多个资源实例。
- 有关复制循环的其他用途,请参阅:
- 若要了解模板的各个部分,请参阅 了解 ARM 模板的结构和语法。
- 若要了解如何部署模板,请参阅 使用 ARM 模板和 Azure PowerShell 部署资源。