Skip to content

Commit 7fc1937

Browse files
authored
Merge pull request #110838 from tfitzmac/0408copy
update property copy
2 parents d11ccde + 2f57555 commit 7fc1937

File tree

1 file changed

+65
-7
lines changed

1 file changed

+65
-7
lines changed

articles/azure-resource-manager/templates/copy-properties.md

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Define multiple instances of a property
33
description: Use copy operation in an Azure Resource Manager template to iterate multiple times when creating a property on a resource.
44
ms.topic: conceptual
5-
ms.date: 02/13/2020
5+
ms.date: 04/14/2020
66
---
77
# Property iteration in ARM templates
88

@@ -24,7 +24,9 @@ The copy element has the following general format:
2424
]
2525
```
2626

27-
For **name**, provide the name of the resource property that you want to create. The **count** property specifies the number of iterations you want for the property.
27+
For **name**, provide the name of the resource property that you want to create.
28+
29+
The **count** property specifies the number of iterations you want for the property.
2830

2931
The **input** property specifies the properties that you want to repeat. You create an array of elements constructed from the value in the **input** property.
3032

@@ -72,11 +74,7 @@ The following example shows how to apply `copy` to the dataDisks property on a v
7274
}
7375
```
7476

75-
Notice that when using `copyIndex` inside a property iteration, you must provide the name of the iteration.
76-
77-
> [!NOTE]
78-
> Property iteration also supports an offset argument. The offset must come after the name of the iteration, such as copyIndex('dataDisks', 1).
79-
>
77+
Notice that when using `copyIndex` inside a property iteration, you must provide the name of the iteration. Property iteration also supports an offset argument. The offset must come after the name of the iteration, such as copyIndex('dataDisks', 1).
8078

8179
Resource Manager expands the `copy` array during deployment. The name of the array becomes the name of the property. The input values become the object properties. The deployed template becomes:
8280

@@ -107,6 +105,66 @@ Resource Manager expands the `copy` array during deployment. The name of the arr
107105
...
108106
```
109107

108+
The copy operation is helpful when working with arrays because you can iterate through each element in the array. Use the `length` function on the array to specify the count for iterations, and `copyIndex` to retrieve the current index in the array.
109+
110+
The following example template creates a failover group for databases that are passed in as an array.
111+
112+
```json
113+
{
114+
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
115+
"contentVersion": "1.0.0.0",
116+
"parameters": {
117+
"primaryServerName": {
118+
"type": "string"
119+
},
120+
"secondaryServerName": {
121+
"type": "string"
122+
},
123+
"databaseNames": {
124+
"type": "array",
125+
"defaultValue": [
126+
"mydb1",
127+
"mydb2",
128+
"mydb3"
129+
]
130+
}
131+
},
132+
"variables": {
133+
"failoverName": "[concat(parameters('primaryServerName'),'/', parameters('primaryServerName'),'failovergroups')]"
134+
},
135+
"resources": [
136+
{
137+
"type": "Microsoft.Sql/servers/failoverGroups",
138+
"apiVersion": "2015-05-01-preview",
139+
"name": "[variables('failoverName')]",
140+
"properties": {
141+
"readWriteEndpoint": {
142+
"failoverPolicy": "Automatic",
143+
"failoverWithDataLossGracePeriodMinutes": 60
144+
},
145+
"readOnlyEndpoint": {
146+
"failoverPolicy": "Disabled"
147+
},
148+
"partnerServers": [
149+
{
150+
"id": "[resourceId('Microsoft.Sql/servers', parameters('secondaryServerName'))]"
151+
}
152+
],
153+
"copy": [
154+
{
155+
"name": "databases",
156+
"count": "[length(parameters('databaseNames'))]",
157+
"input": "[resourceId('Microsoft.Sql/servers/databases', parameters('primaryServerName'), parameters('databaseNames')[copyIndex('databases')])]"
158+
}
159+
]
160+
}
161+
}
162+
],
163+
"outputs": {
164+
}
165+
}
166+
```
167+
110168
The copy element is an array so you can specify more than one property for the resource.
111169

112170
```json

0 commit comments

Comments
 (0)