【发布时间】:2019-03-03 19:08:48
【问题描述】:
有没有办法创建 Azure 策略,要求在创建资源时在资源上存在标签,但不检查特定值?我见过的所有例子都是“检查标签 X 是否存在,并且值 Y”。
我只想让用户知道“您需要在此资源上放置标签 X”,因为该值是用户定义的,因此我无法强制执行特定值。
例如 - 我在每个资源上都需要“BillingCode”,但只有创建资源的人才知道他们正确的帐单代码,因为每个人或项目都不同。
【问题讨论】:
标签: azure
有没有办法创建 Azure 策略,要求在创建资源时在资源上存在标签,但不检查特定值?我见过的所有例子都是“检查标签 X 是否存在,并且值 Y”。
我只想让用户知道“您需要在此资源上放置标签 X”,因为该值是用户定义的,因此我无法强制执行特定值。
例如 - 我在每个资源上都需要“BillingCode”,但只有创建资源的人才知道他们正确的帐单代码,因为每个人或项目都不同。
【问题讨论】:
标签: azure
您可以使用订阅策略来完成此操作。除非满足某些规则,否则它们将阻止部署 Azure 资源。
以下示例取自here。
您可以通过使用 notMatch 运算符而不是下面的直接匹配来修改此示例。 More operators here.
{
"properties": {
"displayName": "Enforce tag and its value on resource groups",
"description": "Enforces a required tag and its value on resource groups.",
"mode": "All",
"parameters": {
"tagName": {
"type": "String",
"metadata": {
"description": "Name of the tag, such as costCenter"
}
},
"tagValue": {
"type": "String",
"metadata": {
"description": "Value of the tag, such as headquarter"
}
}
},
"policyRule": {
"if": {
"allOf": [
{
"field": "type",
"equals": "Microsoft.Resources/subscriptions/resourceGroups"
},
{
"not": {
"field": "[concat('tags[',parameters('tagName'), ']')]",
"equals": "[parameters('tagValue')]"
}
}
]
},
"then": {
"effect": "deny"
}
}
}
}
【讨论】:
您需要exists operator。
例如:
{
"policyRule": {
"if": {
"field": "[concat('tags[',parameters('tagName'), ']')]",
"exists": "false"
},
"then": {
"effect": "deny"
}
},
"parameters": {
"tagName": {
"type": "String",
"metadata": {
"description": "Name of the tag, such as costCenter"
}
}
}
}
【讨论】:
tagValue,而不指定任何默认值?
tagValue(而不是tagName)尝试了你的方法,但是在Azure门户中创建资源组时,它不遵守规则。从技术上讲,tagValue是"",所以验证通过了。
not)。如果它仅不适用于资源组,请记住,您需要使用 "mode": "all" 将策略应用于资源组。