Azure Policy パターン: タグ

タグは、ご利用の Azure リソースを管理、整理、および統括する上で重要なパーツです。 Azure Policy では、modify 効果と修復タスクを使用して、新規および既存のリソース上でタグを広範に構成することができます。

サンプル 1:タグのパラメーター化

このポリシー定義では、tagNametagValue という 2 つのパラメーターを使用して、ポリシー割り当てによってリソース グループで検索される内容を設定します。 この形式により、ポリシー定義で任意の数のタグ名とタグ値の組み合わせを使用できるようになりますが、1 つのポリシー定義のみを維持してください。

Note

このポリシー定義パターンは、パターン: パラメーター - サンプル #1 に示したものと似ていますが、このサンプルでは mode として All を使用し、リソース グループをターゲットとします。

{
    "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"
    },

このサンプルでは、リソース グループをターゲットとしているため、modeAll に設定されます。 ほとんどの場合、タグを操作するときは、modeIndexed に設定する必要があります。 詳細については、mode に関するページを参照してください。

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

ポリシー定義のこの部分では、concat を使用して、パラメーター化された tagName パラメーターと tags['name'] 形式を組み合わせることで、field に、そのタグのパラメーター tagValue を評価するように指示します。 notEquals が使用されているため、tags[tagName]tagValue と等しくない場合、modify 効果がトリガーされます。

"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"
    },

このサンプルでは、リソース グループから値を取得する場合でもリソース グループまたはサブスクリプションをターゲットとしないため、modeIndexed に設定されます。 詳細については、mode に関するページを参照してください。

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

policyRule.if では、サンプル #1 のような concat を使用して tagName の値が評価されますが、それを、親リソース グループ上の同じタグの値と比較する場合は resourceGroup() 関数が使用されます。 ここの 2 番目の句では、リソース グループのタグが値を持ち、null ではないことを確認します。

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

ここでは、リソースの tagName タグに割り当てられている値でも、親リソース グループから値を取得するために、resourceGroup() 関数が使用されます。 このようにして、親リソース グループからタグを継承することができます。 リソースを既に作成していているが、まだタグを追加していない場合は、これと同じポリシー定義と修復タスクで既存のリソースを更新することができます。

次のステップ