Share via


Azure 原則模式:值運算子

運算子會針對指定條件的提供值評估參數支援的範本函式或常值。

警告

如果「範本函式」的結果為錯誤,則原則評估會失敗。 失敗的評估隱含著拒絕的意思。 如需詳細資訊,請參閱避免範本錯誤

原則定義範例

此原則定義會在資源上新增或取代參數 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": ""
        }
    ]
},

運算子會用於屬性中的 policyRule.if 區塊。 在此範例中,allOf邏輯運算子用來指出這兩個條件陳述式都必須為 true,才會發生 modify 效果。

會針對空值的 notEquals 條件,評估 resourceGroup() 範本函式的結果。 如果父資源群組上有 tagName 中提供的標記名稱,則條件會評估為 true。

下一步