首页 > 解决方案 > Azure VM Scaleset 自定义脚本扩展不起作用 - 可能无法获取 VM 标识?

问题描述

我正在尝试使用自定义脚本扩展部署到我的虚拟机规模集,如下所示。

az vmss extension set --debug --name 'CustomScriptExtension' `
    --resource-group 'my-rg' `
    --publisher 'Microsoft.Compute'  `
    --version '1.9.5' `
    --vmss-name 'myvmss' `
    --settings '{\"commandToExecute\": \"powershell.exe ./download-package.ps1\", \"fileUris\": [\"https://[REDACTED].blob.core.windows.net/upload/download-package.ps1\"]}' `
    --protected-settings '{\"managedIdentity\": {\"objectId\": \"[REDACTED]\"}}'

运行时出现以下错误:

cli.azure.cli.core.azclierror:部署失败。相关 ID:73f4d16b-afe0-4373-8773-1d7dd7d26940。VM 在处理扩展“CustomScriptExtension”时报告失败。错误消息:“无法下载所有指定的文件。正在退出。错误消息:引发了 'Microsoft.WindowsAzure.GuestAgent.Plugins.CustomScriptHandler.Downloader.MsiNotFoundException' 类型的异常。”

有关故障排除的更多信息,请访问https://aka.ms/VMExtensionCSEWindowsTroubleshoot 部署失败。相关 ID:73f4d16b-afe0-4373-8773-1d7dd7d26940。VM 在处理扩展“CustomScriptExtension”时报告失败。错误消息:“无法下载所有指定的文件。正在退出。错误消息:引发了 'Microsoft.WindowsAzure.GuestAgent.Plugins.CustomScriptHandler.Downloader.MsiNotFoundException' 类型的异常。”

要下载的文件需要身份验证,因此我为规模集分配了系统分配的标识,并授予它在Storage Blob Data Reader托管 powershell 文件的存储帐户上的角色。

虚拟机上的自定义扩展日志表明它无法获取虚拟机的身份:

[7108+00000001] [11/20/2020 09:12:28.79] [INFO] Handler successfully enabled
[7108+00000001] [11/20/2020 09:12:28.80] [INFO] Loading configuration for sequence number 1
[7108+00000001] [11/20/2020 09:12:28.84] [INFO] HandlerSettings = ProtectedSettingsCertThumbprint: [REDACTED], ProtectedSettings: {[REDACTED]}, PublicSettings: {FileUris: [https://[REDACTED].blob.core.windows.net/upload/download-package.ps1], CommandToExecute: powershell.exe ./download-package.ps1}
[7108+00000001] [11/20/2020 09:12:29.26] [INFO] Downloading files specified in configuration...
[7108+00000001] [11/20/2020 09:12:30.90] [INFO] Attempting to get MSI from IMDS
[7108+00000001] [11/20/2020 09:12:31.04] [WARN] WebClient: non retryable error occurred System.Net.WebException: The remote server returned an error: (400) Bad Request.
   at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
   at System.Net.WebClient.DownloadString(Uri address)
   at Microsoft.WindowsAzure.GuestAgent.Plugins.MsiUtils.WebClient.<>c__DisplayClass3_0.<DownloadStringWithRetries>b__0()
   at Microsoft.WindowsAzure.GuestAgent.Plugins.MsiUtils.WebClientWithRetryAbstract.ActionWithRetries(Action action)
[7108+00000001] [11/20/2020 09:12:31.14] [ERROR] Unknown exception occurred while attempting to get MSI token System.Net.WebException: The remote server returned an error: (400) Bad Request.
   at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
   at System.Net.WebClient.DownloadString(Uri address)
   at Microsoft.WindowsAzure.GuestAgent.Plugins.MsiUtils.WebClient.<>c__DisplayClass3_0.<DownloadStringWithRetries>b__0()
   at Microsoft.WindowsAzure.GuestAgent.Plugins.MsiUtils.WebClientWithRetryAbstract.ActionWithRetries(Action action)
   at Microsoft.WindowsAzure.GuestAgent.Plugins.MsiUtils.WebClient.DownloadStringWithRetries(Uri address)
   at Microsoft.WindowsAzure.GuestAgent.Plugins.MsiUtils.MsiProvider.GetMsiHelper(NameValueCollection queries)
[7108+00000001] [11/20/2020 09:12:31.14] [INFO] Msi was not obtained

我可以通过从元数据端点检索身份令牌,Invoke-WebRequest -Method Get -Uri 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://management.azure.com/'这样似乎设置正确。

任何有关问题可能是什么或如何进一步诊断此问题的建议将不胜感激。

标签: azureazure-virtual-machineazure-vm-scale-set

解决方案


以下是您可以尝试的一些修复

  1. 托管标识的对象 ID 可能不正确。
  2. 还请将 commandToExecute 和 FileUris 移动到具有托管身份的受保护设置中。
  3. 如果要使用系统分配的托管标识,您不需要传递 clientId 或 objectID,更多信息在这里https://docs.microsoft.com/en-us/azure/virtual-machines/extensions/custom-script- windows#property-managedidentity

编辑:当您将 commandToExecute 和 fileUris 添加到受保护的设置时,请显式传递一个空的 json 对象作为设置。否则扩展会因重复设置而失败。


推荐阅读