Share via


Azure 原則模式:參數

您可以使用參數將原則定義設定為動態,以減少所需的原則定義數目。 參數會在原則指派期間進行定義。 參數具有一組預先定義的屬性,用於描述參數和其使用方式。

範例 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 參數會定義為「字串」,並提供描述供其使用。

然後,policyRule.if 區塊中會使用該參數讓原則變成動態的。 在這裡,參數會用來定義評估的欄位,也就是具有 tagName 值的標記。

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

範例 2:陣列參數

此原則定義會使用單一參數 (listOfBandwidthinMbps) 來檢查 Express Route 線路資源是否已將頻寬設定設為其中一個已核准的值。 如果不相符,資源的建立或更新就會遭到拒絕

{
    "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 參數會定義為「陣列」,並提供描述供其使用。 「陣列」會有許多個要比對的值。

然後,在 policyRule.if 區塊中使用該參數。 因為是「陣列」參數,您必須使用「陣列」條件innotIn。 在這裡,該參數會針對 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')]"
}

下一步