英文:
how to add LogAnalyticsWorkSpace extension to VM using Python SDK in Azure
问题
我已在Azure中创建了Log Analytics工作区。现在想要使用Python SDK将其附加为VM的扩展。我已按照文档进行操作,但出现以下错误。
发生类'azure.core.exceptions.HttpResponseError'。
请有人帮助我解决这个问题。
以下是我尝试使用计算管理客户端的方法。
extension = compute_client.virtual_machine_extensions.begin_create_or_update(
"MyRG",
"MyVMName",
"MicrosoftMonitoringAgent", ##VMExtension name
{
"type": "Microsoft.Compute/virtualMachines/extensions",
"location": "eastus",
"publisher": "Microsoft.EnterpriseCloud.Monitoring",
"type_handler_version": "1.0",
"auto_upgrade_minor_version": True,
"settings": {
"workspaceId": "XXXXX"
},
"protected_settings": {
"workspaceKey": "XXXXX"
}
}
).Result()
英文:
I have created Log Analytics workspace in azure. Now to want to attach it as an extension to VM using Python sdk. I have followed the documentation and getting error as follow.
class 'azure.core.exceptions.HttpResponseError'> occurred.
Can someone help me in this please.
Below is the approach I have tried using compute management client.
extension = compute_client.virtual_machine_extensions.begin_create_or_update(
"MyRG",
"MyVMName",
"MicrosoftMonitoringAgent", ##VMExtension name
{
"type": "Microsoft.Compute/virtualMachines/extensions",
"location": "eastus",
"publisher": "Microsoft.EnterpriseCloud.Monitoring",
"type_handler_version": "1.0",
"auto_upgrade_minor_version": True,
"settings": {
"workspaceId": "XXXXX"
},
"protected_settings": {
"workspaceKey": "XXXXX"
}
}
).Result()
答案1
得分: 0
我在我的环境中尝试过,并成功使用Python创建了Log Analytics Workspace扩展到虚拟机:
代码:
我尝试使用**ComputeManagementClient
和DefaultAzureCredential
方法来执行begin_create_or_update**。请确保参数和资源处于正确的状态。
from azure.mgmt.compute import ComputeManagementClient
from azure.identity import DefaultAzureCredential
subscriptionid = "<subscription id>"
credential = DefaultAzureCredential()
compute_client = ComputeManagementClient(subscription_id=subscriptionid, credential=credential)
extension = compute_client.virtual_machine_extensions.begin_create_or_update(
resource_group_name="<resource group>",
vm_name="<vname>",
vm_extension_name="MicrosoftMonitoringAgent",
extension_parameters={
"publisher": "Microsoft.EnterpriseCloud.Monitoring",
"location": "eastus",
"type_handler_version": "1.0",
"auto_upgrade_minor_version": True,
"settings": {
"workspaceId": "7c950e4f-5c9b-4205-ba79-278cd22bd220"
},
"protected_settings": {
"workspaceKey": "TlLvMVn55RQEHtWBJUSvmbYyaVeev4srp1Vu5ZF/dh2paVI9qrQ/8P4rnEGjmPkHD8LbOpQHKUOhSHMz2r/v2A=="
}
}
)
print("扩展已创建")
控制台:
我执行了上面的代码,在我的环境中成功创建了Log Analytics扩展。
门户:
参考链接:
azure-content/log-analytics-azure-vm-extension.md at master · uglide/azure-content (github.com)
英文:
I tried in my environment and successfully created Log-Analytics-WorkSpace extension to VM using python:
Code:
I tried with ComputeManagementClient
and DefaultAzureCredential
method to begin_create_or_update. Make sure with parameters and resources are correct state.
from azure.mgmt.compute import ComputeManagementClient
from azure.identity import DefaultAzureCredential
subscriptionid = "<subscription id>"
credential = DefaultAzureCredential()
compute_client=ComputeManagementClient(subscription_id=subscriptionid,credential=credential)
extension = compute_client.virtual_machine_extensions.begin_create_or_update(
resource_group_name="<resource group>",
vm_name="<vname>",vm_extension_name="MicrosoftMonitoringAgent",extension_parameters=
{
"publisher": "Microsoft.EnterpriseCloud.Monitoring",
"location":"eastus",
"type_handler_version": "1.0",
"auto_upgrade_minor_version": True,
"settings": {
"workspaceId": "7c950e4f-5c9b-4205-ba79-278cd22bd220"
},
"protected_settings": {
"workspaceKey": "TlLvMVn55RQEHtWBJUSvmbYyaVeev4srp1Vu5ZF/dh2paVI9qrQ/8P4rnEGjmPkHD8LbOpQHKUOhSHMz2r/v2A=="
}
}
)
print("Extension is created")
Console:
I executed the above code it created log-analytics extension successfully in my environment.
Portal:
Reference:
azure-content/log-analytics-azure-vm-extension.md at master · uglide/azure-content (github.com).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论