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

时间:2017-03-08 17:33:48

标签: azure azure-web-sites arm-template

我们有一个Azure ARM模板,它正在为Microsoft.Web / site添加appsetting。

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

2 个答案:

答案 0 :(得分:2)

只需创建一个取决于参数的变量:

"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",

的值

答案 1 :(得分:0)

对于问题中的示例,答案最终看起来像这样:

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