Azure 原則模式:標記

標記是管理、組織及治理 Azure 資源的重要部分。 Azure 原則可讓您使用修改效果和補救工作,大規模地在新資源和現有資源上設定標記。

範例 1:參數化標記

此原則定義會使用兩個參數 (tagNametagValue) 來設定原則指派要在資源群組上尋找的內容。 此格式可讓原則定義用於任何數目的標記名稱和標記值組合,但只會維護單一原則定義。

注意

雖然此原則定義模式與模式:參數 - 範例 #1 中的模式類似,但此範例會使用「所有」「模式」和目標資源群組。

{
    "properties": {
        "displayName": "Add or replace a tag on resource groups",
        "mode": "All",
        "description": "Adds or replaces the specified tag and value when any resource group is created or updated. Existing resource groups 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'"
                }
            },
            "tagValue": {
                "type": "String",
                "metadata": {
                    "displayName": "Tag Value",
                    "description": "Value of the tag, such as 'production'"
                }
            }
        },
        "policyRule": {
            "if": {
                "allOf": [{
                        "field": "type",
                        "equals": "Microsoft.Resources/subscriptions/resourceGroups"
                    },
                    {
                        "field": "[concat('tags[', parameters('tagName'), ']')]",
                        "notEquals": "[parameters('tagValue')]"
                    }
                ]
            },
            "then": {
                "effect": "modify",
                "details": {
                    "roleDefinitionIds": [
                        "/providers/microsoft.authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
                    ],
                    "operations": [{
                        "operation": "addOrReplace",
                        "field": "[concat('tags[', parameters('tagName'), ']')]",
                        "value": "[parameters('tagValue')]"
                    }]
                }
            }
        }
    }
}

範例 1:說明

"properties": {
    "displayName": "Add or replace a tag on resource groups",
    "mode": "All",
    "description": "Adds or replaces the specified tag and value when any resource group is created or updated. Existing resource groups can be remediated by triggering a remediation task.",
    "metadata": {
        "category": "Tags"
    },

在此範例中,模式會設定為 [所有],因為是以資源群組為目標。 在大部分情況下,使用標記時,模式應該設定為 [已編製索引]。 如需詳細資訊,請參閱模式

"if": {
    "allOf": [{
            "field": "type",
            "equals": "Microsoft.Resources/subscriptions/resourceGroups"
        },
        {
            "field": "[concat('tags[', parameters('tagName'), ']')]",
            "notEquals": "[parameters('tagValue')]"
        }
    ]
},

在原則定義的這個部分中,concat 會將參數化的 tagName 參數和 tags['name'] 格式結合,以指示欄位評估 tagValue 參數的標記。 使用 notEquals 時,如果 tags[tagName] 不等於 tagValue,則會觸發「修改」效果。

"operations": [{
    "operation": "addOrReplace",
    "field": "[concat('tags[', parameters('tagName'), ']')]",
    "value": "[parameters('tagValue')]"
}]

在這裡,addOrReplace 作業會使用相同格式來使用參數化標記值的,以在評估的資源群組上建立或更新所需的標記值。

範例 2:從資源群組繼承標記值

此原則定義會使用 tagName 參數來判斷要從父資源群組繼承哪一個標記的值。

{
    "properties": {
        "displayName": "Inherit a tag from the resource group",
        "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')]]"
                    }]
                }
            }
        }
    }
}

範例 2:說明

"properties": {
    "displayName": "Inherit a tag from the resource group",
    "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"
    },

在此範例中,模式會設定為 [已編製索引],因為其不是以資源群組或訂用帳戶為目標,即使是從資源群組取得值也是一樣。 如需詳細資訊,請參閱模式

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

policyRule.if會使用像 Sample #1 這類的 concat 來評估 tagName 的值,但會使用 resourceGroup() 函式將其與父資源群組上的相同標記值進行比較。 這裡的第二個子句會檢查資源群組上的標記是否具有值,且不是 Null。

"operations": [{
    "operation": "addOrReplace",
    "field": "[concat('tags[', parameters('tagName'), ']')]",
    "value": "[resourceGroup().tags[parameters('tagName')]]"
}]

在這裡,指派給資源上 tagName 標記的值也會使用 resourceGroup() 函式從父資源群組取得值。 如此一來,您就可以從父資源群組繼承標記。 如果您已經建立資源,但未新增標記,則此相同的原則定義和 補救工作可以更新現有資源。

下一步