if() 函数如何在 Azure 资源管理器模板中执行

How the if() function executes in Azure Resource Manager Templates

我在我的 ARM 模板中使用 if() 函数来有条件地在我的 Web 应用程序资源中设置一些连接字符串值。目前的情况是这样的。

"[if(equals(parameters('isProduction'), 'Yes'), concat(variables('redisCacheName'),'.redis.cache.windows.net:6380|', listKeys(resourceId('Microsoft.Cache/Redis', variables('redisCacheName')), '2015-08-01').primaryKey, '|', variables('resourcePrefix')), parameters('redisSessionStateConnection'))]"

为简化起见,条件如下所示;

[if(equals(arg1, arg2), true_expression, false_expression)]

当我部署 ARM 模板并将 isProduction 参数设置为 No 时,执行会引发异常。当 isProduction 参数设置为 Yes 时,模板可以正常工作。异常与 ARM 试图找到不会在 非生产 环境中部署的 redis 缓存资源有关。

我猜即使isProduction参数值为No,上面条件中的true_expression也是引用了Redis缓存资源正在执行,由于 Redis 缓存资源不是在非生产状态下创建的,它会抛出异常。

所以我的问题是,当我们遇到上述情况时,true_expressionfalse_expressionif() 函数的实际条件执行之前求值?

如果是这样,有什么可能的解决方法来解决这个问题?

我的猜测是:不,只有 是的,根据 if 语句的结果 需要的两个表达式 is 都被评估.

要解决您的问题:您可以使用特定于环境的参数文件。这使您可以只包含要部署到的环境的参数。

请参阅 'Understand the structure and syntax of Azure Resource Manager templates' 文章中的 documentation on parameters

In the parameters section of the template, you specify which values you can input when deploying the resources. These parameter values enable you to customize the deployment by providing values that are tailored for a particular environment (such as dev, test, and production). You do not have to provide parameters in your template, but without parameters your template would always deploy the same resources with the same names, locations, and properties.

if() 的两边都被评估(在 ARM 模板中)。所以你必须使用 "clever" 方法来解决这个问题。

您可以使用嵌套 deployments\variables 来尝试解决这个问题。

更新:这已在前一段时间修复,仅评估 if() 函数的相关部分。