簡體   English   中英

如何使用 Azure 中的 Python SDK 將 LogAnalyticsWorkSpace 擴展添加到 VM

[英]how to add LogAnalyticsWorkSpace extension to VM using Python SDK in Azure

我在 azure 中創建了 Log Analytics 工作區。現在想使用 Python sdk 將其作為 VM 的擴展附加。我已按照文檔進行操作並收到如下錯誤。

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

在此處輸入圖像描述

我在我的環境中進行了嘗試,並使用 python 成功地為 VM 創建了 Log-Analytics-WorkSpace 擴展:

代碼:

我嘗試使用ComputeManagementClientDefaultAzureCredential方法來begin_create_or_update。 確保參數和資源正確 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")

安慰:

我執行了上面的代碼,它在我的環境中成功創建了日志分析擴展。

在此處輸入圖像描述

門戶網站:

在此處輸入圖像描述

參考:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM