Bicep 資源定義
policyAssignments 資源類型可以使用目標作業來部署:
如需每個 API 版本中已變更屬性的清單,請參閱 變更記錄檔。
資源格式
若要建立 Microsoft.Authorization/policyAssignments 資源,請將下列 Bicep 新增至範本。
resource symbolicname 'Microsoft.Authorization/policyAssignments@2025-03-01' = {
scope: resourceSymbolicName or scope
identity: {
type: 'string'
userAssignedIdentities: {
{customized property}: {}
}
}
location: 'string'
name: 'string'
properties: {
assignmentType: 'string'
definitionVersion: 'string'
description: 'string'
displayName: 'string'
enforcementMode: 'string'
metadata: any(...)
nonComplianceMessages: [
{
message: 'string'
policyDefinitionReferenceId: 'string'
}
]
notScopes: [
'string'
]
overrides: [
{
kind: 'string'
selectors: [
{
in: [
'string'
]
kind: 'string'
notIn: [
'string'
]
}
]
value: 'string'
}
]
parameters: {
{customized property}: {
value: any(...)
}
}
policyDefinitionId: 'string'
resourceSelectors: [
{
name: 'string'
selectors: [
{
in: [
'string'
]
kind: 'string'
notIn: [
'string'
]
}
]
}
]
}
}
屬性值
Microsoft.Authorization/policyAssignments
| 名字 | 描述 | 價值 |
|---|---|---|
| 身分識別 | 與原則指派相關聯的受控識別。 | 身分識別 |
| 位置 | 原則指派的位置。 只有在使用受控識別時才需要。 | 字串 |
| 名字 | 資源名稱 | 字串 約束: 模式 = ^[^<>*%&:\?.+/]*[^<>*%&:\?.+/ ]+$ (必要) |
| 性能 | 原則指派的屬性。 | PolicyAssignmentProperties |
| 範圍 | 在與部署範圍不同的範圍內建立資源時,請使用 。 | 將此屬性設定為資源的符號名稱,以套用 擴充資源。 |
身份
| 名字 | 描述 | 價值 |
|---|---|---|
| 型別 | 識別類型。 將系統或使用者指派的身分識別新增至資源時,這是唯一的必要字段。 | “無” “系統分配” 'UserAssigned' |
| 使用者指派的身份 | 與原則相關聯的使用者身分識別。 使用者身分識別字典索引鍵參考的格式為 ARM 資源標識符:'/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ManagedIdentity/userAssignedIdentities/{identityName}'。 | IdentityUserAssignedIdentities |
IdentityUserAssignedIdentities
| 名字 | 描述 | 價值 |
|---|
不合規消息
| 名字 | 描述 | 價值 |
|---|---|---|
| 訊息 | 描述資源不符合原則之原因的訊息。 這會顯示在「拒絕」錯誤訊息和資源不符合規範的合規性結果中。 | 字串 (必要) |
| policyDefinitionReferenceId | 原則集合定義中訊息的目標原則定義參考標識符。 這隻適用於原則指派指派原則集定義時。 如果未提供此訊息,訊息會套用至此原則指派指派的所有原則。 | 字串 |
覆蓋
| 名字 | 描述 | 價值 |
|---|---|---|
| 類 | 覆寫種類。 | 'definitionVersion' 'policyEffect' |
| 選擇 | 選取器表達式的清單。 | 選取器[] |
| 價值 | 要覆寫原則屬性的值。 | 字串 |
參數值
| 名字 | 描述 | 價值 |
|---|
參數值值
| 名字 | 描述 | 價值 |
|---|---|---|
| 價值 | 參數的值。 | 任何 |
PolicyAssignmentProperties
| 名字 | 描述 | 價值 |
|---|---|---|
| assignmentType | 原則指派的類型。 可能的值為 NotSpecified、System、SystemHidden 和 Custom。 變。 | “自定義” “未指定” '系統' “SystemHidden” |
| 定義版本 | 要使用的原則定義版本。 | 字串 |
| 描述 | 如果違反原則,此訊息將會是回應的一部分。 | 字串 |
| 顯示名稱 | 原則指派的顯示名稱。 | 字串 |
| enforcementMode | 原則指派強制模式。 可能的值為 Default、DoNotEnforce 和 Enroll | 預設 “不強制執行” “註冊” |
| 中繼資料 | 原則指派元數據。 元數據是開放式物件,通常是索引鍵值組的集合。 | 任何 |
| nonComplianceMessages | 描述資源不符合原則之原因的訊息。 | NoncomplianceMessage[] |
| notScopes | 原則的排除範圍。 | 字串[] |
| 重寫 | 原則屬性值覆寫。 | 覆寫[] |
| 參數 | 指派原則規則的參數值。 索引鍵是參數名稱。 | 參數值 |
| policyDefinitionId | 要指派的原則定義或原則集定義的標識碼。 | 字串 |
| resourceSelectors | 依資源屬性篩選原則的資源選取器清單。 | 資源選擇器 |
資源選擇器
| 名字 | 描述 | 價值 |
|---|---|---|
| 名字 | 資源選取器的名稱。 | 字串 |
| 選擇 | 選取器表達式的清單。 | 選取器[] |
選擇器
| 名字 | 描述 | 價值 |
|---|---|---|
| 在 | 要篩選的值清單。 | 字串[] |
| 類 | 選取器種類。 | “policyDefinitionReferenceId” 'resourceLocation' 'resourceType' 'resourceWithoutLocation' |
| notIn | 要篩選掉的值清單。 | 字串[] |
UserAssignedIdentitiesValue
| 名字 | 描述 | 價值 |
|---|
使用範例
Azure 快速入門範例
下列 Azure 快速入門範本 包含用於部署此資源類型的 Bicep 範例。
| Bicep 檔案 | 描述 |
|---|---|
| 指派內建原則以稽核 VM 受控磁碟 | 此範本會將內建原則指派給資源群組範圍,以稽核虛擬機 (VM) 受控磁碟。 |
| 建立 Azure 虛擬網路管理員和範例 VNET | 此範本會將 Azure 虛擬網路管理員和範例虛擬網路部署到具名資源群組中。 它支援多個連線拓撲和網路群組成員資格類型。 |
| 部署原則 def 並指派給多個 Mgmt 群組 | 此範本是管理群組層級範本,會建立原則定義,並將該原則指派給多個管理群組。 |
| 部署原則定義,並指派給管理群組 | 此範本是管理群組層級範本,會建立原則定義,並將該原則指派給目標管理群組。 目前無法透過 Azure 入口網站部署此範本。 |
ARM 樣本資源定義
policyAssignments 資源類型可以使用目標作業來部署:
如需每個 API 版本中已變更屬性的清單,請參閱 變更記錄檔。
資源格式
若要建立 Microsoft.Authorization/policyAssignments 資源,請將下列 JSON 新增至範本。
{
"type": "Microsoft.Authorization/policyAssignments",
"apiVersion": "2025-03-01",
"name": "string",
"identity": {
"type": "string",
"userAssignedIdentities": {
"{customized property}": {
}
}
},
"location": "string",
"properties": {
"assignmentType": "string",
"definitionVersion": "string",
"description": "string",
"displayName": "string",
"enforcementMode": "string",
"metadata": {},
"nonComplianceMessages": [
{
"message": "string",
"policyDefinitionReferenceId": "string"
}
],
"notScopes": [ "string" ],
"overrides": [
{
"kind": "string",
"selectors": [
{
"in": [ "string" ],
"kind": "string",
"notIn": [ "string" ]
}
],
"value": "string"
}
],
"parameters": {
"{customized property}": {
"value": {}
}
},
"policyDefinitionId": "string",
"resourceSelectors": [
{
"name": "string",
"selectors": [
{
"in": [ "string" ],
"kind": "string",
"notIn": [ "string" ]
}
]
}
]
}
}
屬性值
Microsoft.Authorization/policyAssignments
| 名字 | 描述 | 價值 |
|---|---|---|
| apiVersion | API 版本 | '2025-03-01' |
| 身分識別 | 與原則指派相關聯的受控識別。 | 身分識別 |
| 位置 | 原則指派的位置。 只有在使用受控識別時才需要。 | 字串 |
| 名字 | 資源名稱 | 字串 約束: 模式 = ^[^<>*%&:\?.+/]*[^<>*%&:\?.+/ ]+$ (必要) |
| 性能 | 原則指派的屬性。 | PolicyAssignmentProperties |
| 型別 | 資源類型 | “Microsoft.Authorization/policyAssignments” |
身份
| 名字 | 描述 | 價值 |
|---|---|---|
| 型別 | 識別類型。 將系統或使用者指派的身分識別新增至資源時,這是唯一的必要字段。 | “無” “系統分配” 'UserAssigned' |
| 使用者指派的身份 | 與原則相關聯的使用者身分識別。 使用者身分識別字典索引鍵參考的格式為 ARM 資源標識符:'/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ManagedIdentity/userAssignedIdentities/{identityName}'。 | IdentityUserAssignedIdentities |
IdentityUserAssignedIdentities
| 名字 | 描述 | 價值 |
|---|
不合規消息
| 名字 | 描述 | 價值 |
|---|---|---|
| 訊息 | 描述資源不符合原則之原因的訊息。 這會顯示在「拒絕」錯誤訊息和資源不符合規範的合規性結果中。 | 字串 (必要) |
| policyDefinitionReferenceId | 原則集合定義中訊息的目標原則定義參考標識符。 這隻適用於原則指派指派原則集定義時。 如果未提供此訊息,訊息會套用至此原則指派指派的所有原則。 | 字串 |
覆蓋
| 名字 | 描述 | 價值 |
|---|---|---|
| 類 | 覆寫種類。 | 'definitionVersion' 'policyEffect' |
| 選擇 | 選取器表達式的清單。 | 選取器[] |
| 價值 | 要覆寫原則屬性的值。 | 字串 |
參數值
| 名字 | 描述 | 價值 |
|---|
參數值值
| 名字 | 描述 | 價值 |
|---|---|---|
| 價值 | 參數的值。 | 任何 |
PolicyAssignmentProperties
| 名字 | 描述 | 價值 |
|---|---|---|
| assignmentType | 原則指派的類型。 可能的值為 NotSpecified、System、SystemHidden 和 Custom。 變。 | “自定義” “未指定” '系統' “SystemHidden” |
| 定義版本 | 要使用的原則定義版本。 | 字串 |
| 描述 | 如果違反原則,此訊息將會是回應的一部分。 | 字串 |
| 顯示名稱 | 原則指派的顯示名稱。 | 字串 |
| enforcementMode | 原則指派強制模式。 可能的值為 Default、DoNotEnforce 和 Enroll | 預設 “不強制執行” “註冊” |
| 中繼資料 | 原則指派元數據。 元數據是開放式物件,通常是索引鍵值組的集合。 | 任何 |
| nonComplianceMessages | 描述資源不符合原則之原因的訊息。 | NoncomplianceMessage[] |
| notScopes | 原則的排除範圍。 | 字串[] |
| 重寫 | 原則屬性值覆寫。 | 覆寫[] |
| 參數 | 指派原則規則的參數值。 索引鍵是參數名稱。 | 參數值 |
| policyDefinitionId | 要指派的原則定義或原則集定義的標識碼。 | 字串 |
| resourceSelectors | 依資源屬性篩選原則的資源選取器清單。 | 資源選擇器 |
資源選擇器
| 名字 | 描述 | 價值 |
|---|---|---|
| 名字 | 資源選取器的名稱。 | 字串 |
| 選擇 | 選取器表達式的清單。 | 選取器[] |
選擇器
| 名字 | 描述 | 價值 |
|---|---|---|
| 在 | 要篩選的值清單。 | 字串[] |
| 類 | 選取器種類。 | “policyDefinitionReferenceId” 'resourceLocation' 'resourceType' 'resourceWithoutLocation' |
| notIn | 要篩選掉的值清單。 | 字串[] |
UserAssignedIdentitiesValue
| 名字 | 描述 | 價值 |
|---|
使用範例
Azure 快速入門範本
下列 Azure 快速入門範本 部署此資源類型。
| 範本 | 描述 |
|---|---|
|
將內建原則指派給現有的資源群組 |
此範本會將內建原則指派給現有的資源群組。 |
|
指派內建原則以稽核 VM 受控磁碟 |
此範本會將內建原則指派給資源群組範圍,以稽核虛擬機 (VM) 受控磁碟。 |
|
建立 Azure 虛擬網路管理員和範例 VNET |
此範本會將 Azure 虛擬網路管理員和範例虛擬網路部署到具名資源群組中。 它支援多個連線拓撲和網路群組成員資格類型。 |
|
部署原則 def 並指派給多個 Mgmt 群組 |
此範本是管理群組層級範本,會建立原則定義,並將該原則指派給多個管理群組。 |
|
部署原則定義,並指派給管理群組 |
此範本是管理群組層級範本,會建立原則定義,並將該原則指派給目標管理群組。 目前無法透過 Azure 入口網站部署此範本。 |
Terraform (AzAPI 提供者) 資源定義
policyAssignments 資源類型可以使用目標作業來部署:
如需每個 API 版本中已變更屬性的清單,請參閱 變更記錄檔。
資源格式
若要建立 Microsoft.Authorization/policyAssignments 資源,請將下列 Terraform 新增至範本。
resource "azapi_resource" "symbolicname" {
type = "Microsoft.Authorization/policyAssignments@2025-03-01"
name = "string"
parent_id = "string"
identity {
type = "string"
identity_ids = [
"string"
]
}
location = "string"
body = {
properties = {
assignmentType = "string"
definitionVersion = "string"
description = "string"
displayName = "string"
enforcementMode = "string"
metadata = ?
nonComplianceMessages = [
{
message = "string"
policyDefinitionReferenceId = "string"
}
]
notScopes = [
"string"
]
overrides = [
{
kind = "string"
selectors = [
{
in = [
"string"
]
kind = "string"
notIn = [
"string"
]
}
]
value = "string"
}
]
parameters = {
{customized property} = {
value = ?
}
}
policyDefinitionId = "string"
resourceSelectors = [
{
name = "string"
selectors = [
{
in = [
"string"
]
kind = "string"
notIn = [
"string"
]
}
]
}
]
}
}
}
屬性值
Microsoft.Authorization/policyAssignments
| 名字 | 描述 | 價值 |
|---|---|---|
| 身分識別 | 與原則指派相關聯的受控識別。 | 身分識別 |
| 位置 | 原則指派的位置。 只有在使用受控識別時才需要。 | 字串 |
| 名字 | 資源名稱 | 字串 約束: 模式 = ^[^<>*%&:\?.+/]*[^<>*%&:\?.+/ ]+$ (必要) |
| 父項識別碼 | 要套用此延伸模組資源之資源的標識碼。 | 字串 (必要) |
| 性能 | 原則指派的屬性。 | PolicyAssignmentProperties |
| 型別 | 資源類型 | “Microsoft.Authorization/policyAssignments@2025-03-01” |
身份
| 名字 | 描述 | 價值 |
|---|---|---|
| 型別 | 識別類型。 將系統或使用者指派的身分識別新增至資源時,這是唯一的必要字段。 | “無” “系統分配” 'UserAssigned' |
| 使用者指派的身份 | 與原則相關聯的使用者身分識別。 使用者身分識別字典索引鍵參考的格式為 ARM 資源標識符:'/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ManagedIdentity/userAssignedIdentities/{identityName}'。 | IdentityUserAssignedIdentities |
IdentityUserAssignedIdentities
| 名字 | 描述 | 價值 |
|---|
不合規消息
| 名字 | 描述 | 價值 |
|---|---|---|
| 訊息 | 描述資源不符合原則之原因的訊息。 這會顯示在「拒絕」錯誤訊息和資源不符合規範的合規性結果中。 | 字串 (必要) |
| policyDefinitionReferenceId | 原則集合定義中訊息的目標原則定義參考標識符。 這隻適用於原則指派指派原則集定義時。 如果未提供此訊息,訊息會套用至此原則指派指派的所有原則。 | 字串 |
覆蓋
| 名字 | 描述 | 價值 |
|---|---|---|
| 類 | 覆寫種類。 | 'definitionVersion' 'policyEffect' |
| 選擇 | 選取器表達式的清單。 | 選取器[] |
| 價值 | 要覆寫原則屬性的值。 | 字串 |
參數值
| 名字 | 描述 | 價值 |
|---|
參數值值
| 名字 | 描述 | 價值 |
|---|---|---|
| 價值 | 參數的值。 | 任何 |
PolicyAssignmentProperties
| 名字 | 描述 | 價值 |
|---|---|---|
| assignmentType | 原則指派的類型。 可能的值為 NotSpecified、System、SystemHidden 和 Custom。 變。 | “自定義” “未指定” '系統' “SystemHidden” |
| 定義版本 | 要使用的原則定義版本。 | 字串 |
| 描述 | 如果違反原則,此訊息將會是回應的一部分。 | 字串 |
| 顯示名稱 | 原則指派的顯示名稱。 | 字串 |
| enforcementMode | 原則指派強制模式。 可能的值為 Default、DoNotEnforce 和 Enroll | 預設 “不強制執行” “註冊” |
| 中繼資料 | 原則指派元數據。 元數據是開放式物件,通常是索引鍵值組的集合。 | 任何 |
| nonComplianceMessages | 描述資源不符合原則之原因的訊息。 | NoncomplianceMessage[] |
| notScopes | 原則的排除範圍。 | 字串[] |
| 重寫 | 原則屬性值覆寫。 | 覆寫[] |
| 參數 | 指派原則規則的參數值。 索引鍵是參數名稱。 | 參數值 |
| policyDefinitionId | 要指派的原則定義或原則集定義的標識碼。 | 字串 |
| resourceSelectors | 依資源屬性篩選原則的資源選取器清單。 | 資源選擇器 |
資源選擇器
| 名字 | 描述 | 價值 |
|---|---|---|
| 名字 | 資源選取器的名稱。 | 字串 |
| 選擇 | 選取器表達式的清單。 | 選取器[] |
選擇器
| 名字 | 描述 | 價值 |
|---|---|---|
| 在 | 要篩選的值清單。 | 字串[] |
| 類 | 選取器種類。 | “policyDefinitionReferenceId” 'resourceLocation' 'resourceType' 'resourceWithoutLocation' |
| notIn | 要篩選掉的值清單。 | 字串[] |
UserAssignedIdentitiesValue
| 名字 | 描述 | 價值 |
|---|
使用範例
Terraform 範例
部署原則指派的基本範例。
terraform {
required_providers {
azapi = {
source = "Azure/azapi"
}
azurerm = {
source = "hashicorp/azurerm"
}
}
}
provider "azurerm" {
features {
}
}
provider "azapi" {
skip_provider_registration = false
}
variable "resource_name" {
type = string
default = "acctest0001"
}
variable "location" {
type = string
default = "eastus"
}
data "azurerm_client_config" "current" {
}
data "azapi_resource" "subscription" {
type = "Microsoft.Resources/subscriptions@2021-01-01"
resource_id = "/subscriptions/${data.azurerm_client_config.current.subscription_id}"
response_export_values = ["*"]
}
resource "azapi_resource" "policyDefinition" {
type = "Microsoft.Authorization/policyDefinitions@2021-06-01"
parent_id = "/subscriptions/${data.azurerm_client_config.current.subscription_id}"
name = var.resource_name
body = {
properties = {
description = ""
displayName = "my-policy-definition"
mode = "All"
parameters = {
allowedLocations = {
metadata = {
description = "The list of allowed locations for resources."
displayName = "Allowed locations"
strongType = "location"
}
type = "Array"
}
}
policyRule = {
if = {
not = {
field = "location"
in = "[parameters('allowedLocations')]"
}
}
then = {
effect = "audit"
}
}
policyType = "Custom"
}
}
schema_validation_enabled = false
response_export_values = ["*"]
}
resource "azapi_resource" "policyAssignment" {
type = "Microsoft.Authorization/policyAssignments@2022-06-01"
parent_id = data.azapi_resource.subscription.id
name = var.resource_name
body = {
properties = {
displayName = ""
enforcementMode = "Default"
parameters = {
listOfAllowedLocations = {
value = [
"West Europe",
"West US 2",
"East US 2",
]
}
}
policyDefinitionId = azapi_resource.policyDefinition.id
scope = data.azapi_resource.subscription.id
}
}
schema_validation_enabled = false
response_export_values = ["*"]
}