本文介绍如何在 Azure 资源管理器模板(ARM 模板)中创建多个属性实例。 通过将复制循环添加到模板中资源的属性部分,可以在部署期间动态设置属性的项数。 还可以避免重复模板语法。
只能将复制循环与顶级资源一起使用,即使将复制循环应用于属性也是如此。 若要了解如何将子资源更改为顶级资源,请参阅 子资源的迭代。
语法
将 copy
元素添加到模板的资源部分,以设置属性的项数。 copy 元素具有以下常规格式:
"copy": [
{
"name": "<name-of-property>",
"count": <number-of-iterations>,
"input": <values-for-the-property>
}
]
对于 name
,请提供要创建的资源属性的名称。
该 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 不支持将零作为有效计数。
属性迭代
以下示例演示如何将 copy 循环应用于 dataDisks
虚拟机上的属性:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"numberOfDataDisks": {
"type": "int",
"minValue": 0,
"maxValue": 16,
"defaultValue": 3,
"metadata": {
"description": "The number of dataDisks to create."
}
},
...
},
"resources": [
{
"type": "Microsoft.Compute/virtualMachines",
"apiVersion": "2022-11-01",
...
"properties": {
"storageProfile": {
...
"copy": [
{
"name": "dataDisks",
"count": "[parameters('numberOfDataDisks')]",
"input": {
"lun": "[copyIndex('dataDisks')]",
"createOption": "Empty",
"diskSizeGB": 1023
}
}
]
}
...
}
}
]
}
请注意,在属性迭代中使用 copyIndex 时,必须提供迭代的名称。 属性迭代还支持偏移参数。 偏移量必须位于迭代名称之后,例如 copyIndex('dataDisks', 1)
。
部署的模板变为:
{
"name": "examplevm",
"type": "Microsoft.Compute/virtualMachines",
"apiVersion": "2020-06-01",
"properties": {
"storageProfile": {
"dataDisks": [
{
"lun": 0,
"createOption": "Empty",
"diskSizeGB": 1023
},
{
"lun": 1,
"createOption": "Empty",
"diskSizeGB": 1023
},
{
"lun": 2,
"createOption": "Empty",
"diskSizeGB": 1023
}
],
...
在处理数组时,复制操作非常有用,因为你可以遍历数组中的每个元素。 使用数组上的 length 函数指定迭代计数,并 copyIndex
检索数组中的当前索引。
以下示例模板为作为数组传入的数据库创建故障转移组。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"primaryServerName": {
"type": "string"
},
"secondaryServerName": {
"type": "string"
},
"databaseNames": {
"type": "array",
"defaultValue": [
"mydb1",
"mydb2",
"mydb3"
]
}
},
"variables": {
"failoverName": "[format('{0}/{1}failovergroups', parameters('primaryServerName'), parameters('primaryServerName'))]"
},
"resources": [
{
"type": "Microsoft.Sql/servers/failoverGroups",
"apiVersion": "2015-05-01-preview",
"name": "[variables('failoverName')]",
"properties": {
"readWriteEndpoint": {
"failoverPolicy": "Automatic",
"failoverWithDataLossGracePeriodMinutes": 60
},
"readOnlyEndpoint": {
"failoverPolicy": "Disabled"
},
"partnerServers": [
{
"id": "[resourceId('Microsoft.Sql/servers', parameters('secondaryServerName'))]"
}
],
"copy": [
{
"name": "databases",
"count": "[length(parameters('databaseNames'))]",
"input": "[resourceId('Microsoft.Sql/servers/databases', parameters('primaryServerName'), parameters('databaseNames')[copyIndex('databases')])]"
}
]
}
}
],
"outputs": {
}
}
该 copy
元素是一个数组,因此可以为资源指定多个属性。
{
"type": "Microsoft.Network/loadBalancers",
"apiVersion": "2017-10-01",
"name": "exampleLB",
"properties": {
"copy": [
{
"name": "loadBalancingRules",
"count": "[length(parameters('loadBalancingRules'))]",
"input": {
...
}
},
{
"name": "probes",
"count": "[length(parameters('loadBalancingRules'))]",
"input": {
...
}
}
]
}
}
可以将资源和属性迭代一起使用。 按名称引用属性迭代。
{
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2018-04-01",
"name": "[format('{0}{1}', parameters('vnetname'), copyIndex())]",
"copy":{
"count": 2,
"name": "vnetloop"
},
"___location": "[resourceGroup().___location]",
"properties": {
"addressSpace": {
"addressPrefixes": [
"[parameters('addressPrefix')]"
]
},
"copy": [
{
"name": "subnets",
"count": 2,
"input": {
"name": "[format('subnet-{0}', copyIndex('subnets'))]",
"properties": {
"addressPrefix": "[variables('subnetAddressPrefix')[copyIndex('subnets')]]"
}
}
}
]
}
}
示例模板
以下示例演示了为属性创建多个值的常见方案。
模板 | DESCRIPTION |
---|---|
具有可变数据磁盘数的 VM 部署 | 使用虚拟机部署多个数据磁盘。 |
后续步骤
- 若要完成教程,请参阅 教程:使用 ARM 模板创建多个资源实例。
- 有关复制循环的其他用途,请参阅:
- 若要了解模板的各个部分,请参阅 了解 ARM 模板的结构和语法。
- 若要了解如何部署模板,请参阅 使用 ARM 模板和 Azure PowerShell 部署资源。