分享方式:


Azure 原則定義結構參數

參數可減少原則定義數量來幫助您簡化原則管理。 試想參數是表單上的欄位:nameaddresscitystate。 這些參數一律保持不變,但其值會根據填寫表單的個人而有所變更。 在建立原則時,參數也是以相同的方式運作。 藉由在原則定義中納入參數,您便可以針對不同的案例使用不同的值,來重複使用該原則。

新增或移除參數

參數可能會新增至現有和指派的定義。 新的參數必須包含 defaultValue 屬性。 這個屬性可避免原則或計畫的現有指派間接地變成無效。

參數無法從原則定義中移除,因為可能有設定參數值的指派,而且該參考會中斷。 某些內建原則定義已取代使用中繼資料 "deprecated": true 的參數,這會在 Azure 入口網站中指派定義時隱藏參數。 雖然自訂原則定義不支援這個方法,但另一個選項是複製並建立新的自訂原則定義,而不使用參數。

參數屬性

參數會在原則定義中使用下列屬性:

  • name:參數的名稱。 由原則規則中的 parameters 部署函式使用。 如需詳細資訊,請參閱使用參數值
  • type:判斷參數是 stringarrayobjectbooleanintegerfloatdateTime
  • metadata: 定義主要由 Azure 入口網站使用的子屬性,以顯示使用者易讀的資訊:
    • description: 參數用途的說明。 能用來提供可接受值的範例。
    • displayName: 參數在入口網站中顯示的自訂名稱。
    • strongType: (選擇性) 透過入口網站指派原則定義時會使用。 提供內容感知清單。 如需詳細資訊,請參閱 strongType
    • assignPermissions: (選擇性) 設定為 True,讓 Azure 入口網站在原則指派期間建立角色指派。 如果您想要在指派範圍之外指派權限,此屬性會很有用。 原則中每個角色定義 (或所有方案原則中的每個角色定義) 都有一個角色指派。 參數值必須是有效的資源或範圍。
    • deprecated: 布林值旗標,指出參數是否在內建定義中已被取代。
  • defaultValue: (選擇性) 如果沒有提供值,就在指派中設定參數的值。 更新已指派的現有原則定義時需要。 oject 類型參數的值須符合適當的結構描述。
  • allowedValues: (選擇性) 提供參數在指派期間所接受的值陣列。
    • 區分大小寫: 指派原則時允許的值比較會區分大小寫,這表示指派中選取的參數值必須符合定義中 allowedValues 陣列中的值大小寫。 不過,選取指派的值之後,根據所使用的條件,字元串比較的評估可能會不區分大小寫。 例如,如果參數將 Dev 指定為指派中允許的標籤值,而且這個值會與使用 equals 條件的輸入字串進行比較,Azure 原則稍後會將標記值 dev 評估為相符項目,即使其為小寫字母,因為 notEquals 不區分大小寫。
    • 物件類型參數的值必須符合適當的結構描述。
  • schema:(選擇性) 使用自行定義的 JSON 結構描述在指派期間提供參數輸入的驗證。 此屬性僅支援物件類型參數,並遵循 Json.NET 結構描述 2019-09 實作。 您可在 https://json-schema.org/ 中深入了解如何使用結構描述,並在 https://www.jsonschemavalidator.net/ 中測試草稿結構描述。

範例參數

範例 1

舉例來說,您可以定義一個原則定義來限制可部署資源的位置。 該原則定義的參數可為 allowedLocations,且每個原則定義的指派都會使用參數來限制接受的值。 透過入口網站完成指派時,strongType 提供增強的體驗:

"parameters": {
  "allowedLocations": {
    "type": "array",
    "metadata": {
      "description": "The list of allowed locations for resources.",
      "displayName": "Allowed locations",
      "strongType": "location"
    },
    "defaultValue": [
      "westus2"
    ],
    "allowedValues": [
      "eastus2",
      "westus2",
      "westus"
    ]
  }
}

指派此陣列類型參數 (無 strongType) 時的範例輸入可為 ["westus", "eastus2"]

範例 2

在進一步案例中,您可定義原則,要求 Kubernetes 叢集 Pod 使用指定標籤。 該原則定義的參數可為 labelSelector,且原則定義的每項指派皆使用此參數,根據標籤索引鍵和值指定有問題的 Kubernetes 資源:

"parameters": {
  "labelSelector": {
    "type": "Object",
    "metadata": {
      "displayName": "Kubernetes label selector",
      "description": "Label query to select Kubernetes resources for policy evaluation. An empty label selector matches all Kubernetes resources."
    },
    "defaultValue": {},
    "schema": {
      "description": "A label selector is a label query over a set of resources. The result of matchLabels and matchExpressions are ANDed. An empty label selector matches all resources.",
      "type": "object",
      "properties": {
        "matchLabels": {
          "description": "matchLabels is a map of {key,value} pairs.",
          "type": "object",
          "additionalProperties": {
            "type": "string"
          },
          "minProperties": 1
        },
        "matchExpressions": {
          "description": "matchExpressions is a list of values, a key, and an operator.",
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "key": {
                "description": "key is the label key that the selector applies to.",
                "type": "string"
              },
              "operator": {
                "description": "operator represents a key's relationship to a set of values.",
                "type": "string",
                "enum": [
                  "In",
                  "NotIn",
                  "Exists",
                  "DoesNotExist"
                ]
              },
              "values": {
                "description": "values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. If the operator is Exists or DoesNotExist, the values array must be empty.",
                "type": "array",
                "items": {
                  "type": "string"
                }
              }
            },
            "required": [
              "key",
              "operator"
            ],
            "additionalProperties": false
          },
          "minItems": 1
        }
      },
      "additionalProperties": false
    }
  },
}

指派此物件類型參數時的範例輸入為 JSON 格式 (由指定的結構描述驗證),且可能為:

{
  "matchLabels": {
    "poolID": "abc123",
    "nodeGroup": "Group1",
    "region": "southcentralus"
  },
  "matchExpressions": [
    {
      "key": "name",
      "operator": "In",
      "values": [
        "payroll",
        "web"
      ]
    },
    {
      "key": "environment",
      "operator": "NotIn",
      "values": [
        "dev"
      ]
    }
  ]
}

使用參數值

在原則規則中,您可以使用下列 parameters 函式語法參考參數:

{
  "field": "location",
  "in": "[parameters('allowedLocations')]"
}

此範例參考參數屬性中示範的 allowedLocations 參數。

strongType

metadata 屬性中,您可以使用 strongType 在 Azure 入口網站中提供多重選取的選項清單。 strongType 可以是支援的資源類型或允許的值。 若要判斷資源類型是否適用於 strongType,請使用 Get-AzResourceProvider資源類型 strongType 的格式為 <Resource Provider>/<Resource Type>。 例如: Microsoft.Network/virtualNetworks/subnets

支援部分不是由 Get-AzResourceProvider 傳回的資源類型。 這些類型如下所示:

  • Microsoft.RecoveryServices/vaults/backupPolicies

strongType 的非資源類型允許值為:

  • location
  • resourceTypes
  • storageSkus
  • vmSKUs
  • existingResourceGroups

下一步