在 ARM 模板中有条件地设置 appsetting 值

Set an appsetting value conditionally in an ARM template

我们有一个 Azure ARM 模板,它正在为 Microsoft 添加应用程序设置。Web/site。

"resources": [
    {
        "apiVersion": "2016-03-01",
        "name": "myazurefunction", 
        "type": "Microsoft.Web/sites", 
        "properties": {
            "name": "myazurefunction", 
            "siteConfig": {
                "appSettings": [
{
    "name": "MY_SERVICE_URL", 
    "value": "[concat('https://myservice-', parameters('env'), '.domain.ca')]"
}
                ]    
            }
        }
    }
]

我们还有四个 parameters.environment.json 文件。例如,这是parameters.dev.json的内容。

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01...",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "env": {
            "value": "dev"
        }
    }
}

模板及其参数有利于约定优于配置。这在大多数情况下工作得很好,并导致以下 MY_SERVICE_URL 值。

问题是我们想打破 dev 环境的惯例。也就是说,我们希望它有一个看起来像这样的 MY_SERVICE_URL

我们如何配置 ARM 模板以打破只能用于一种环境的惯例?

虽然我的第一个想法是使用这样的条件,但这样的 ARM 功能似乎不可用。

"name": "MY_SERVICE_URL", 
"value": "[parameters('env') -eq 'dev' 
    ? 'https://abc123.foo.bar.baz.ca' 
    :  concat('https://myservice-', parameters('env'), '.domain.ca')]"

只需创建一个依赖于参数的变量:

"parameters": {
...
"DeploymentType": {
    "type": "string",
    "allowedValues": [
        "Dev",
        "Prod"
    ]
}
...
"variables": {
    "Dev": "https://some_service-ci.domain.com",
    "Prod": "https://abc123.foo.bar.baz.com",
    "DeploymentVariable": "[variables(parameters('DeploymentType'))]",
...
"appSettings": [
    "name": "MY_SERVICE_URL", 
    "value": "[variables('DeploymentVariable')]"
]
...

好的,那么这是如何工作的。你传入参数'DeploymentType',可以是PROD也可以是DEV。如果您传递 DEV "DeploymentVariable": "[variables(parameters('DeploymentType'))]", - 计算结果为 "[variables('Dev')]" 并获得 "Dev": "https://some_service-ci.domain.com",

的值

对于问题中的示例,答案最终如下所示:

"variables": {
   "myServiceUrl_default": "[concat('https://myservice-', parameters('env'), '.domain.ca')]",
   "myServiceUrl_dev": "https://abc123.foo.bar.baz.ca",
   "myServiceUrl_ci": "[variables('myServiceUrl_default')]",
   "myServiceUrl_qa": "[variables('myServiceUrl_default')]",
   "myServiceUrl_prod": "[variables('myServiceUrl_default')]",
   "myServiceUrl": "[variables(concat('myServiceUrl_', 'parameters('env')'))]"  
},

...

"appSettings: [
   {
      "name": "MY_SERVICE_URL",
      "value": "[variables('myServiceUrl')]"
   }
]