Share via


快速入門:使用 Python 建立管理群組

管理群組是可協助您跨多個訂用帳戶管理容器存取、原則及合規性的容器。 建立這些容器來建置可與 Azure 原則Azure 角色型存取控制搭配使用的高效率階層。 如需有關管理群組的詳細資訊,請參閱使用 Azure 管理群組來組織資源

在目錄中建立的第一個管理群組可能需要 15 分鐘的時間才能完成。 第一次在 Azure 中設定目錄的管理群組服務時會執行一些程序。 程序完成時,您會收到通知。 如需詳細資訊,請參閱管理群組的初始設定

必要條件

  • 如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶

  • 如果未啟用階層保護,則租用戶中的任何 Azure AD 使用者都可以建立管理群組,而不需將管理群組寫入權限指派給該使用者。 這個新管理群組會變成根管理群組或預設管理群組的子系,而建立者會獲得「擁有者」角色指派。 管理群組服務允許此功能,因此在根層級不需要角色指派。 沒有任何使用者可以存取建立的根管理群組。 為了避免尋找 Azure AD 全域管理員的困難以開始使用管理群組,我們允許在根層級建立初始管理群組。

Azure Cloud Shell

Azure Cloud Shell 是裝載於 Azure 中的互動式殼層環境,可在瀏覽器中使用。 您可以使用 Bash 或 PowerShell 搭配 Cloud Shell,與 Azure 服務共同使用。 您可以使用 Cloud Shell 預先安裝的命令,執行本文提到的程式碼,而不必在本機環境上安裝任何工具。

要啟動 Azure Cloud Shell:

選項 範例/連結
選取程式碼或命令區塊右上角的 [試試看]。 選取 [試試看] 並不會自動將程式碼或命令複製到 Cloud Shell 中。 Screenshot that shows an example of Try It for Azure Cloud Shell.
請前往 https://shell.azure.com,或選取 [啟動 Cloud Shell] 按鈕,在瀏覽器中開啟 Cloud Shell。 Button to launch Azure Cloud Shell.
選取 Azure 入口網站右上方功能表列上的 [Cloud Shell] 按鈕。 Screenshot that shows the Cloud Shell button in the Azure portal

若要使用 Azure Cloud Shell:

  1. 啟動 Cloud Shell。

  2. 選取程式碼區塊 (或命令區塊) 上的 [複製] 按鈕以複製程式碼或命令。

  3. 透過在 Windows 和 Linux 上選取 Ctrl+Shift+V;或在 macOS 上選取 Cmd+Shift+V,將程式碼或命令貼到 Cloud Shell 工作階段中。

  4. 選取 Enter 鍵執行程式碼或命令。

新增 Resource Graph 程式庫

若要使用 Python 來控管管理群組,必須新增程式庫。 此程式庫適用於可使用 Python 的任何位置,包括 bash on Windows 10 或安裝在本機上。

  1. 確認已安裝最新的 Python (至少 3.8)。 如果尚未安裝,請在 Python.org 下載。

  2. 確認已安裝最新的 Azure CLI (至少 2.5.1)。 如果尚未安裝,請參閱 安裝 Azure CLI

    注意

    Azure CLI 是啟用 Python 以在下列範例中使用 CLI 型驗證的必要項目。 如需其他選項的相關資訊,請參閱使用適用於 Python 的 Azure 管理程式庫進行驗證

  3. 透過 Azure CLI 進行驗證。

    az login
    
  4. 在您選擇的 Python 環境中,安裝管理群組所需的程式庫:

    # Add the management groups library for Python
    pip install azure-mgmt-managementgroups
    
    # Add the Resources library for Python
    pip install azure-mgmt-resource
    
    # Add the CLI Core library for Python for authentication (development only!)
    pip install azure-cli-core
    

    注意

    如果為所有使用者安裝 Python,則必須從提高許可權的主控台執行這些命令。

  5. 驗證是否已安裝程式庫。 azure-mgmt-managementgroups 應為 0.2.0 或更高版本,azure-mgmt-resource 應為 9.0.0 或更高版本,azure-cli-core 應為 2.5.0 或更高版本。

    # Check each installed library
    pip show azure-mgmt-managementgroups azure-mgmt-resource azure-cli-core
    

建立管理群組

  1. 建立 Python 指令碼,並將下列來源儲存為 mgCreate.py

    # Import management group classes
    from azure.mgmt.managementgroups import ManagementGroupsAPI
    
    # Import specific methods and models from other libraries
    from azure.common.credentials import get_azure_cli_credentials
    from azure.common.client_factory import get_client_from_cli_profile
    from azure.mgmt.resource import ResourceManagementClient, SubscriptionClient
    
    # Wrap all the work in a function
    def createmanagementgroup( strName ):
        # Get your credentials from Azure CLI (development only!) and get your subscription list
        subsClient = get_client_from_cli_profile(SubscriptionClient)
        subsRaw = []
        for sub in subsClient.subscriptions.list():
            subsRaw.append(sub.as_dict())
        subsList = []
        for sub in subsRaw:
            subsList.append(sub.get('subscription_id'))
    
        # Create management group client and set options
        mgClient = get_client_from_cli_profile(ManagementGroupsAPI)
        mg_request = {'name': strName, 'display_name': strName}
    
        # Create management group
        mg = mgClient.management_groups.create_or_update(group_id=strName,create_management_group_request=mg_request)
    
        # Show results
        print(mg)
    
    createmanagementgroup("MyNewMG")
    
  2. 使用 Azure CLI 與 az login 進行驗證。

  3. 在終端機中輸入下列命令:

    py mgCreate.py
    

建立管理群組的結果會輸出至主控台作為 LROPoller 物件。

清除資源

如果您想要從 Python 環境中移除已安裝的程式庫,則可以使用下列命令:

# Remove the installed libraries from the Python environment
pip uninstall azure-mgmt-managementgroups azure-mgmt-resource azure-cli-core

下一步

在本快速入門中,您已建立管理群組來組織您的資源階層。 管理群組可以保留訂用帳戶或其他管理群組。

若要深入了解管理群組,以及如何管理您的資源階層,請繼續: