gpt4 book ai didi

Azure Policy 检查空值

转载 作者:行者123 更新时间:2023-12-03 01:00:24 25 4
gpt4 key购买 nike

我需要用于标记的 Azure 策略。我希望用户在创建资源组时需要定义标签。该策略还应检查 tagvaule 是否不为空。

我尝试过以下方法:

{
"properties": {
"displayName": "Require a tag Billto and a value that is not empty",
"policyType": "Custom",
"mode": "All",
"description": "Enforces a required tag and its value on resource groups.",
"metadata": {
"category": "Tags",
},
"parameters": {
"tagName": {
"type": "String",
"metadata": {
"displayName": "Tag Name",
"description": "Name of the tag, such as 'Billto'"
}
},
"tagValue": {
"type": "String",
"metadata": {
"displayName": "Tag Value",
"description": "Value of the tag, such as 'Costcenter'"
}
}
},
"policyRule": {
"if": {
"allOf": [
{
"field": "type",
"equals": "Microsoft.Resources/subscriptions/resourceGroups"
},
{
"field": "[concat('tags[', parameters('tagName'), ']')]",
"exists": "false"
},
{
"value": "[concat('tags[', parameters('tagValue'), ']')]",
"equals": ""
}
]
},
"then": {
"effect": "deny"
}
}
}

有人可以帮助我并给我正确的代码吗?谢谢托马斯

最佳答案

此策略定义将拒绝给定标记为空值或完全缺少标记的资源组:

{
"properties": {
"mode": "All",
"parameters": {
"tagName": {
"type": "String",
"metadata": {
"displayName": "Tag Name",
"description": "Name of the tag, such as 'Billto'"
}
}
},
"policyRule": {
"if": {
"allOf": [
{
"field": "type",
"equals": "Microsoft.Resources/subscriptions/resourceGroups"
},
{
"anyOf": [
{
"field": "[concat('tags[', parameters('tagName'), ']')]",
"exists": false
},
{
"field": "[concat('tags[', parameters('tagName'), ']')]",
"equals": ""
}
]
}
]
},
"then": {
"effect": "deny"
}
}
}
}

分解:

  1. parameters('tagName') 解析为参数 tagName 的值。对于本示例的其余部分,我们将使用 Billto 作为标记名称。
  2. "field": "[concat('tags[',parameters('tagName'), ']')]" 解析为 "field": "tags[Billto] “
  3. "field": "tags[Billto]" 将获取 Billto 标记的
  4. 如果资源没有 Billto 标记,则 Billto 标记不会有值,因此 "exists": false 将为 true,策略将拒绝。如果 Billto 标记的值为空,则 "equals": "" 将为 true,并且策略将拒绝。

关于Azure Policy 检查空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61356867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com