You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Terraform升级至1.5.6后执行plan遇Failed to load plugin schemas错误

Terraform升级后执行plan失败:无法加载azurerm/random插件schema问题

问题背景

  • 原有环境:使用Terraform 0.12 + AzureRM Provider 2.20部署Windows虚拟机及相关资源
  • 升级目标:Terraform 1.5.6 + AzureRM Provider 3.50.0,启用Azure Key Vault密钥轮换功能,同时用random模块生成VM密码
  • 异常现象:terraform init -upgrade执行成功,但terraform plan失败,报错无法加载azurerm和random插件的schema;日志中还出现内置terraform provider的schema不可用提示,已确认下载的插件具备执行权限

配置代码

Terraform块配置

terraform {
  required_version = "~> 1.5.6"
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "~> 3.50.0"
    }
  }
  backend "azurerm" {} #Backend variables are initialized through the secret and variable folders
}

AzureRM Provider配置

provider "azurerm" {
  # Whilst the `version` attribute is optional, we recommend pinning to a given version of the Provider
  #version         = "~> 3.50.0"
  tenant_id       = var.tenant_id
  subscription_id = var.subscription_id

  features {}
}

错误信息

│ Error: Failed to load plugin schemas
│ 
│ Error while loading schemas for plugin components: Failed to obtain
│ provider schema: Could not load the schema for provider
│ registry.terraform.io/-/azurerm: failed to instantiate provider
│ "registry.terraform.io/-/azurerm" to obtain schema: unavailable provider
│ "registry.terraform.io/-/azurerm"..
╵
╷
│ Error: Failed to load plugin schemas
│ 
│ Error while loading schemas for plugin components: Failed to obtain
│ provider schema: Could not load the schema for provider
│ registry.terraform.io/-/azurerm: failed to instantiate provider
│ "registry.terraform.io/-/azurerm" to obtain schema: unavailable provider
│ "registry.terraform.io/-/azurerm"..
╵
╷
│ Error: Failed to load plugin schemas
│ 
│ Error while loading schemas for plugin components: 2 problems:
│ 
│ - Failed to obtain provider schema: Could not load the schema for provider
│ registry.terraform.io/-/azurerm: failed to instantiate provider
│ "registry.terraform.io/-/azurerm" to obtain schema: unavailable provider
│ "registry.terraform.io/-/azurerm".
│ - Failed to obtain provider schema: Could not load the schema for provider
│ registry.terraform.io/-/random: failed to instantiate provider
│ "registry.terraform.io/-/random" to obtain schema: unavailable provider
│ "registry.terraform.io/-/random"..

Trace日志片段

2023-09-06T19:57:41.714Z [TRACE] AttachSchemaTransformer: attaching resource schema to azurerm_key_vault_key.this
2023-09-06T19:57:41.714Z [TRACE] AttachSchemaTransformer: attaching resource schema to azurerm_windows_virtual_machine.windows_vms
2023-09-06T19:57:41.714Z [TRACE] AttachSchemaTransformer: attaching resource schema to azurerm_network_interface.windows_nics
2023-09-06T19:57:41.714Z [TRACE] AttachSchemaTransformer: attaching resource schema to azurerm_virtual_machine_data_disk_attachment.this
2023-09-06T19:57:41.714Z [TRACE] terraform.contextPlugins: Initializing provider "terraform.io/builtin/terraform" to read its schema
2023-09-06T19:57:41.714Z [ERROR] AttachSchemaTransformer: No provider config schema available for provider["terraform.io/builtin/terraform"]
2023-09-06T19:57:41.714Z [TRACE] AttachSchemaTransformer: attaching resource schema to azurerm_backup_protected_vm.this
2023-09-06T19:57:41.714Z [TRACE] AttachSchemaTransformer: attaching resource schema to azurerm_key_vault_secret.this
2023-09-06T19:57:41.714Z [TRACE] AttachSchemaTransformer: attaching resource schema to data.terraform_remote_state.loadbalancer
2023-09-06T19:57:41.714Z [TRACE] AttachSchemaTransformer: attaching resource schema to data.terraform_remote_state.applicationsecuritygroup
2023-09-06T19:57:41.714Z [TRACE] AttachSchemaTransformer: attaching provider config schema to provider["registry.terraform.io/hashicorp/azurerm"]
2023-09-06T19:57:42.582Z [TRACE] AttachSchemaTransformer: attaching resource schema to azurerm_key_vault_access_policy.this (expand)
2023-09-06T19:57:42.582Z [TRACE] AttachSchemaTransformer: attaching resource schema to data.azurerm_backup_policy_vm.this (expand)
2023-09-06T19:57:42.582Z [TRACE] AttachSchemaTransformer: attaching resource schema to data.terraform_remote_state.storage (expand)
2023-09-06T19:57:42.582Z [ERROR] AttachSchemaTransformer: No provider config schema available for provider["terraform.io/builtin/terraform"]
2023-09-06T19:57:42.582Z [TRACE] AttachSchemaTransformer: attaching resource schema to azurerm_key_vault_key.this (expand)
2023-09-06T19:57:42.582Z [TRACE] AttachSchemaTransformer: attaching resource schema to azurerm_disk_encryption_set.this (expand)

.terraform目录结构

./.terraform:
total 20
drwxr-xr-x 3 AzDevOps AzDevOps 4096 Sep  6 19:46 .
drwxr-xr-x 6 AzDevOps AzDevOps 4096 Sep  6 19:46 ..
-rw-r--r-- 1 AzDevOps AzDevOps    5 Sep  6 19:46 environment
drwxr-xr-x 3 AzDevOps AzDevOps 4096 Sep  6 19:46 providers
-rw-r--r-- 1 AzDevOps AzDevOps 1398 Sep  6 19:46 terraform.tfstate

