Azure Policy 패턴: 태그

태그는 Azure 리소스를 관리, 구성 및 관리하는 데 중요한 부분입니다. Azure Policy를 사용하면 수정 효과와 조정 작업을 통해 새 리소스와 기존 리소스에 대한 태그를 대규모로 구성할 수 있습니다.

샘플 1: 태그 매개 변수화

이 정책 정의는 두 개의 매개 변수, 즉, tagNametagValue를 사용하여 정책 할당이 리소스 그룹에서 찾고 있는 항목을 설정합니다. 이 형식을 사용하면 원하는 수의 태그 이름 및 태그 값 조합에 정책 정의를 사용할 수 있지만 단일 정책 정의만 유지 관리합니다.

참고 항목

이 정책 정의 패턴은 패턴: 매개 변수 - 샘플 #1의 패턴과 비슷합니다. 이 샘플은 modeAll을 사용하며 리소스 그룹을 대상으로 합니다.

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

이 샘플에서는 mode가 리소스 그룹을 대상으로 하기 때문에 All로 설정됩니다. 대부분의 경우 태그를 사용할 때 modeIndexed로 설정해야 합니다. 자세한 내용은 mode를 참조하세요.

"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와 일치하지 않으면 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"
    },

이 샘플에서 mode는 리소스 그룹에서 값을 가져오지만 리소스 그룹 또는 구독을 대상으로 하지 않으므로 Indexed로 설정됩니다. 자세한 내용은 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() 함수를 사용하여 부모 리소스 그룹에 있는 동일한 태그의 값과 비교합니다. 두 번째 절은 리소스 그룹의 태그가 값을 포함하고 null이 아닌지 확인합니다.

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

여기에서 리소스의 tagName 태그에 할당되는 값도 resourceGroup() 함수를 사용하여 부모 리소스 그룹에서 값을 가져옵니다. 이러한 방식으로 부모 리소스 그룹에서 태그를 상속할 수 있습니다. 이미 리소스를 만들었지만 태그를 추가하지 않은 경우 이 정책 정의와 수정 작업으로 기존 리소스를 업데이트할 수 있습니다.

다음 단계