Tags 带条件的ARM模板资源标记
我已经创建了ARM模板,以便在我的开发环境中发布azure资源 现在我需要在template.json中为资源标记添加一个条件,即仅当subscription().displayName为“Dev”时,它才会创建标记 除了“Dev”,它不应该从资源下的template.json创建任何标记Tags 带条件的ARM模板资源标记,tags,arm,conditional-statements,Tags,Arm,Conditional Statements,我已经创建了ARM模板,以便在我的开发环境中发布azure资源 现在我需要在template.json中为资源标记添加一个条件,即仅当subscription().displayName为“Dev”时,它才会创建标记 除了“Dev”,它不应该从资源下的template.json创建任何标记 "resources": [ { "type": "Microsoft.Web/connections", "apiVersion": "201
"resources": [
{
"type": "Microsoft.Web/connections",
"apiVersion": "2016-06-01",
"name": "[parameters('connections_office365_name')]",
"location": "[resourceGroup().location]",
"properties": {
"displayName": "manish.jain@gmail.com",
"customParameterValues": {},
"api": {
"id": "[concat('/subscriptions/',subscription().subscriptionId,'/providers/Microsoft.Web/locations/',resourceGroup().location,'/managedApis/office365')]"
}
},
"tags": {
"Creator": "Manish Jain",
"Environment": "Dev",
"Date": "08/31/2019"
}
}
以下ARM模板演示了如何使整个标记字典有条件:
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"type": "string"
},
"storageAccountName": {
"type": "string"
},
"accountType": {
"type": "string"
},
"kind": {
"type": "string"
},
"accessTier": {
"type": "string"
},
"supportsHttpsTrafficOnly": {
"type": "bool"
},
"createTag": {
"type": "bool"
}
},
"variables": {},
"resources": [
{
"name": "[parameters('storageAccountName')]",
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2019-06-01",
"location": "[parameters('location')]",
"properties": {
"accessTier": "[parameters('accessTier')]",
"supportsHttpsTrafficOnly": "[parameters('supportsHttpsTrafficOnly')]"
},
"dependsOn": [],
"sku": {
"name": "[parameters('accountType')]"
},
"kind": "[parameters('kind')]",
"tags": "[if(parameters('createTag'),json('{\"tag1\": \"value1\",\"tag2\":\"value2\"}'), json('{}'))]"
}
],
"outputs": {}
}
要将此应用于您的特定场景,请使用
[if(parameters('createTag')…
而不是[if(equals)(subscription().displayName,'Dev')…
以下ARM模板演示如何使整个标记字典有条件:
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"type": "string"
},
"storageAccountName": {
"type": "string"
},
"accountType": {
"type": "string"
},
"kind": {
"type": "string"
},
"accessTier": {
"type": "string"
},
"supportsHttpsTrafficOnly": {
"type": "bool"
},
"createTag": {
"type": "bool"
}
},
"variables": {},
"resources": [
{
"name": "[parameters('storageAccountName')]",
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2019-06-01",
"location": "[parameters('location')]",
"properties": {
"accessTier": "[parameters('accessTier')]",
"supportsHttpsTrafficOnly": "[parameters('supportsHttpsTrafficOnly')]"
},
"dependsOn": [],
"sku": {
"name": "[parameters('accountType')]"
},
"kind": "[parameters('kind')]",
"tags": "[if(parameters('createTag'),json('{\"tag1\": \"value1\",\"tag2\":\"value2\"}'), json('{}'))]"
}
],
"outputs": {}
}
要将此应用于您的特定场景,使用if块的条件标记可以起作用,而不是
[if(parameters('createTag')…
)…[if(equals)(subscription().displayName,'Dev')…但是,这是最佳实践吗
如果您是基于环境有条件地创建标记,为什么不创建标记值作为参数并将其传递到ARM模板中呢
这将避免整个条件块,相反,参数模板中定义的特定标记值将插入到ARM模板中
这种方法的缺点是,每个环境都需要标记值,而不仅仅是较低的环境。但是,您的一致性会提高,因为每个环境都可以有相同的标记和不同的值。使用if块的条件标记有效;但是,这是最佳做法吗 如果您是基于环境有条件地创建标记,为什么不创建标记值作为参数并将其传递到ARM模板中呢 这将避免整个条件块,相反,参数模板中定义的特定标记值将插入到ARM模板中
这种方法的缺点是,每个环境都需要标记值,而不仅仅是较低的环境。但是,由于每个环境都可以有相同的标记和不同的值,因此您的一致性将得到提高。我创建了一个悬赏,希望有人能帮我解决这个问题。然后,我找到了我自己。希望这会有帮助!我创造了一个悬赏,希望有人能帮我解决这个问题。然后我自己解决了。希望这会有帮助!