Kommentar
Åtkomst till den här sidan kräver auktorisering. Du kan prova att logga in eller ändra kataloger.
Åtkomst till den här sidan kräver auktorisering. Du kan prova att ändra kataloger.
Viktigt!
Azure Lab Services tas ur bruk den 28 juni 2027. Mer information finns i pensionsguiden. För att förenkla migreringen har Microsoft publicerat automationsskript som hjälper dig att rensa Lab Services-resurser. Dessa är tillgängliga på GitHub-lagringsplatsen för Azure Lab Services-tillbakadragningsskript.
I den här artikeln får du lära dig hur du skapar ett labb med Python och Azure Python-bibliotek (SDK). Labbet använder inställningarna från en tidigare skapad labbplan. Detaljerad översikt över Azure Lab Services finns i En introduktion till Azure Lab Services.
Förutsättningar
- Ett Azure-konto med en aktiv prenumeration. Om du inte har någon Azure-prenumeration skapar du ett kostnadsfritt konto innan du börjar.
- Ett Azure-konto med behörighet att hantera ett labb, till exempel rollen Labbskapare, ägare, deltagare eller Lab Services-deltagare i Azure RBAC. Läs mer om inbyggda roller och tilldelningsomfång för Azure Lab Services.
- En Azure-labbplan. Om du inte har någon labbplan än följer du stegen i Snabbstart: Konfigurera resurser för att skapa labb.
- Konfigurera lokal Python-utvecklingsmiljö för Azure.
- Requirements.txt kan laddas ned från Azure Python-exempel
Skapa ett labb
Innan du kan skapa ett labb behöver du labbplansobjektet. I skapa en labbplan med hjälp av Python får du lära dig hur du skapar en labbplan med namnet BellowsCollege_labplan i en resursgrupp med namnet BellowsCollege_rg.
# --------------------------------------------------------------------------
# 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()
Rensa resurser
Om du inte fortsätter att använda det här programmet tar du bort gruppen och labbet med följande steg:
# --------------------------------------------------------------------------
# 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()
Nästa steg
Som administratör kan du lära dig mer om Azure PowerShell-modulen och Az.LabServices-cmdletar.