在ARM模板中有条件地设置appsetting值 [英] Set an appsetting value conditionally in an ARM template
问题描述
我们有一个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
- 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屋!