title | description | author | ms.service | ms.custom | ms.topic | ms.date | ms.author |
---|---|---|---|---|---|---|---|
Tutorial - python |
Create a virtual machine and assign an automanage best practices configuration profile to it. |
andrsmith |
automanage |
devx-track-python |
tutorial |
08/25/2022 |
andrsmith |
In this tutorial, you'll create a resource group and a virtual machine. You'll then assign an Automanage Best Practices configuration profile to the new machine using the Python SDK.
Sign in to Azure by using following command:
az login
Connect-AzAccount
Create a resource group:
az group create --name "test-rg" --location "eastus"
new-azResourceGroup -Name "test-rg" -Location "eastus"
Create a Windows virtual machine:
az vm create `
--resource-group "test-rg" `
--name "testvm" `
--location "eastus" `
--image win2016datacenter `
--admin-username testUser `
--size Standard_D2s_v3 `
--storage-sku Standard_LRS
New-AzVm `
-ResourceGroupName 'test-rg' `
-Name 'testvm' `
-Location 'eastus' `
-VirtualNetworkName 'testvm-vnet' `
-SubnetName 'testvm-subnet' `
-SecurityGroupName 'test-vm-nsg'
Now that we've successfully created a resource group and a virtual machine, it's time to set up a Python project and assign an Automanage Best Practices configuration profile to the newly created virtual machine.
Install the Azure Identity and Azure Automanage packages using pip
:
pip install azure-mgmt-automanage
pip install azure-identity
Create an app.py
file and import the installed packages within it:
from azure.identity import DefaultAzureCredential
from azure.mgmt.automanage import AutomanageClient
Set some local variables:
sub = "<sub ID>"
rg = "test-rg"
vm = "testvm"
Use the DefaultAzureCredential within the azure-identity
package to authenticate to Azure. Then, use the credential to create an Automanage Client.
credential = DefaultAzureCredential()
client = AutomanageClient(credential, sub)
Now we'll create an assignment between our new virtual machine and a Best Practices profile:
assignment = {
"properties": {
"configurationProfile": "/providers/Microsoft.Automanage/bestPractices/AzureBestPracticesProduction",
}
}
# assignment name must be 'default'
client.configuration_profile_assignments.create_or_update(
"default", rg, vm, assignment)
Run the Python file:
python app.py
Navigate to the virtual machine and select the Automanage blade:
View the Automanage Profile now enabled on the virtual machine:
For more information on the Automanage Python SDK, please visit the azure-samples-python-management repo.