You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Azure Policy中添加动态标签并将资源类型设为标签值?

Azure 资源标签自动配置策略(新增resource_class标签)

以下是调整后的完整策略,已新增resource_class标签的自动赋值逻辑,默认从资源的type属性按/拆分取最后一段作为标签值,可按需调整拆分规则:

{
    "mode": "Indexed",
    "policyRule": {
        "if": {
            "anyOf": [
                {
                    "field": "tags[division]",
                    "exists": "false"
                },
                {
                    "field": "tags[division_code]",
                    "exists": "false"
                },
                {
                    "field": "tags[resource_class]",
                    "exists": "false"
                }
            ]
        },
        "then": {
            "effect": "append",
            "details": [
                {
                    "field": "tags[division]",
                    "value": "[tolower(parameters('division'))]"
                },
                {
                    "field": "tags[division_code]",
                    "value": "[tolower(parameters('division_code'))]"
                },
                {
                    "field": "tags[resource_class]",
                    "value": "[last(split(field('type'), '/'))]"
                }
            ]
        }
    },
    "parameters": {
        "division": {
            "type": "String",
            "metadata": {
                "displayName": "division",
                "description": "Value of the tag, such as 'production'"
            }
        },
        "division_code": {
            "type": "String",
            "metadata": {
                "displayName": "division_code",
                "description": "Value of the tag, such as '1234'"
            }
        }
    }
}

自定义取值规则说明

如果需要调整resource_class的生成规则,可修改对应value字段的表达式:

  • 如需统一转小写:将值替换为[tolower(last(split(field('type'), '/')))]
  • 如需取资源类型的前缀部分:将值替换为[split(field('type'), '/')[0]](例如Microsoft.Compute/virtualMachines会得到Microsoft.Compute)
  • 如需取固定位置的拆分片段:调整split返回数组的索引即可

内容的提问来源于stack exchange,提问作者Chirag

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 05:00:02