다음을 통해 공유


Python 및 Azure SDK(Python 라이브러리)를 사용하여 Azure Lab Services에서 랩 만들기

Important

Azure Lab Services는 2027년 6월 28일에 사용 중지됩니다. 자세한 내용은 사용 중지 가이드를 참조하세요.

이 문서에서는 Python 및 Azure SDK(Python 라이브러리)를 사용하여 랩을 만드는 방법을 알아봅니다. 랩은 이전에 만들어진 랩 계획의 설정을 사용합니다. Azure Lab Services에 대한 자세한 개요는 Azure Lab Services 소개를 참조하세요.

필수 조건

  • 활성 구독이 있는 Azure 계정. Azure 구독이 아직 없는 경우 시작하기 전에 체험 계정을 만듭니다.

랩 만들기

랩을 만들려면 먼저 랩 계획 개체가 필요합니다. Python을 사용하여 랩 계획 만들기에서는 BellowsCollege_rg라는 리소스 그룹에서 BellowsCollege_labplan이라는 랩 계획을 만드는 방법을 알아봅니다.

# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

from datetime import timedelta
import time
from azure.identity import DefaultAzureCredential
from azure.mgmt.labservices import LabServicesClient
from azure.mgmt.resource import ResourceManagementClient

def main():

    SUBSCRIPTION_ID = "<Subscription ID>"
    TIME = str(time.time()).replace('.','')
    GROUP_NAME = "BellowsCollege_rg"
    LABPLAN = "BellowsCollege_labplan"
    LAB = "BellowsCollege_lab"
    LOCATION = 'southcentralus'    

    # Create clients
    # # For other authentication approaches, please see: https://pypi.org/project/azure-identity/
    resource_client = ResourceManagementClient(
        credential=DefaultAzureCredential(),
        subscription_id=SUBSCRIPTION_ID
    )
    
    labservices_client = LabServicesClient(
        credential=DefaultAzureCredential(),
        subscription_id=SUBSCRIPTION_ID
    )

    #Get single LabServices Lab Plan
    labservices_labplan = labservices_client.lab_plans.get(GROUP_NAME, LABPLAN)

    print("Get lab plans")
    print(labservices_labplan)

    #Get image information
    LABIMAGES = labservices_client.images.list_by_lab_plan(GROUP_NAME,LABPLAN)
    image = (list(filter(lambda x: (x.name == "microsoftwindowsdesktop.windows-11.win11-21h2-pro"), LABIMAGES)))
    
    #Get lab quota
    USAGEQUOTA = timedelta(hours=10)

    # Password
    CUSTOMPASSWORD = "<custom password>"
    # Create LabServices Lab
    LABBODY = {
        "name": LAB,
        "location" : LOCATION,
        "properties" : {
            "networkProfile": {},
            "connectionProfile" : {
                "webSshAccess" : "None",
                "webRdpAccess" : "None",
                "clientSshAccess" : "None",
                "clientRdpAccess" : "Public"
            },
            "AutoShutdownProfile" : {
                "shutdownOnDisconnect" : "Disabled",
                "shutdownWhenNotConnected" : "Disabled",
                "shutdownOnIdle" : "None"
            },
            "virtualMachineProfile" : {
                "createOption" : "TemplateVM",
                "imageReference" : {
                    "offer": image[0].offer,
                    "publisher": image[0].publisher,
                    "sku": image[0].sku,
                    "version": image[0].version
                },
                "sku" : {
                    "name" : "Classic_Fsv2_2_4GB_128_S_SSD",
                    "capacity" : 2
                },
                "additionalCapabilities" : {
                    "installGpuDrivers" : "Disabled"
                },
                "usageQuota" : USAGEQUOTA,
                "UseSharedPassword" : "Enabled",
                "adminUser" : {
                    "username" : "testuser",
                    "password" : CUSTOMPASSWORD
                }
            },
            "securityProfile" : {
                "openAccess" : "Disabled"
            },
            "rosterProfile" : {},
            "labPlanId" : labservices_labplan.id,
            "title" : "lab-python",
            "description" : "lab 99 description updated"
        }
    }

    poller = labservices_client.labs.begin_create_or_update(
        GROUP_NAME,
        LAB,
        LABBODY
    )

    lab_result = poller.result()
    print(f"Created Lab  {lab_result.name}")

    # Get LabServices Labs
    labservices_lab = labservices_client.labs.get(GROUP_NAME,LAB)
    print("Get lab:\n{}".format(labservices_lab))
    


if __name__ == "__main__":
    main()


리소스 정리

이 애플리케이션을 계속 사용하지 않으려면 다음 단계에 따라 그룹 및 랩을 삭제합니다.

# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

from datetime import timedelta
import time
from azure.identity import DefaultAzureCredential
from azure.mgmt.labservices import LabServicesClient
from azure.mgmt.resource import ResourceManagementClient

# - other dependence -
# - end -
#

def main():

    SUBSCRIPTION_ID = "<Subscription ID>"
    TIME = str(time.time()).replace('.','')
    GROUP_NAME = "BellowsCollege_rg"
    LABPLAN = "BellowsCollege_labplan"
    LAB = "BellowsCollege_lab"
    LOCATION = 'southcentralus'    

    # Create clients
    # # For other authentication approaches, please see: https://pypi.org/project/azure-identity/
    resource_client = ResourceManagementClient(
        credential=DefaultAzureCredential(),
        subscription_id=SUBSCRIPTION_ID
    )
    
    labservices_client = LabServicesClient(
        credential=DefaultAzureCredential(),
        subscription_id=SUBSCRIPTION_ID
    )

    # Delete Lab
    labservices_client.labs.begin_delete(
        GROUP_NAME,
        LAB
    ).result()
    print("Deleted lab.\n")

    # Delete Group
    resource_client.resource_groups.begin_delete(
        GROUP_NAME
    ).result()


if __name__ == "__main__":
    main()

다음 단계

관리자는 Azure PowerShell 모듈Az.LabServices cmdlet에 대해 자세히 알아볼 수 있습니다.