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.

输出迭代Outputs iteration

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.

以下示例创建可变数量的存储帐户,并返回每个存储帐户的终结点: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