The Microsoft documentation for modularizing ARM templates is missing a sample where the copy loop is combined with returning output. I modified the example template which Microsoft uses in their Get values from linked template example to demonstrate this scenario.
The linked template gets a parameter where we can pass in a name for the greeting.
{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "name": { "type": "string" } }, "variables": {}, "resources": [], "outputs": { "greetingMessage": { "value": "[concat('Hello ', parameters('name')]", "type": "string" } } }
The main template deploys the linked template and gets the returned values from the name array. Notice that it references the deployment resource by name, and it uses the name of the property returned by the linked template.
{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "names": { "type": "array" } }, "variables": {}, "resources": [ { "type": "Microsoft.Resources/deployments", "apiVersion": "2020-06-01", "name": "linkedTemplate", "properties": { "mode": "Incremental", "templateLink": { "uri": "[uri(deployment().properties.templateLink.uri, 'helloworld.json')]", "contentVersion": "1.0.0.0" }, "parameters": { "name": { "value": "[parameters('names')[copyIndex()]]" } } }, "copy": { "name": "namesArray", "count": "[length(parameters('names'))]", "mode": "serial" } } ], "outputs": { "messageFromLinkedTemplates": { "type": "array", "copy": { "count": "[length(parameters('names'))]", "input": "[reference('linkedTemplate')[copyIndex()].outputs.greetingMessage.value]" } } } }
Comments
Post a Comment