Azure Policy 패턴: 매개 변수

매개 변수를 사용하여 필요한 정책 정의의 수를 줄이기 위해 정책 정의를 동적으로 만들 수 있습니다. 매개 변수는 정책 할당 중에 정의됩니다. 매개 변수에는 매개 변수 및 사용 방법을 설명하는 미리 정의된 속성 세트가 있습니다.

샘플 1: 문자열 매개 변수

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

참고 항목

모드모두를 사용하고 리소스 그룹과 작동하는 태그 샘플은 패턴: 태그 - 샘플 #1을 참조하세요.

{
    "properties": {
        "displayName": "Require tag and its value",
        "policyType": "BuiltIn",
        "mode": "Indexed",
        "description": "Enforces a required tag and its value. Does not apply to resource groups.",
        "parameters": {
            "tagName": {
                "type": "String",
                "metadata": {
                    "description": "Name of the tag, such as costCenter"
                }
            },
            "tagValue": {
                "type": "String",
                "metadata": {
                    "description": "Value of the tag, such as headquarter"
                }
            }
        },
        "policyRule": {
            "if": {
                "not": {
                    "field": "[concat('tags[', parameters('tagName'), ']')]",
                    "equals": "[parameters('tagValue')]"
                }
            },
            "then": {
                "effect": "deny"
            }
        }
    }
}

샘플 1: 설명

"tagName": {
    "type": "String",
    "metadata": {
        "description": "Name of the tag, such as costCenter"
    }
},

이 정책 정의의 부분에서 tagName 매개 변수는 string으로 정의되며 사용할 수 있도록 설명이 제공됩니다.

그런 다음, policyRule.if 블록에서 매개 변수를 사용하여 정책을 동적으로 만듭니다. 여기에서 계산되는 필드를 정의하는 데 사용됩니다. 이는 tagName값이 포함된 태그입니다.

"if": {
    "not": {
        "field": "[concat('tags[', parameters('tagName'), ']')]",
        "equals": "[parameters('tagValue')]"
    }
},

샘플 2: Array 매개 변수

이 정책 정의는 단일 매개 변수인 listOfBandwidthinMbps를 사용하여 Express 경로 회로 리소스가 승인된 값 중 하나로 대역폭 설정을 구성했는지 확인합니다. 일치하지 않는 경우 리소스에 대한 만들기 또는 업데이트가 거부됩니다.

{
    "properties": {
        "displayName": "Allowed Express Route bandwidth",
        "description": "This policy enables you to specify a set of express route bandwidths that your organization can deploy.",
        "parameters": {
            "listOfBandwidthinMbps": {
                "type": "Array",
                "metadata": {
                    "description": "The list of SKUs that can be specified for express route.",
                    "displayName": "Allowed Bandwidth"
                }
            }
        },
        "policyRule": {
            "if": {
                "allOf": [{
                        "field": "type",
                        "equals": "Microsoft.Network/expressRouteCircuits"
                    },
                    {
                        "not": {
                            "field": "Microsoft.Network/expressRouteCircuits/serviceProvider.bandwidthInMbps",
                            "in": "[parameters('listOfBandwidthinMbps')]"
                        }
                    }
                ]
            },
            "then": {
                "effect": "Deny"
            }
        }
    }
}

샘플 2: 설명

"listOfBandwidthinMbps": {
    "type": "Array",
    "metadata": {
        "description": "The list of SKUs that can be specified for express route.",
        "displayName": "Allowed Bandwidth"
    }
}

이 정책 정의의 부분에서 listOfBandwidthinMbps 매개 변수는 array로 정의되며 사용할 수 있도록 설명이 제공됩니다. array로 일치하는 값이 여러 개 있습니다.

그런 다음, policyRule.if 블록에서 매개 변수를 사용합니다. array 매개 변수로 arrayconditionin 또는 notIn이 사용되어야 합니다. 여기에서 정의된 값 중 하나로 serviceProvider.bandwidthInMbps 별칭에 대해 사용됩니다.

"not": {
    "field": "Microsoft.Network/expressRouteCircuits/serviceProvider.bandwidthInMbps",
    "in": "[parameters('listOfBandwidthinMbps')]"
}

샘플 3: 매개 변수화된 효과

정책 정의를 다시 사용할 수 있도록 하는 일반적인 방법은 효과 자체를 매개 변수화하는 것입니다. 이 예제에서는 단일 매개 변수 효과를 사용합니다. 효과를 매개 변수화하면 다른 효과를 가진 범위에 동일한 정의를 할당할 수 있습니다.

{
    "properties": {
        "displayName": "All authorization rules except RootManageSharedAccessKey should be removed from Service Bus namespace",
        "policyType": "BuiltIn",
        "mode": "All",
        "description": "Service Bus clients should not use a namespace level access policy that provides access to all queues and topics in a namespace. To align with the least privilege security model, you should create access policies at the entity level for queues and topics to provide access to only the specific entity",
        "metadata": {
            "version": "1.0.1",
            "category": "Service Bus"
        },
        "parameters": {
            "effect": {
                "type": "string",
                "defaultValue": "Audit",
                "allowedValues": [
                    "Audit",
                    "Deny",
                    "Disabled"
                ],
                "metadata": {
                    "displayName": "Effect",
                    "description": "The effect determines what happens when the policy rule is evaluated to match"
                }
            }
        },
        "policyRule": {
            "if": {
                "allOf": [{
                        "field": "type",
                        "equals": "Microsoft.ServiceBus/namespaces/authorizationRules"
                    },
                    {
                        "field": "name",
                        "notEquals": "RootManageSharedAccessKey"
                    }
                ]
            },
            "then": {
                "effect": "[parameters('effect')]"
            }
        }
    }
}

샘플 3: 설명

"parameters": {
    "effect": {
        "type": "string",
        "defaultValue": "Audit",
        "allowedValues": [
            "Audit",
            "Deny",
            "Disabled"
        ],
        "metadata": {
            "displayName": "Effect",
            "description": "The effect determines what happens when the policy rule is evaluated to match"
        }
    }
},

정책 정의의 이 부분에서 효과 매개 변수는 문자열로 정의됩니다. 정책 정의는 할당의 기본값을 감사로 설정하고 다른 옵션을 사용 안함거부로 제한합니다.

그런 다음, 효과에 대한 policyRule.then 블록에서 매개 변수를 사용합니다.

"then": {
    "effect": "[parameters('effect')]"
}

다음 단계