ARM 模板中的输出迭代Output iteration in ARM templates

本文展示了如何为 Azure 资源管理器 (ARM) 模板中的输出创建多个值。This article shows you how to create more than one value for an output in your Azure Resource Manager (ARM) template. 通过将 copy 元素添加到模板的 outputs 节,可以在部署过程中动态返回许多项。By adding the copy element to the outputs section of your template, you can dynamically return a number of items during deployment.

还可以将 copy 用于资源资源中的属性,以及变量You can also use copy with resources, properties in a resource, and variables.

语法Syntax

copy 元素采用以下常规格式:The copy element has the following general format:

"copy": {
  "count": <number-of-iterations>,
  "input": <values-for-the-output>
}

count 属性指定要对该输出值进行的迭代次数。The count property specifies the number of iterations you want for the output value.

input 属性指定要重复的属性。The input property specifies the properties that you want to repeat. 你将创建一个由 input 属性中的值构造的元素数组。You create an array of elements constructed from the value in the input property. 它可以是单个属性(例如字符串),也可以是具有多个属性的对象。It can be a single property (like a string), or an object with several properties.

复制限制Copy limits

count 不能超过 800。The count can't exceed 800.

count 不能为负数。The count can't be a negative number. 如果使用最新版本的 Azure CLI、PowerShell 或 REST API 部署模板,则它可以为零。It can be zero if you deploy the template with a recent version of Azure CLI, PowerShell, or REST API. 具体而言,必须使用:Specifically, you must use:

  • Azure PowerShell 2.6 或更高版本****Azure PowerShell 2.6 or later
  • Azure CLI 2.0.74 或更高版本****Azure CLI 2.0.74 or later
  • REST API 版本 2019-05-10 或更高版本****REST API version 2019-05-10 or later
  • 链接的部署必须将 API 版本 2019-05-10 或更高版本用于部署资源类型****Linked deployments must use API version 2019-05-10 or later for the deployment resource type

更早版本的 PowerShell、CLI 和 REST API 不支持将 count 设为零。Earlier versions of PowerShell, CLI, and the REST API don't support zero for count.

输出迭代Outputs iteration

以下示例创建可变数量的存储帐户,并返回每个存储帐户的终结点:The following example creates a variable number of storage accounts and returns an endpoint for each storage account:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "storageCount": {
            "type": "int",
            "defaultValue": 2
        }
    },
    "variables": {
        "baseName": "[concat('storage', uniqueString(resourceGroup().id))]"
    },
    "resources": [
        {
            "type": "Microsoft.Storage/storageAccounts",
            "apiVersion": "2019-04-01",
            "name": "[concat(copyIndex(), variables('baseName'))]",
            "location": "[resourceGroup().location]",
            "sku": {
                "name": "Standard_LRS"
            },
            "kind": "Storage",
            "properties": {},
            "copy": {
                "name": "storagecopy",
                "count": "[parameters('storageCount')]"
            }
        }
    ],
    "outputs": {
        "storageEndpoints": {
            "type": "array",
            "copy": {
                "count": "[parameters('storageCount')]",
                "input": "[reference(concat(copyIndex(), variables('baseName'))).primaryEndpoints.blob]"
            }
        }
    }
}

前面的模板返回包含以下值的数组:The preceding template returns an array with the following values:

[
    "https://0storagecfrbqnnmpeudi.blob.core.chinacloudapi.cn/",
    "https://1storagecfrbqnnmpeudi.blob.core.chinacloudapi.cn/"
]

下一示例返回新存储帐户的三个属性。The next example returns three properties from the new storage accounts.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "storageCount": {
            "type": "int",
            "defaultValue": 2
        }
    },
    "variables": {
        "baseName": "[concat('storage', uniqueString(resourceGroup().id))]"
    },
    "resources": [
        {
            "type": "Microsoft.Storage/storageAccounts",
            "apiVersion": "2019-04-01",
            "name": "[concat(copyIndex(), variables('baseName'))]",
            "location": "[resourceGroup().location]",
            "sku": {
                "name": "Standard_LRS"
            },
            "kind": "Storage",
            "properties": {},
            "copy": {
                "name": "storagecopy",
                "count": "[parameters('storageCount')]"
            }
        }
    ],
    "outputs": {
        "storageInfo": {
            "type": "array",
            "copy": {
                "count": "[parameters('storageCount')]",
                "input": {
                    "id": "[reference(concat(copyIndex(), variables('baseName')), '2019-04-01', 'Full').resourceId]",
                    "blobEndpoint": "[reference(concat(copyIndex(), variables('baseName'))).primaryEndpoints.blob]",
                    "status": "[reference(concat(copyIndex(), variables('baseName'))).statusOfPrimary]"
                }
            }
        }
    }
}

前面的示例返回包含以下值的数组:The preceding example returns an array with the following values:

[
    {
        "id": "Microsoft.Storage/storageAccounts/0storagecfrbqnnmpeudi",
        "blobEndpoint": "https://0storagecfrbqnnmpeudi.blob.core.chinacloudapi.cn/",
        "status": "available"
    },
    {
        "id": "Microsoft.Storage/storageAccounts/1storagecfrbqnnmpeudi",
        "blobEndpoint": "https://1storagecfrbqnnmpeudi.blob.core.chinacloudapi.cn/",
        "status": "available"
    }
]

后续步骤Next steps