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

OpenTofu执行tofu init时查询hashicorp/azure Provider失败求助

OpenTofu部署Azure资源时tofu init报错:找不到hashicorp/azure provider

运行tofu init时出现如下错误:

Initializing the backend...

Initializing provider plugins...
- Finding hashicorp/random versions matching "~> 3.0"...
- Finding latest version of hashicorp/azure...
- Finding hashicorp/azurerm versions matching "~> 4.0"...
- Installing hashicorp/random v3.6.3...
- Installed hashicorp/random v3.6.3 (signed, key ID 0C0AF313E5FD9F80)
- Installing hashicorp/azurerm v4.19.0...
- Installed hashicorp/azurerm v4.19.0 (signed, key ID 0C0AF313E5FD9F80)

Providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://opentofu.org/docs/cli/plugins/signing/
╷
│ Error: Failed to query available provider packages
│ 
│ Could not retrieve the list of available versions for provider hashicorp/azure: provider registry
│ registry.opentofu.org does not have a provider named registry.opentofu.org/hashicorp/azure
│ 
│ All modules should specify their required_providers so that external consumers will get the correct providers when
│ using a module. To see which modules are currently depending on hashicorp/azure, run the following command:
│     tofu providers
│ 
│ If you believe this provider is missing from the registry, please submit a issue on the OpenTofu Registry
│ https://github.com/opentofu/registry/issues/new/choose

已尝试的操作:

  • 删除锁文件后重新运行tofu init
  • 通过.config文件和设置环境变量TF_REGISTRY_DISCOVERY=1指定使用Terraform注册表
  • 将azurerm provider版本更新至~>4.0
  • 搜索代码中所有hashicorp/azure引用,未找到

tofu providers输出:

Providers required by configuration:
.
├── provider[registry.opentofu.org/hashicorp/azurerm] ~> 4.0
├── provider[registry.opentofu.org/hashicorp/random] ~> 3.0
└── provider[registry.opentofu.org/hashicorp/azure]

当前provider配置文件内容:

terraform {
  required_providers {    
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.0"
    }
    random = {
      source  = "hashicorp/random"
      version = "~> 3.0"
    }
  }
}

provider "azurerm" {
  features {
  }
}

解决思路

  1. 定位隐藏的hashicorp/azure依赖

    • 运行tofu providers -v查看详细依赖树,找到触发hashicorp/azure请求的模块。部分嵌套模块可能未显式声明required_providers,默认引用了已废弃的旧版azure provider(而非当前使用的azurerm)。
    • 遍历所有.tf文件(包括子目录和模块目录),搜索provider "azure"或required_providers中的azure条目,排查遗漏的旧版provider引用。
  2. 强制重定向hashicorp/azure到azurerm
    在根模块的terraform块中添加provider_installation配置,将已废弃的hashicorp/azure映射到官方维护的hashicorp/azurerm:

    terraform {
      required_providers {    
        azurerm = {
          source  = "hashicorp/azurerm"
          version = "~> 4.0"
        }
        random = {
          source  = "hashicorp/random"
          version = "~> 3.0"
        }
      }
    
      provider_installation {
        override "hashicorp/azure" {
          to = "hashicorp/azurerm"
        }
      }
    }
    
  3. 清理本地依赖缓存

    • 删除OpenTofu本地缓存目录(默认路径为~/.opentofu.d/plugins),避免缓存中的旧provider信息干扰初始化流程,之后重新运行tofu init。
  4. 检查并升级第三方模块

    • 若使用了第三方模块,确认模块版本是否已移除对hashicorp/azure的依赖,尝试升级到最新兼容版本。
    • 若无法升级模块,可在模块调用时显式声明required_providers,强制模块使用azurerm:
      module "example_module" {
        source  = "example/module/azure"
        version = "x.x.x"
      
        providers = {
          azurerm = azurerm
        }
      
        required_providers {
          azurerm = {
            source  = "hashicorp/azurerm"
            version = "~> 4.0"
          }
        }
      }
      

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 07:54:50