- 网络安全
- 认证鉴权
- 运维
- 后端
【免费下载链接】teleport
The easiest, and most secure way to access and protect all of your infrastructure.
Teleport 在对 Azure 虚拟机执行自动发现(Auto Discovery)与自动注册(Auto Enrollment)时,需要借助 Azure VM 代理(Azure VM Agent)通过扩展机制在目标主机上执行安装脚本。当这一链路中断时,Teleport 会生成一条名为 "VM agent not available" 的用户任务(User Task)提示,指出"Teleport 无法访问 Azure VM 代理以在该 VM 上运行注册命令,Azure 报告扩展操作被禁止(extension operations are disallowed)"。本文以仓库中的官方排查文档 azure-vm-agent-not-available.md 为主体,结合 Teleport 源码中 Run Command 的执行链路与错误分类逻辑,系统讲解该错误的成因、验证手段与修复步骤,帮助你快速定位并解决 Azure VM 自动注册失败问题。
错误出现的场景:Azure VM 自动注册的完整链路
Teleport 的 Azure 自动发现功能会扫描订阅下符合条件的虚拟机,并尝试为每台 VM 自动安装并注册 Teleport Agent。安装过程的核心依赖是Azure VM Run Command——Teleport 会在目标 VM 上创建一条名为teleport-install的 Run Command 来执行注册脚本。
从源码 lib/cloud/azure/vm.go 可以看到这条执行链路的关键实现:
runCommandName = "teleport-install"固定了安装命令的名称(vm.go);RunCommandClient.Run会构造VirtualMachineRunCommand,设置脚本来源与TimeoutInSeconds超时,然后通过 Azure SDK 的VirtualMachineRunCommandsClient发起创建(vm.go);- 执行过程中以 30 秒为周期轮询命令完成状态(
runCommandResultPollingFrequency,vm.go)。
而在真正发起 Run Command 之前,Teleport 会先调用checkVMStatus检查 VM 的运行状态与 Agent 就绪状态(vm.go)。该函数通过 Azure Instance View(InstanceViewTypesInstanceView)读取 VM 的实例视图信息,其判断逻辑为:
- VM 处于运行状态且 Agent 已就绪(
AgentIsReady)→ 放行,继续执行 Run Command; - VM 未运行 → 返回
NewVMNotRunningError(对应另一条错误 "VM not running"); - VM 在运行但Agent 未就绪→ 返回
NewVMAgentNotAvailableError,即本文讨论的错误。
因此,"VM agent not available" 本质上表示:VM 电源状态正常,但 Azure 报告 VM Agent 处于不可用状态,Teleport 无法借助 Agent 的扩展通道在 VM 上执行注册命令。
Teleport 如何将底层错误转换为用户任务
这个底层错误并不会直接以原始报错形式抛给用户。在自动注册失败后,Teleport 的服务端会对错误进行分类,并生成一条结构化的"用户任务"(User Task),在 Web UI 中向管理员展示问题摘要、详细说明与修复指引。
错误分类逻辑位于 lib/srv/discovery/azure_vm_error_parser.go 的classifyAzureVMEnrollmentError函数中(azure_vm_error_parser.go):
switch { case errors.Is(err, &azure.VMNotRunningError{}): return usertasks.AutoDiscoverAzureVMIssueVMNotRunning case errors.Is(err, &azure.VMAgentNotAvailableError{}): return usertasks.AutoDiscoverAzureVMIssueVMAgentNotAvailable case trace.IsAccessDenied(err) && strings.Contains(err.Error(), "runCommands"): return usertasks.AutoDiscoverAzureVMIssueMissingRunCommandsPermission default: return usertasks.AutoDiscoverAzureVMIssueEnrollmentError }当分类结果为AutoDiscoverAzureVMIssueVMAgentNotAvailable(字符串值为"azure-vm-agent-not-available",定义见 api/types/usertasks/object.go)时,Web UI 便会加载 descriptions/azure-vm-agent-not-available.md 作为该任务的标题与修复说明。
描述文档的加载机制在 lib/usertasks/descriptions.go 中实现:所有descriptions/*.md文件通过//go:embed嵌入二进制,loadIssueTitleDescription将文档第一行的# 标题提取为任务标题,其余部分作为详细描述(descriptions.go)。仓库中的 descriptions_test.go 通过TestAllDescriptions遍历DiscoverAzureVMIssueTypes等全部问题类型,确保每个类型都有对应的标题与描述文档。
该文档即本篇指南的主体内容,其核心结论是:这个错误通常由两类原因引起——VM Agent 未安装或运行不健康,或扩展操作(extension operations)被禁用。
原因一:VM Agent 未安装或不健康
现象与原理
Azure VM Agent(Linux 上为 waagent)是 Azure 平台与虚拟机内部通信的桥梁,Run Command、扩展安装等功能都必须通过它来执行。如果 Agent 未安装、被卸载,或安装后处于异常状态(进程未运行、版本过旧、与平台通信失败),Azure 就无法把 Run Command 下发到 VM 内部,Teleport 的注册脚本自然无法执行,进而触发VMAgentNotAvailableError。
在checkVMStatus的实现中,Agent 状态直接来自 VM 的 Instance View(vm.go):
if getVMResp.Properties != nil && getVMResp.Properties.InstanceView != nil { instanceView := getVMResp.Properties.InstanceView return checkStatus(instanceView.VMAgent, instanceView.Statuses) }只有当VMAgent的实例视图显示 Agent 已就绪时,Teleport 才会继续执行注册命令;否则视为 Agent 不可用。
排查与修复步骤
- 登录 Azure 门户,进入目标 VM 的 "Settings → Extensions + applications" 页面,或在 Azure CLI 中查看 Agent 状态:
az vm get-instance-view \ --resource-group <resource-group> \ --name <vm-name> \ --query "instanceView.vmAgent" - 确认 VM Agent 状态为
Ready、Provisioned,且版本号非空。 - 若 Agent 未安装或状态异常:
- Linux VM:参考 Azure 官方文档中关于 Linux VM Agent(waagent)的安装与更新说明,在 VM 内重新安装或重启 Agent 服务;
- Windows VM:确认 Windows Guest Agent 服务存在且处于运行状态。
- 确保 VM 有正常的出站网络连接,能够访问 Azure 平台的控制平面(Agent 需要与平台保持心跳通信)。
- 修复后,可在 Teleport 中重新触发自动注册(或等待下一次发现周期),确认用户任务是否消失。
原因二:扩展操作被禁用(allowExtensionOperations = false)
现象与原理
Azure VM 的osProfile.allowExtensionOperations属性控制是否允许对该 VM 执行扩展类操作。Run Command 在 Azure 的权限模型中被归类为扩展操作,因此当该属性为false时,Azure 会直接拒绝 Teleport 发起的 Run Command 请求,即使 VM Agent 本身完全健康。这正是错误描述中"Azure reported that extension operations are disallowed"的含义。
检查方法(Azure CLI)
使用官方排查文档提供的命令查询当前值(azure-vm-agent-not-available.md):
az vm show \ --resource-group <resource-group> \ --name <vm-name> \ --query "osProfile.allowExtensionOperations"- 输出为
true:扩展操作允许,问题应继续向原因一或其他方向排查; - 输出为
false或null:扩展操作被禁用,需要开启后才能让 Run Command 生效。
修复方法
- 通过 Azure CLI 更新 VM 配置(需要在
az login后执行):az vm update \ --resource-group <resource-group> \ --name <vm-name> \ --set osProfile.allowExtensionOperations=true - 通过 ARM 模板 / Bicep / Terraform:在 VM 资源的
osProfile中显式设置allowExtensionOperations: true,然后重新部署。 - 通过 Azure 门户:在 VM 的 "Extensions + applications" 页面尝试安装或管理扩展;若页面提示扩展操作被禁用,则需要在 VM 配置中启用该属性。
需要注意,该属性通常在 VM 创建时通过osProfile设定。若 VM 由组织模板统一创建且该属性被置为false,可能需要修改创建模板或走变更流程,而不是直接对单个 VM 打补丁。
原因三:Azure Policy 强制禁用了扩展操作
错误描述文档的最后一句特别强调:"如果某个策略(policy)禁用了扩展操作,Run Command 将无法工作。" 这是原因二在组织级层面的常见体现:
- 企业可能通过 Azure Policy 强制将 VM 的
Microsoft.Compute/virtualMachines/extensions或 Run Command 相关操作设为 Deny; - 此时即便单台 VM 的
osProfile.allowExtensionOperations为true,策略仍会拒绝扩展类操作,Run Command 同样无法执行。
排查建议
- 在 Azure 门户中查看该 VM 所属订阅/资源组上生效的 Policy 与 Initiative(尤其关注 Compute 相关的 Deny 策略);
- 通过
az policy相关命令列出策略分配与合规状态,确认是否存在命中该 VM 的拒绝策略; - 若命中组织策略,需要与基础设施团队协调,为 Teleport 自动注册场景申请豁免(exemption)或调整策略范围,而不能绕过策略直接执行。
完整排查清单与相邻问题
综合以上分析,当你在 Teleport 中看到 "VM agent not available" 用户任务时,可按以下顺序排查:
| 步骤 | 检查项 | 判定依据 | 处理方式 |
|---|---|---|---|
| 1 | VM 电源状态 | Instance View 显示 VM 处于 Running | 若未运行,属于 "VM not running" 问题,先启动 VM |
| 2 | VM Agent 状态 | az vm get-instance-view中vmAgent为 Ready | 未就绪则重装/重启 Agent,检查出站网络 |
| 3 | allowExtensionOperations | az vm show --query "osProfile.allowExtensionOperations"为true | 为false时按上文方法开启 |
| 4 | Azure Policy | 订阅/资源组上是否存在 Deny 扩展操作的策略 | 协调豁免或调整策略 |
| 5 | 集成身份权限 | 集成所用的 Azure 身份是否具备 Run Command 权限 | 若缺失,属于 Missing Run Command permissions 问题 |
其中第 5 步的权限问题与本文主题紧密相邻:即使 Agent 健康且扩展操作允许,若 Teleport 集成身份缺少Microsoft.Compute/virtualMachines/runCommand/action等权限,注册同样会失败,但会以另一条用户任务(azure-vm-missing-run-commands-permission)呈现,其完整权限清单可参考 azure-vm-missing-run-commands-permission.md。
另外,Teleport 在生成 Azure VM 相关用户任务时,还会为每台失败 VM 附带一个直达 Azure 门户的ResourceURL(格式为https://portal.azure.com/#resource<resource-id>,实现在 lib/usertasks/urls.go,并有 urls_test.go 中的TestAzureVMURLs覆盖验证)。你可以直接从用户任务中跳转到对应 VM 的 Azure 门户页面,快速执行上述检查。
小结
"VM agent not available" 是 Teleport Azure VM 自动注册链路中一个可预期、可排查的失败类型:Teleport 通过checkVMStatus主动探测 VM Agent 就绪状态(vm.go),将VMAgentNotAvailableError分类为azure-vm-agent-not-available(azure_vm_error_parser.go),并通过 User Task 机制把官方排查文档 azure-vm-agent-not-available.md 呈现给管理员。修复的关键在于:保证 VM Agent 健康运行、确保osProfile.allowExtensionOperations为true、确认没有策略拦截扩展操作,同时留意集成身份的 Run Command 权限。逐项核对上述检查清单后,绝大多数注册失败场景都能得到解决;若仍未恢复,可结合服务端日志中classifyAzureVMEnrollmentError落入默认分支(azure-vm-enrollment-error)时的原始错误信息继续深入分析。
- 网络安全
- 认证鉴权
- 运维
- 后端
【免费下载链接】teleport
The easiest, and most secure way to access and protect all of your infrastructure.
相关推荐
Teleport Azure VM 自动注册故障排查:集成身份缺少 Run Command 权限
Teleport Azure VM 自动注册故障排查:集成身份缺少 Run Command 权限 Teleport 在 Azure VM 自动注册(auto e
网络安全认证鉴权运维后端Bend 报 CUDA not available 错误怎么排查?
Bend 报 CUDA not available 错误怎么排查? 在 Bend 上用 bend run cu <file.bend 启动 CUDA 解释器时,
编程语言编译器语言运行时高性能计算3个技巧快速掌握CryptoMiniSat:高效解决约束满足问题的终极指南
3个技巧快速掌握CryptoMiniSat:高效解决约束满足问题的终极指南 CryptoMiniSat是一个先进的增量SAT求解器,专门用于解决布尔可满足性问题
网络安全认证鉴权运维后端
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考