To enable automatic instance repairs for Azure using Python, you can follow the below steps:
Step 1: Install the Azure SDK for Python using pip command as shown below:
pip install azure-mgmt-compute
Step 2: Import the required modules as shown below:
from azure.identity import AzureCliCredential
from azure.mgmt.compute import ComputeManagementClient
Step 3: Authenticate and create a compute management client instance as shown below:
credential = AzureCliCredential()
subscription_id = '<your-subscription-id>'
compute_client = ComputeManagementClient(credential, subscription_id)
Step 4: Get the resource group and virtual machine name for which you want to enable automatic instance repairs as shown below:
resource_group_name = '<your-resource-group-name>'
vm_name = '<your-vm-name>'
Step 5: Enable automatic instance repairs for the virtual machine using the below code:
vm = compute_client.virtual_machines.get(resource_group_name, vm_name)
vm.instance_view.auto_upgrade_policy = 'Rolling'
compute_client.virtual_machines.create_or_update(resource_group_name, vm_name, vm)
In the above code, we are getting the virtual machine instance, setting the auto-upgrade policy to “Rolling”, and then updating the virtual machine with the new policy.
Step 6: Verify if the automatic instance repairs are enabled for the virtual machine using the below code:
vm = compute_client.virtual_machines.get(resource_group_name, vm_name)
print(vm.instance_view.auto_upgrade_policy)
The above code will print the auto-upgrade policy of the virtual machine, which should be “Rolling” if the automatic instance repairs are enabled.
That’s it! You have successfully enabled automatic instance repairs for Azure virtual machine using Python.