首页 > 解决方案 > Terraform:无法安装提供程序,与依赖锁定文件中的校验和不匹配

问题描述

我使用 CI 系统编译 terraform 提供程序并将它们捆绑到图像中,但是每次运行 terraform init 时,都会出现以下错误/失败。

│ Error: Failed to install provider
│ 
│ Error while installing rancher/rancher2 v1.13.0: the current package for
│ registry.terraform.io/rancher/rancher2 1.13.0 doesn't match any of the
│ checksums previously recorded in the dependency lock file

对我的提供程序文件中列出的所有提供程序重复此消息,如下所示:

terraform {
  required_version = ">= 0.13"

  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "2.55.0"
    }
    github = {
      source  = "integrations/github"
      version = "4.8.0"
    }
  }
...snip...
}

terraform hcl 锁定文件存储在 repo 中,只有当 repo 中存在锁定文件时,才会出现这些错误并且 terraform init 失败。可能是什么原因?

标签: terraform

解决方案


问题是我的本地工作站是使用 darwin 平台的 Mac,因此为 darwin 下载了所有提供程序,并将哈希值存储在该平台的锁定文件中。当在 Linux 上运行的 CI 系统运行时,它会尝试检索锁定文件中列出的提供程序,但校验和不匹配,因为它们使用不同的平台。

解决方案是在本地使用以下命令生成一个新的 terraform 依赖锁文件,所有平台都为 terraform,运行在不同平台上的其他系统将能够服从依赖锁文件。

terraform providers lock -platform=windows_amd64 -platform=darwin_amd64 -platform=linux_amd64

推荐阅读