在 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
问题是我们想要打破 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屋!