Azure 原則 定義結構參數

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

新增或移除參數

參數可能會新增至現有和指派的定義。 新的參數必須包含 defaultValue 屬性。 這個屬性可防止原則或計劃的現有指派間接地失效。

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

參數屬性

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

  • name:參數的名稱。 由 parameters 原則規則內的部署函式使用。 如需詳細資訊,請參閱 使用參數值
  • type:判斷參數是否為 string、、array、、objectbooleanintegerfloatdateTime
  • metadata:定義 Azure 入口網站 主要用來顯示使用者易記資訊的子屬性:
    • description:參數用途的說明。 可用來提供可接受的值範例。
    • displayName:參數入口網站中顯示的易記名稱。
    • strongType:(選擇性) 透過入口網站指派原則定義時使用。 提供內容感知清單。 如需詳細資訊,請參閱 strongType
    • assignPermissions:(選擇性) 設定為 true,在原則指派期間 Azure 入口網站 建立角色指派。 如果您想要在指派範圍之外指派許可權,這個屬性就很有用。 原則中每個角色定義都有一個角色指派(或所有計劃原則中每個角色定義中的每個角色定義)。 參數值必須是有效的資源或範圍。
    • deprecated:布爾值旗標,指出參數是否在內建定義中已被取代。
  • defaultValue:(選擇性 如果未指定任何值,則會在指派中設定參數的值。 更新指派的現有原則定義時,為必要專案。 針對 oject 類型參數,值必須符合適當的架構。
  • allowedValues:(選擇性) 提供參數在指派期間接受的值陣列。
    • 區分大小寫:指派原則時允許的值比較會區分大小寫,這表示指派中選取的參數值必須符合定義中 allowedValues 陣列中的值大小寫。 不過,選取指派的值之後,根據 所使用的條件 ,對字串比較的評估可能會不區分大小寫。 例如,如果 參數在Dev指派中指定為允許的標籤,而且這個值會與使用 equals 條件的輸入字串進行比較,則 Azure 原則 稍後會評估的標籤值dev做為相符專案,即使它不區分大小寫,也notEquals一樣。
    • 針對物件類型參數,值必須符合適當的架構。
  • schema:(選擇性) 使用自我定義的 JSON 架構,在指派期間提供參數輸入的驗證。 此屬性僅支持物件類型參數,並遵循 架構 2019-09 實作 Json.NET。 您可以在 中深入瞭解如何使用架構 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

下一步