Share via


教學課程:建立虛擬機器並將 Automanage 設定檔指派給它

在本教學課程中,您將建立資源群組和虛擬機器。 接著,您將使用 Python SDK 將 Automanage 最佳做法組態設定檔指派給新機器。

必要條件

建立資源

登入 Azure

使用下列命令登入 Azure:

az login

建立資源群組

建立資源群組:

az group create --name "test-rg" --location "eastus"

建立虛擬機器

建立 Windows 虛擬機器:

az vm create `
    --resource-group "test-rg" `
    --name "testvm" `
    --location "eastus" `
    --image win2016datacenter `
    --admin-username testUser `
    --size Standard_D2s_v3 `
    --storage-sku Standard_LRS

將最佳做法設定檔指派給虛擬機器

既然我們已成功建立資源群組和虛擬機器,現在是時候設定 Python 專案,並將 Automanage 最佳做法組態設定檔指派給新建立的虛擬機器。

安裝 Python 套件

使用 pip 安裝 Azure 身分識別和 Azure Automanage 套件:

pip install azure-mgmt-automanage
pip install azure-identity

匯入套件

建立 app.py 檔案,並在其中匯入已安裝的套件:

from azure.identity import DefaultAzureCredential
from azure.mgmt.automanage import AutomanageClient

設定一些區域變數:

sub = "<sub ID>"
rg = "test-rg"
vm = "testvm"

向 Azure 進行驗證並建立 Automanage 用戶端

使用 azure-identity 套件內的DefaultAzureCredential ,向 Azure 進行驗證。 然後,使用認證來建立 Automanage 用戶端

credential = DefaultAzureCredential()
client = AutomanageClient(credential, sub)

建立最佳做法設定檔指派

現在,我們將建立新虛擬機器與最佳做法設定檔之間的指派:

assignment = {
    "properties": {
        "configurationProfile": "/providers/Microsoft.Automanage/bestPractices/AzureBestPracticesProduction",
    }
}

# assignment name must be 'default'
client.configuration_profile_assignments.create_or_update(
    "default", rg, vm, assignment)

執行 Python 檔案:

python app.py


在入口網站中檢視指派

瀏覽至虛擬機器,然後選取 [Automanage] 刀鋒視窗:automanage blade

檢視虛擬機器上現已啟用的 Automanage 設定檔:automanage vm

下一步

如需 Automanage Python SDK 的詳細資訊,請瀏覽 azure-samples-python-management repo