Azure Policy 패턴: 효과

Azure Policy에는 서비스에서 비준수 리소스에 반응하는 방법을 결정하는 다양한 효과가 있습니다. 일부 효과는 간단하며 정책 정의에 추가 속성이 필요하지 않지만 다른 효과에는 여러 가지 속성이 필요합니다.

샘플 1: 단순 효과

이 정책 정의는 매개 변수 tagName에 정의된 태그가 평가된 리소스에 있는지 여부를 확인합니다. 태그가 아직 없는 경우 modify 효과가 트리거되고 매개 변수 tagValue에 값이 포함된 태그를 추가합니다.

{
    "properties": {
        "displayName": "Add a tag to resource groups",
        "policyType": "BuiltIn",
        "mode": "All",
        "description": "Adds the specified tag and value when any resource group missing this tag is created or updated. Existing resource groups can be remediated by triggering a remediation task. If the tag exists with a different value it will not be changed.",
        "metadata": {
            "version": "1.0.0",
            "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'), ']')]",
                        "exists": "false"
                    }
                ]
            },
            "then": {
                "effect": "modify",
                "details": {
                    "roleDefinitionIds": [
                        "/providers/microsoft.authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
                    ],
                    "operations": [{
                        "operation": "add",
                        "field": "[concat('tags[', parameters('tagName'), ']')]",
                        "value": "[parameters('tagValue')]"
                    }]
                }
            }
        }
    }
}

샘플 1: 설명

"effect": "modify",
"details": {
    "roleDefinitionIds": [
        "/providers/microsoft.authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
    ],
    "operations": [{
        "operation": "add",
        "field": "[concat('tags[', parameters('tagName'), ']')]",
        "value": "[parameters('tagValue')]"
    }]
}

modify 효과를 적용하려면 roleDefinitionIdsoperations를 정의하는 policyRule.then.details 블록이 필요합니다. 이러한 매개 변수는 태그를 추가하고 리소스를 수정하는 데 필요한 역할과 사용할 수정 작업을 Azure Policy에 알려줍니다. 이 예제에서는 추가작업 및 매개 변수를 사용하여 태그와 해당 값을 설정합니다.

샘플 2: 복합 효과

이 정책 정의는 매개 변수 publishertype에 정의된 확장이 없을 때 각 가상 머신을 감사합니다. auditIfNotExists를 사용하여 가상 머신과 관련된 리소스를 확인하고 정의된 매개 변수와 일치하는 인스턴스가 있는지 여부를 확인합니다. 이 예제에서는 extensions 형식을 확인합니다.

{
    "type": "Microsoft.Authorization/policyDefinitions",
    "name": "audit-vm-extension",
    "properties": {
        "displayName": "Audit if extension does not exist",
        "description": "This policy audits if a required extension doesn't exist.",
        "parameters": {
            "publisher": {
                "type": "String",
                "metadata": {
                    "description": "The publisher of the extension",
                    "displayName": "Extension Publisher"
                }
            },
            "type": {
                "type": "String",
                "metadata": {
                    "description": "The type of the extension",
                    "displayName": "Extension Type"
                }
            }
        },
        "policyRule": {
            "if": {
                "allOf": [{
                        "field": "type",
                        "equals": "Microsoft.Compute/virtualMachines"
                    },
                    {
                        "field": "Microsoft.Compute/imagePublisher",
                        "in": [
                            "MicrosoftWindowsServer"
                        ]
                    },
                    {
                        "field": "Microsoft.Compute/imageOffer",
                        "in": [
                            "WindowsServer"
                        ]
                    }
                ]
            },
            "then": {
                "effect": "auditIfNotExists",
                "details": {
                    "type": "Microsoft.Compute/virtualMachines/extensions",
                    "existenceCondition": {
                        "allOf": [{
                                "field": "Microsoft.Compute/virtualMachines/extensions/publisher",
                                "equals": "[parameters('publisher')]"
                            },
                            {
                                "field": "Microsoft.Compute/virtualMachines/extensions/type",
                                "equals": "[parameters('type')]"
                            }
                        ]
                    }
                }
            }
        }
    }
}

샘플 2: 설명

"details": {
    "type": "Microsoft.Compute/virtualMachines/extensions",
    "existenceCondition": {
        "allOf": [{
                "field": "Microsoft.Compute/virtualMachines/extensions/publisher",
                "equals": "[parameters('publisher')]"
            },
            {
                "field": "Microsoft.Compute/virtualMachines/extensions/type",
                "equals": "[parameters('type')]"
            }
        ]
    }
}

auditIfNotExists 효과를 적용하려면 policyRule.then.details 블록에서 찾을 typeexistenceCondition 모두를 정의해야 합니다. existenceCondition논리 연산자와 같은 정책 언어 요소를 사용하여 일치하는 관련 리소스가 있는지 여부를 확인합니다. 이 예제서는 각 별칭에 대해 확인된 값이 매개 변수에 정의됩니다.

다음 단계