azure-devops - 如何在ARM模板azure中使用if else

标签 azure-devops azure-pipelines azure-rm-template

我在 C# 中创建了示例函数,它根据参数设置位置值。我想使用arm模板样式格式编写下面的表达式。

public static Main(string name)
{
    string location = string.Empty;
    if(name == "uksouth")
    {
        location = "UKS";
    }else if(name == "ukwest")
    {
        location = "UKE";
    }else if(name == "IndiaWest")
    {
        location = "INDW";
    }
    else {
        location = "INDS";
    }

}

我已经为一个匹配条件编写了此内容,但我想根据用户资源组返回值。

"value": "[if(equals(resourceGroup().location,'uksouth'), 'UKS', 'EUS')]"

最佳答案

不幸的是,ARM 模板不提供相当于“开关”的机制,而这可能会使这变得更容易。但是,您可以嵌套多个 if 语句。语法有点笨拙,但这应该与您编写的代码等效:

"value": "[if(equals(resourceGroup().location,'uksouth'), 'UKS', [if(equals(resourceGroup().location,'ukwest'), 'UKE', [if(equals(resourceGroup().location,'IndiaWest'), 'INDW', 'INDS')])])]"

下面是相同的代码,但应用了一些格式,以使这里发生的情况更加明显:

"value": "
    [if(equals(resourceGroup().location,'uksouth'), 
        'UKS', 
    [if(equals(resourceGroup().location,'ukwest'), 
        'UKE', 
    [if(equals(resourceGroup().location,'IndiaWest'), 
        'INDW', 
    'INDS')])])]
"

您还可以考虑 this answer 中描述的方法寻求更清洁的解决方案。

关于azure-devops - 如何在ARM模板azure中使用if else,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69899202/

相关文章:

azure-devops - ## [错误]在以下环境变量: BUILD_BUILDNUMBER中找不到版本号数据

azure - 如何编写 azure 函数来触发 azure devops 管道?这可能吗?

azure-devops - 如果 yaml 中的前一阶段部署条件设置为 false,则阻止部署

.net - 在 Azure DevOps 中获取 UseDotNet@2 以使用 vmImage 的 dotnet SDK(或来自缓存的 SDK)

azure - BlobServiceClient 对象没有属性 `exists`

azure - 如何将运行时参数的数量传递给Azure管道中的变量?

azure - 通过 ARM 模板对 EndpointType 为 AzureFunction 的主题进行事件订阅

c# - 在 VSTS 上构建 MonoGame 项目

Azure Web Job App.Config 转换在 TFS 发布管道中不起作用

azure - 使用 ARM 模板在 cosmos Db 中启用 disableLocalAuth