if() 函数如何在 Azure 资源管理器模板中执行 [英] How the if() function executes in Azure Resource Manager Templates

查看:22
本文介绍了if() 函数如何在 Azure 资源管理器模板中执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using if() functions in my ARM template to conditionally set some connection string values in my Web App resource. The current condition looks like this.

"[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'))]"

为了简化,条件如下所示;

To simplify it, the condition looks like this;

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

当我使用 isProduction 参数设置为 No 部署 ARM 模板时,执行会引发异常.当 isProduction 参数设置为 Yes 时,模板工作正常.异常与 ARM 尝试查找不会部署在非生产环境中的 redis 缓存资源有关.

When I deploy the ARM template with isProduction parameter set to No the execution throws an exception. When isProduction parameter is set to Yes then the template works fine. The exceptions is related to ARM trying to find the redis cache resource which will not be deployed in non production environment.

我的猜测是,即使 isProduction 参数值为 No,上述条件中引用 Redis Cache 资源的 true_expression 是正在执行,并且由于 Redis 缓存资源不是在非生产状态下创建的,它会引发异常.

My guess is even if the isProduction parameter value is No, the true_expression in the above condition which is referencing the Redis Cache resource is getting executed and since the Redis Cache resource is not created in a non production state, it throws the exception.

所以我的问题是,当我们有上述条件时,if() 函数中的 true_expressionfalse_expression 是否是在执行 if() 函数的实际条件之前评估?

So my question is, when we have a condition like above, will the true_expression and the false_expression in the if() function is evaluated before the actual condition of the if() function is executed?

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

If so what would be possible workarounds to get around this problem?

推荐答案

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

Both sides of if() are evaluated regardless (in ARM templates). so you have to work around that using "clever" ways.

您可以使用嵌套部署\变量来尝试解决这个问题.

you could use nested deployments\variables to try and work around that.

更新:这个已经修复了一段时间,只评估了if()函数的相关部分.

update: this has been fixed some time ago, only the relevant part of if() function is evaluated.

这篇关于if() 函数如何在 Azure 资源管理器模板中执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