英文:
ARM Template - conditionally evaluate reference function
问题
"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
}
}
英文:
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?
答案1
得分: 0
你可以使用nested if()
函数来有条件地将一个模板的输出传递到另一个模板中。
我将代码修改如下:
{
"$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, ''))]"
}
}
}
}
所以,在这里,如果isInfraEnabled
设置为true
,则会调用reference()
函数,并将结果传递给值参数。如果isInfraEnabled
为false
,外部的if()
函数会检查它并返回一个空字符串,忽略来自部署的整个参数。
因此,内部的if()
函数用于识别这个条件并返回默认值(variables('myStorageAccount').deploymentName)
。
部署成功:
成功部署:
有关更详细信息,请参阅输出 ARM 模板。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论