ARM模板 – 有条件地评估引用函数

huangapple go评论44阅读模式
英文:

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()函数,并将结果传递给值参数。如果isInfraEnabledfalse,外部的if()函数会检查它并返回一个空字符串,忽略来自部署的整个参数。

因此,内部的if()函数用于识别这个条件并返回默认值(variables('myStorageAccount').deploymentName)

部署成功:

ARM模板 – 有条件地评估引用函数

成功部署:

ARM模板 – 有条件地评估引用函数

有关更详细信息,请参阅输出 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:

ARM模板 – 有条件地评估引用函数

Deployed successfully:

ARM模板 – 有条件地评估引用函数

Refer this output arm templates for more detailed information.

huangapple
  • 本文由 发表于 2023年4月7日 00:12:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75951612.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定