如何使用Azure的Python SDK将LogAnalyticsWorkSpace扩展添加到虚拟机 (VM) 中

huangapple go评论112阅读模式
英文:

how to add LogAnalyticsWorkSpace extension to VM using Python SDK in Azure

问题

我已在Azure中创建了Log Analytics工作区。现在想要使用Python SDK将其附加为VM的扩展。我已按照文档进行操作,但出现以下错误。

发生类'azure.core.exceptions.HttpResponseError'。

请有人帮助我解决这个问题。

以下是我尝试使用计算管理客户端的方法。

  1. extension = compute_client.virtual_machine_extensions.begin_create_or_update(
  2. "MyRG",
  3. "MyVMName",
  4. "MicrosoftMonitoringAgent", ##VMExtension name
  5. {
  6. "type": "Microsoft.Compute/virtualMachines/extensions",
  7. "location": "eastus",
  8. "publisher": "Microsoft.EnterpriseCloud.Monitoring",
  9. "type_handler_version": "1.0",
  10. "auto_upgrade_minor_version": True,
  11. "settings": {
  12. "workspaceId": "XXXXX"
  13. },
  14. "protected_settings": {
  15. "workspaceKey": "XXXXX"
  16. }
  17. }
  18. ).Result()

如何使用Azure的Python SDK将LogAnalyticsWorkSpace扩展添加到虚拟机 (VM) 中

英文:

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.

  1. extension = compute_client.virtual_machine_extensions.begin_create_or_update(
  2. "MyRG",
  3. "MyVMName",
  4. "MicrosoftMonitoringAgent", ##VMExtension name
  5. {
  6. "type": "Microsoft.Compute/virtualMachines/extensions",
  7. "location": "eastus",
  8. "publisher": "Microsoft.EnterpriseCloud.Monitoring",
  9. "type_handler_version": "1.0",
  10. "auto_upgrade_minor_version": True,
  11. "settings": {
  12. "workspaceId": "XXXXX"
  13. },
  14. "protected_settings": {
  15. "workspaceKey": "XXXXX"
  16. }
  17. }
  18. ).Result()

如何使用Azure的Python SDK将LogAnalyticsWorkSpace扩展添加到虚拟机 (VM) 中

答案1

得分: 0

我在我的环境中尝试过,并成功使用Python创建了Log Analytics Workspace扩展到虚拟机:

代码:

我尝试使用**ComputeManagementClientDefaultAzureCredential方法来执行begin_create_or_update**。请确保参数和资源处于正确的状态。

  1. from azure.mgmt.compute import ComputeManagementClient
  2. from azure.identity import DefaultAzureCredential
  3. subscriptionid = "<subscription id>"
  4. credential = DefaultAzureCredential()
  5. compute_client = ComputeManagementClient(subscription_id=subscriptionid, credential=credential)
  6. extension = compute_client.virtual_machine_extensions.begin_create_or_update(
  7. resource_group_name="<resource group>",
  8. vm_name="<vname>",
  9. vm_extension_name="MicrosoftMonitoringAgent",
  10. extension_parameters={
  11. "publisher": "Microsoft.EnterpriseCloud.Monitoring",
  12. "location": "eastus",
  13. "type_handler_version": "1.0",
  14. "auto_upgrade_minor_version": True,
  15. "settings": {
  16. "workspaceId": "7c950e4f-5c9b-4205-ba79-278cd22bd220"
  17. },
  18. "protected_settings": {
  19. "workspaceKey": "TlLvMVn55RQEHtWBJUSvmbYyaVeev4srp1Vu5ZF/dh2paVI9qrQ/8P4rnEGjmPkHD8LbOpQHKUOhSHMz2r/v2A=="
  20. }
  21. }
  22. )
  23. print("扩展已创建")

控制台:

我执行了上面的代码,在我的环境中成功创建了Log Analytics扩展。

如何使用Azure的Python SDK将LogAnalyticsWorkSpace扩展添加到虚拟机 (VM) 中

门户:

如何使用Azure的Python SDK将LogAnalyticsWorkSpace扩展添加到虚拟机 (VM) 中

参考链接:

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.

  1. from azure.mgmt.compute import ComputeManagementClient
  2. from azure.identity import DefaultAzureCredential
  3. subscriptionid = &quot;&lt;subscription id&gt;&quot;
  4. credential = DefaultAzureCredential()
  5. compute_client=ComputeManagementClient(subscription_id=subscriptionid,credential=credential)
  6. extension = compute_client.virtual_machine_extensions.begin_create_or_update(
  7. resource_group_name=&quot;&lt;resource group&gt;&quot;,
  8. vm_name=&quot;&lt;vname&gt;&quot;,vm_extension_name=&quot;MicrosoftMonitoringAgent&quot;,extension_parameters=
  9. {
  10. &quot;publisher&quot;: &quot;Microsoft.EnterpriseCloud.Monitoring&quot;,
  11. &quot;location&quot;:&quot;eastus&quot;,
  12. &quot;type_handler_version&quot;: &quot;1.0&quot;,
  13. &quot;auto_upgrade_minor_version&quot;: True,
  14. &quot;settings&quot;: {
  15. &quot;workspaceId&quot;: &quot;7c950e4f-5c9b-4205-ba79-278cd22bd220&quot;
  16. },
  17. &quot;protected_settings&quot;: {
  18. &quot;workspaceKey&quot;: &quot;TlLvMVn55RQEHtWBJUSvmbYyaVeev4srp1Vu5ZF/dh2paVI9qrQ/8P4rnEGjmPkHD8LbOpQHKUOhSHMz2r/v2A==&quot;
  19. }
  20. }
  21. )
  22. print(&quot;Extension is created&quot;)

Console:

I executed the above code it created log-analytics extension successfully in my environment.

如何使用Azure的Python SDK将LogAnalyticsWorkSpace扩展添加到虚拟机 (VM) 中

Portal:

如何使用Azure的Python SDK将LogAnalyticsWorkSpace扩展添加到虚拟机 (VM) 中

Reference:

azure-content/log-analytics-azure-vm-extension.md at master · uglide/azure-content (github.com).

huangapple
  • 本文由 发表于 2023年1月9日 14:24:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75053790.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定