./.terraform/providers:
total 12
drwxr-xr-x 3 AzDevOps AzDevOps 4096 Sep  6 19:46 .
drwxr-xr-x 3 AzDevOps AzDevOps 4096 Sep  6 19:46 ..
drwxr-xr-x 3 AzDevOps AzDevOps 4096 Sep  6 19:46 registry.terraform.io

./.terraform/providers/registry.terraform.io:
total 12
drwxr-xr-x 3 AzDevOps AzDevOps 4096 Sep  6 19:46 .
drwxr-xr-x 3 AzDevOps AzDevOps 4096 Sep  6 19:46 ..
drwxr-xr-x 4 AzDevOps AzDevOps 4096 Sep  6 19:46 hashicorp

./.terraform/providers/registry.terraform.io/hashicorp:
total 16
drwxr-xr-x 4 AzDevOps AzDevOps 4096 Sep  6 19:46 .
drwxr-xr-x 3 AzDevOps AzDevOps 4096 Sep  6 19:46 ..
drwxr-xr-x 3 AzDevOps AzDevOps 4096 Sep  6 19:46 azurerm
drwxr-xr-x 3 AzDevOps AzDevOps 4096 Sep  6 19:46 random

./.terraform/providers/registry.terraform.io/hashicorp/azurerm:
total 12
drwxr-xr-x 3 AzDevOps AzDevOps 4096 Sep  6 19:46 .
drwxr-xr-x 4 AzDevOps AzDevOps 4096 Sep  6 19:46 ..
drwxr-xr-x 3 AzDevOps AzDevOps 4096 Sep  6 19:46 3.50.0

./.terraform/providers/registry.terraform.io/hashicorp/azurerm/3.50.0:
total 12
drwxr-xr-x 3 AzDevOps AzDevOps 4096 Sep  6 19:46 .
drwxr-xr-x 3 AzDevOps AzDevOps 4096 Sep  6 19:46 ..
drwxr-xr-x 2 AzDevOps AzDevOps 4096 Sep  6 19:46 linux_amd64

./.terraform/providers/registry.terraform.io/hashicorp/azurerm/3.50.0/linux_amd64:
total 187464
drwxr-xr-x 2 AzDevOps AzDevOps      4096 Sep  6 19:46 .
drwxr-xr-x 3 AzDevOps AzDevOps      4096 Sep  6 19:46 ..
-rwxr-xr-x 1 AzDevOps AzDevOps 191954944 Sep  6 19:46 terraform-provider-azurerm_v3.50.0_x5

./.terraform/providers/registry.terraform.io/hashicorp/random:
total 12
drwxr-xr-x 3 AzDevOps AzDevOps 4096 Sep  6 19:46 .
drwxr-xr-x 4 AzDevOps AzDevOps 4096 Sep  6 19:46 ..
drwxr-xr-x 3 AzDevOps AzDevOps 4096 Sep  6 19:46 3.5.1

./.terraform/providers/registry.terraform.io/hashicorp/random/3.5.1:
total 12
drwxr-xr-x 3 AzDevOps AzDevOps 4096 Sep  6 19:46 .
drwxr-xr-x 3 AzDevOps AzDevOps 4096 Sep  6 19:46 ..
drwxr-xr-x 2 AzDevOps AzDevOps 4096 Sep  6 19:46 linux_amd64

./.terraform/providers/registry.terraform.io/hashicorp/random/3.5.1/linux_amd64:
total 12904
drwxr-xr-x 2 AzDevOps AzDevOps     4096 Sep  6 19:46 .
drwxr-xr-x 3 AzDevOps AzDevOps     4096 Sep  6 19:46 ..
-rwxr-xr-x 1 AzDevOps AzDevOps 13205504 Sep  6 19:46 terraform-provider-random_v3.5.1_x5

解决方案

1. 补充random provider的声明

报错中registry.terraform.io/-/random说明Terraform找不到该provider的正确命名空间,需在required_providers中添加random的声明:

terraform {
  required_version = "~> 1.5.6"
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "~> 3.50.0"
    }
    random = {
      source = "hashicorp/random"
      version = "~> 3.5.1"
    }
  }
  backend "azurerm" {}
}

2. 清理插件缓存并重新初始化

  • 删除本地的.terraform目录和.terraform.lock.hcl文件
  • 重新执行terraform init -upgrade,强制重新下载并初始化所有provider插件

3. 验证Azure Pipelines代理环境

  • 确认代理使用的是Linux amd64架构(与插件目录的linux_amd64匹配)
  • 检查代理的网络策略,确保没有限制Terraform插件的初始化请求(如出站端口、防火墙规则)

4. 解决内置terraform provider警告

日志中的No provider config schema available是Terraform 1.5.x早期版本的已知问题,升级到Terraform 1.5.7及以上补丁版本即可消除该警告,此警告不影响核心功能,但建议升级以获得更好的稳定性。

原因分析

  • 核心问题:未在required_providers中声明random provider,导致Terraform无法识别其正确的源地址,出现registry.terraform.io/-/random的无效命名空间引用
  • 次要问题:旧插件缓存可能存在状态残留,清理后重新初始化可彻底解决插件加载异常
  • 内置provider警告:属于版本兼容性问题,升级Terraform即可修复

内容的提问来源于stack exchange,提问作者thetechguy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 13:42:01