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

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

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()

如何使用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.

   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()

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

答案1

得分: 0

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

代码:

我尝试使用**ComputeManagementClientDefaultAzureCredential方法来执行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的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.

from azure.mgmt.compute import ComputeManagementClient
from azure.identity import DefaultAzureCredential


subscriptionid = &quot;&lt;subscription id&gt;&quot;
credential = DefaultAzureCredential()
compute_client=ComputeManagementClient(subscription_id=subscriptionid,credential=credential)
extension = compute_client.virtual_machine_extensions.begin_create_or_update(
   resource_group_name=&quot;&lt;resource group&gt;&quot;,
        vm_name=&quot;&lt;vname&gt;&quot;,vm_extension_name=&quot;MicrosoftMonitoringAgent&quot;,extension_parameters=
        {
             &quot;publisher&quot;: &quot;Microsoft.EnterpriseCloud.Monitoring&quot;,
             &quot;location&quot;:&quot;eastus&quot;,       
             &quot;type_handler_version&quot;: &quot;1.0&quot;,            
             &quot;auto_upgrade_minor_version&quot;: True,
             &quot;settings&quot;: {
               &quot;workspaceId&quot;: &quot;7c950e4f-5c9b-4205-ba79-278cd22bd220&quot;
                },
             &quot;protected_settings&quot;: {
                &quot;workspaceKey&quot;: &quot;TlLvMVn55RQEHtWBJUSvmbYyaVeev4srp1Vu5ZF/dh2paVI9qrQ/8P4rnEGjmPkHD8LbOpQHKUOhSHMz2r/v2A==&quot;
                }               
        }
    )   

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:

确定