在ARM模板中有条件地设置appsetting值 [英] Set an appsetting value conditionally in an ARM template

查看:99
本文介绍了在ARM模板中有条件地设置appsetting值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个Azure ARM模板,该模板正在为Microsoft.Web/站点添加应用程序设置.

We have an Azure ARM template, which is adding appsettings for a 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的内容.

We also have four parameters.environment.json files. For instance, this is the content of 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值.

The template and its parameters favor convention over configuration. This is working nicely for the most part, and leads to the following MY_SERVICE_URL values.

  • https://myservice-dev.domain.ca
  • https://myservice-qa.domain.ca
  • https://myservice-ci.domain.ca
  • https://myservice-prod.domain.ca

问题是我们要打破dev环境的约定.也就是说,我们希望它的MY_SERVICE_URL看起来像这样:

The problem is that we want to break the convention for the dev environment. That is, we want it to have a MY_SERVICE_URL that looks something like this:

我们如何配置ARM模板以仅针对一种环境违反约定?

How can we configure the ARM template to break the convention for only one environment?

我的第一个方法是使用这样的条件,但是这样的ARM函数似乎不可用.

My first though is to use a conditional like this, but such an ARM function appears not to be available.

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

推荐答案

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

just create a variable that would depend on the parameter:

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

Ok, so how does this work. you pass in the parameter 'DeploymentType', it can be PROD or DEV. If you pass DEV "DeploymentVariable": "[variables(parameters('DeploymentType'))]", - this evaluates to "[variables('Dev')]" and gets the value of "Dev": "https://some_service-ci.domain.com",

这篇关于在ARM模板中有条件地设置appsetting值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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