2017-03-08 65 views
1

我們有一個Azure ARM模板,它爲Microsoft.Web /站點添加appsettings。在ARM模板中有條件地設置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

剛剛創建,將取決於參數變量:

"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

對不起,固定一個錯字,並添加評論@ ShaunLuttin – 4c74356b41

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