使用 Bicep 檔案的管理群組部署

此文章描述如何在部署至管理群組時使用 Bicep 設定範圍。

隨著您的組織逐漸成熟,您可以部署 Bicep 檔案,以在管理群組層級建立資源。 例如,您可能需要為管理群組定義並指派原則 (部分機器翻譯) 或 Azure 角色型存取控制 (Azure RBAC) (部分機器翻譯)。 使用管理群組層級範本,您可以在管理群組層級以宣告方式套用原則及指派角色。

訓練資源

如果您比較想要透過逐步指導來了解部署範圍,請參閱使用 Bicep 將資源部署到訂用帳戶、管理群組和租用戶

支援的資源

並非所有的資源類型都可部署到管理群組層級。 此節將列出支援的資源類型。

針對 Azure 藍圖,請使用:

針對 Azure 原則,請使用:

針對存取控制,請使用:

針對部署至訂用帳戶或資源群組的巢狀範本,請使用:

針對管理您的資源,請使用:

管理群組是租用戶層級的資源。 不過,您可以將新管理群組的範圍設為租用戶,藉以在管理群組部署中建立管理群組。 請參閱管理群組 (部分機器翻譯)。

集合範圍

若要將範圍設為管理群組,請使用:

targetScope = 'managementGroup'

部署命令

若要部署至管理群組,請使用管理群組部署命令。

針對 Azure CLI,請使用 az deployment mg create (部分機器翻譯):

az deployment mg create \
  --name demoMGDeployment \
  --location WestUS \
  --management-group-id myMG \
  --template-uri "https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/management-level-deployment/azuredeploy.json"

有關用於部署 ARM 範本的部署命令和選項,如需詳細資訊,請參閱:

部署位置和名稱

針對管理群組層級部署,您必須提供部署的位置。 部署的位置與您部署的資源位置不同。 部署位置會指定部署資料的儲存位置。 訂用帳戶 (部分機器翻譯) 和租用戶 (部分機器翻譯) 部署也需要位置。 針對資源群組 (部分機器翻譯) 部署,資源群組的位置會用來儲存部署資料。

您可以提供部署的名稱,或使用預設的部署名稱。 預設名稱是範本檔案的名稱。 例如,部署名為 main.bicep 的範本會建立 main 的預設部署名稱。

對於每個部署名稱而言,此位置是不可變的。 當某個位置已經有名稱相同的現有部署時,您無法在其他位置建立部署。 例如,如果您在 centralus 中建立名為 deployment1 的管理群組部署,稍後即便您是在 westus 的位置建立另一個部署,該部署仍然無法使用 deployment1 作為名稱。 如果您收到錯誤代碼 InvalidDeploymentLocation,請使用不同的名稱或與先前該名稱部署相同的位置。

部署範圍

部署至管理群組時,您可以將資源部署至:

  • 作業的目標管理群組
  • 租用戶中的另一個管理群組
  • 管理群組中的訂用帳戶
  • 管理群組中的資源群組
  • 資源群組的租用戶

延伸模組資源的範圍可以設為與部署目標不同的目標。

部署範本的使用者必須有權存取指定範圍。

將範圍設為管理群組

若要將資源部署到目標管理群組,請使用 resource 關鍵字新增那些資源。

targetScope = 'managementGroup'

// policy definition created in the management group
resource policyDefinition 'Microsoft.Authorization/policyDefinitions@2021-06-01' = {
  ...
}

若要將另一個管理群組設為目標,請新增模組 (部分機器翻譯)。 使用 managementGroup 函式 (部分機器翻譯) 來設定 scope 屬性。 提供管理群組名稱。

targetScope = 'managementGroup'

param otherManagementGroupName string

// module deployed at management group level but in a different management group
module exampleModule 'module.bicep' = {
  name: 'deployToDifferentMG'
  scope: managementGroup(otherManagementGroupName)
}

將範圍設為訂用帳戶

您也可以將管理群組內的訂用帳戶設為目標。 部署範本的使用者必須有權存取指定範圍。

