Azure Policy 模式:value 运算符

value 运算符会对参数支持的模板函数或文本进行评估,针对为给定条件提供的值。

警告

如果模板函数的结果是一个错误,则策略评估会失败。 评估失败是一种隐式拒绝。 有关详细信息,请参阅避免模板失败

示例策略定义

此策略定义添加或替换在资源上的参数 tagName(字符串)中指定的标记,并从资源所在的资源组继承 tagName 的值。 创建或更新资源时,会进行此评估。 充当 modify 效果的修正可以通过修正任务在现有资源上运行。

{
    "properties": {
        "displayName": "Inherit a tag from the resource group",
        "policyType": "BuiltIn",
        "mode": "Indexed",
        "description": "Adds or replaces the specified tag and value from the parent resource group when any resource is created or updated. Existing resources can be remediated by triggering a remediation task.",
        "metadata": {
            "category": "Tags"
        },
        "parameters": {
            "tagName": {
                "type": "String",
                "metadata": {
                    "displayName": "Tag Name",
                    "description": "Name of the tag, such as 'environment'"
                }
            }
        },
        "policyRule": {
            "if": {
                "allOf": [{
                        "field": "[concat('tags[', parameters('tagName'), ']')]",
                        "notEquals": "[resourceGroup().tags[parameters('tagName')]]"
                    },
                    {
                        "value": "[resourceGroup().tags[parameters('tagName')]]",
                        "notEquals": ""
                    }
                ]
            },
            "then": {
                "effect": "modify",
                "details": {
                    "roleDefinitionIds": [
                        "/providers/microsoft.authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
                    ],
                    "operations": [{
                        "operation": "addOrReplace",
                        "field": "[concat('tags[', parameters('tagName'), ']')]",
                        "value": "[resourceGroup().tags[parameters('tagName')]]"
                    }]
                }
            }
        }
    }
}

说明

"if": {
    "allOf": [{
            "field": "[concat('tags[', parameters('tagName'), ']')]",
            "notEquals": "[resourceGroup().tags[parameters('tagName')]]"
        },
        {
            "value": "[resourceGroup().tags[parameters('tagName')]]",
            "notEquals": ""
        }
    ]
},

value 运算符用在属性policyRule.if 块中。 在此示例中,逻辑运算符allOf 用于说明这两个条件语句都必须为 true 才能产生 modify 效果。

value 会对模板函数 resourceGroup() 的结果进行评估,其条件是结果 notEquals 空值。 如果在父资源组的 tagName 中提供的标记名称存在,则条件的评估结果为 true。

后续步骤