如何解决在 Bitbucket 管道中使用 Terraform 收到错误“无效的旧提供程序地址”
我正在尝试使用 terraform v0.14.3 部署一个 bitbucket 管道,以在谷歌云中创建资源。管道失败并出现错误:
错误:旧版提供商地址无效 此配置或其关联状态指的是不合格的提供者 “谷歌”。 您必须先完成 Terraform 0.13 升级过程,然后才能升级到更高版本 版本。
我们将 terraform 的本地版本更新为 v.0.13.0,然后运行:terraform 0.13upgrade
,如本指南中所述:https://www.terraform.io/upgrade-guides/0-13.html。生成的 versions.tf 文件需要 terraform 版本 >=0.13,我们所需的提供程序块现在如下所示:
terraform {
backend "gcs" {
bucket = "some-bucket"
prefix = "terraform/state"
credentials = "key.json" #this is just a bitbucket pipeline variable
}
required_providers {
google = {
source = "hashicorp/google"
version = "~> 2.20.0"
}
}
}
provider "google" {
project = var.project_ID
credentials = "key.json"
region = var.project_region
}
在启动 bitbucket 管道时,我们仍然遇到相同的错误。有谁知道如何克服这个错误?提前致谢。
解决方法
说明
这是因为 Terraform 一次仅支持从一项主要功能升级而来的升级。您的情况中较旧的状态文件很可能是使用 0.13 版本之前的版本编写的,如果您没有使用 0.13 版本运行 terraform apply
,则此错误是意料之中的,因为到 0.13 的升级未完成。如果您运行它,它可能已经解决了(如上面的答案所述)。
解决方案
- 此时(即使用 terraform 0.14.x)您可以使用
replace-provider
命令
terraform state replace-provider "registry.terraform.io/-/google" "hashicorp/google"
- 现在再次初始化,这应该负责安装提供程序:
terraform init
可以在 here 中找到有关该问题的更多信息。
,在我们的例子中,我们在 aws 上遇到了类似的错误
...
Error: Invalid legacy provider address
This configuration or its associated state refers to the unqualified provider
"aws".
解决的步骤是通过再次运行 terraform init
、检查警告并最终使用以下方法更新状态文件来确保语法已升级。
# update provider in state file
terraform state replace-provider -- -/aws hashicorp/aws
# reinit
terraform init
特定于操作问题,如果问题仍然存在,请验证从本地和管道对存储桶位置的访问。还要验证在管道中运行的 terraform 版本。根据配置,远程状态文件可能/无法更新。
,另外,我已经安装了 terraform 0.14.6,但状态似乎卡在 0.12 中。就我而言,我有 3 个参考文献被关闭,这篇文章帮助我确定了哪些参考文献(“州要求的提供者”中的所有条目在链接中都有 - 。https://github.com/hashicorp/terraform/issues/27615 我通过为每个关闭的条目运行 replace-provider 命令来纠正它,然后运行 terraform init。我注意到这样做并运行了 git diff,tfstate 已经更新,现在使用 0.14.x terraform 而不是我以前的 0.12.x。即
terraform providers
terraform state replace-provider registry.terraform.io/-/azurerm registry.terraform.io/hashicorp/azurerm
,
对我来说同样的问题。我跑了:
terraform providers
这给了我:
Providers required by configuration:
registry.terraform.io/hashicorp/google
Providers required by state:
registry.terraform.io/-/google
所以我跑了:
terraform state replace-provider registry.terraform.io/-/google registry.terraform.io/hashicorp/google
成功了。
,我的情况是这样的
Error: Invalid legacy provider address
This configuration or its associated state refers to the unqualified provider
"openstack".
You must complete the Terraform 0.13 upgrade process before upgrading to later
versions.
解决问题
remove the .terraform folder
执行以下命令
terraform state replace-provider -- -/openstack terraform-provider-openstack/openstack
执行此命令后,您将看到以下打印内容,输入 yes
Terraform will perform the following actions:
~ Updating provider:
- registry.terraform.io/-/openstack
+ registry.terraform.io/terraform-provider-openstack/openstack
Changing 11 resources:
openstack_compute_servergroup_v2.kubernetes_master
openstack_networking_network_v2.kube_router
openstack_compute_instance_v2.kubernetes_worker
openstack_networking_subnet_v2.internal
openstack_networking_subnet_v2.kube_router
data.openstack_networking_network_v2.external_network
openstack_compute_instance_v2.kubernetes_etcd
openstack_networking_router_interface_v2.internal
openstack_networking_router_v2.internal
openstack_compute_instance_v2.kubernetes_master
openstack_networking_network_v2.internal
Do you want to make these changes?
Only 'yes' will be accepted to continue.
Enter a value: yes
Successfully replaced provider for 11 resources.
,
当您在 TF13 下时,您是否至少为正在运行的项目应用了一次状态?
根据 TF 文档:https://www.terraform.io/upgrade-guides/0-14.html
0.14 中没有自动更新命令(单独)(就像 0.13 中那样)。升级的唯一方法是在将 TF13 移至 14 时在命令下至少强制项目状态一次。
您也可以在项目目录中尝试terraform init。
,我们今天在运营环境中遇到了类似的问题。我们成功完成了 terraform 0.13upgrade
命令。这确实引入了一个 versions.tf
文件。
但是,仍然无法使用此设置执行 terraform init
,并弹出以下错误:
Error: Invalid legacy provider address
状态文件中的进一步调查显示,对于某些资源,提供程序块未更新。因此,我们必须运行以下命令来完成升级过程。
terraform state replace-provider "registry.terraform.io/-/google" "hashicorp/google"
EDIT 部署到下一个环境表明这是由条件资源引起的。为了轻松启用/禁用某些资源,我们利用 count
属性并使用 0 或 1。对于带有 count = 0
的资源,未使用 Terraform 0.13 更改,提供程序未更新。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。