若要將管理群組內的訂用帳戶設為目標,請新增模組。 使用 subscription 函式 (部分機器翻譯) 來設定 scope 屬性。 提供訂用帳戶識別碼。

targetScope = 'managementGroup'

param subscriptionID string

// module deployed to subscription in the management group
module exampleModule 'module.bicep' = {
  name: 'deployToSub'
  scope: subscription(subscriptionID)
}

將範圍設為資源群組

您也可以將管理群組內的資源群組設為目標。 部署範本的使用者必須有權存取指定範圍。

若要將管理群組內的資源群組設為目標,請新增模組。 使用 resourceGroup 函式 (部分機器翻譯) 來設定 scope 屬性。 提供訂用帳戶識別碼和資源群組名稱。

targetScope = 'managementGroup'

param subscriptionID string
param resourceGroupName string

// module deployed to resource group in the management group
module exampleModule 'module.bicep' = {
  name: 'deployToRG'
  scope: resourceGroup(subscriptionID, resourceGroupName)
}

將範圍設為租用戶

若要在租用戶建立資源,請新增模組。 使用 tenant 函式 (部分機器翻譯) 來設定其 scope 屬性。 部署範本的使用者必須擁有在租用戶部署的必要存取權 (部分機器翻譯)。

targetScope = 'managementGroup'

// module deployed at tenant level
module exampleModule 'module.bicep' = {
  name: 'deployToTenant'
  scope: tenant()
}

或者,您也可以針對某些資源類型將範圍設為 /,例如管理群組。 下一節將描述如何建立新的管理群組。

管理群組

若要在管理群組部署中建立管理群組,您必須將範圍設為租用戶。

下列範例會在根管理群組中建立新的管理群組。

targetScope = 'managementGroup'

param mgName string = 'mg-${uniqueString(newGuid())}'

resource newMG 'Microsoft.Management/managementGroups@2021-04-01' = {
  scope: tenant()
  name: mgName
  properties: {}
}

output newManagementGroup string = mgName

下一個範例會在設為部署目標的管理群組中建立新的管理群組。 其會使用管理群組函式 (部分機器翻譯)。

targetScope = 'managementGroup'

param mgName string = 'mg-${uniqueString(newGuid())}'

resource newMG 'Microsoft.Management/managementGroups@2021-04-01' = {
  scope: tenant()
  name: mgName
  properties: {
    details: {
      parent: {
        id: managementGroup().id
      }
    }
  }
}

output newManagementGroup string = mgName

訂用帳戶

若要使用 ARM 範本在管理群組中建立新的 Azure 訂用帳戶,請參閱:

若要部署會將現有 Azure 訂用帳戶移至新管理群組的範本,請參閱在 ARM 範本中移動訂用帳戶 (部分機器翻譯)

Azure 原則

部署至管理群組的自訂原則定義是管理群組的延伸模組。 若要取得自訂原則定義的識別碼,請使用 extensionResourceId() (部分機器翻譯) 函式。 內建原則定義是租用戶層級的資源。 若要取得內建原則定義的識別碼,請使用 tenantResourceId() (部分機器翻譯) 函式。

下列範例顯示如何在管理群組層級定義 (部分機器翻譯) 原則,並加以指派。

targetScope = 'managementGroup'

@description('An array of the allowed locations, all other locations will be denied by the created policy.')
param allowedLocations array = [
  'australiaeast'
  'australiasoutheast'
  'australiacentral'
]

resource policyDefinition 'Microsoft.Authorization/policyDefinitions@2021-06-01' = {
  name: 'locationRestriction'
  properties: {
    policyType: 'Custom'
    mode: 'All'
    parameters: {}
    policyRule: {
      if: {
        not: {
          field: 'location'
          in: allowedLocations
        }
      }
      then: {
        effect: 'deny'
      }
    }
  }
}

resource policyAssignment 'Microsoft.Authorization/policyAssignments@2022-06-01' = {
  name: 'locationAssignment'
  properties: {
    policyDefinitionId: policyDefinition.id
  }
}

下一步

若要了解其他範圍,請參閱: