I am trying to send the output of a deployment to another template as an argument, but conditionally.
Here is my code:
"properties": {
"mode": "[variables('deploymentMode')]",
"templateLink": {
"relativePath": "MyChildTemplate.json"
},
"parameters": {
"blobEndpoint": {
"value": "[if(parameters('isInfraEnabled'), reference(variables('myStorageAccount').deploymentName, variables('deploymentAPIVersion')).outputs.blobStorageEndpoint.value, variables('myStorageAccount').deploymentName)]"
},
//other parameters
}
So the flag isInfraEnabled is false, but it looks like ARM is still trying to evaluate the reference() and the deployment is not found. The deployment is also conditional on isInfraEnabled flag so it wont be found.
How can I solve this?
You can use the nested if()
functions to conditionally pass the outputs from one template to another.
I modified the code as below:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [],
"properties": {
"mode": "[variables('deploymentMode')]",
"templateLink": {
"relativePath": "storage.json"
},
"parameters": {
"blobEndpoint": {
"value": "[if(parameters('isInfraEnabled'), reference(variables('myStorageAccount').deploymentName, variables('deploymentAPIVersion')).outputs.blobStorageEndpoint.value, if(equals(parameters('isInfraEnabled'), false), variables('myStorageAccount').deploymentName, ''))]"
}
}
}
}
So here, If isInfraEnabled
is set to true
, the reference()
function is called and the result is passed to the value parameter. If isInfraEnabled
is false
, the outer if()
function checks for it and returns an empty string, ignoring the entire argument from the deployment.
So, the inner if()
function that identifies this condition and returns the default value (variables('myStorageAccount').deploymentName)
.
Deployment succeeded:
Deployed successfully:
Refer this output arm templates for more detailed information.