有谁知道是否可以使用多个提供程序版本的 Terraform 脚本?
例如 azurerm 2.0.0 版用于创建一个资源,而 1.4.0 版用于另一个?
我尝试指定提供者,如此处所述:https://www.terraform.io/docs/configuration/providers.html
但是,它似乎不起作用,因为它试图解析同时满足 1.4.0 和 2.0.0 的单个提供程序。
它错误如下:
No provider "azurerm" plugins meet the constraint "=1.4.0,=2.0.0".
我问这个是因为我们有一个大型的 Terraform 代码库,如果可行,我想逐位迁移。
曾经有人提出过类似的问题,这里是:Terraform: How to install multiple versions of provider plugins?
但它没有得到有效的答案
最佳答案
如何使用同一 Terraform 提供程序的多个版本
这使我们能够从 helm2 平稳过渡到 helm3,同时使新部署能够立即使用 helm3,从而减少技术债务的积累。
当然,您可以对大多数提供商执行相同操作
我们是如何解决这个问题的
因此,我们的想法是下载我们提供程序的特定版本(在我的情况下为 helm 0.10.6)并将其移动到 terraform 默认使用的文件系统镜像之一。关键部分是我们的插件二进制文件的重命名。在 zip 我们可以找到 terraform-provider-helm_v0.10.6
,但我们将其重命名为 terraform-provider-helm2_v0.10.6
PLUGIN_PATH=/usr/share/terraform/plugins/registry.terraform.io/hashicorp/helm2/0.10.6/linux_amd64
mkdir -p $PLUGIN_PATH
curl -sLo_ 'https://releases.hashicorp.com/terraform-provider-helm/0.10.6/terraform-provider-helm_0.10.6_linux_amd64.zip'
unzip -p _ 'terraform-provider-helm*' > ${PLUGIN_PATH}/terraform-provider-helm2_v0.10.6
rm _
chmod 755 ${PLUGIN_PATH}/terraform-provider-helm2_v0.10.6
然后当我们声明我们的两个提供者插件时我们可以使用
hashicorp/helm2
从文件系统镜像中下载插件,让terraform直接下载最新的hashicorp/helm
提供者,使用 helm3terraform {
required_providers {
helm2 = {
source = "hashicorp/helm2"
}
helm = {
source = "hashicorp/helm"
version = ">= 2.0.0"
}
}
}
# you will find the doc here https://registry.terraform.io/providers/hashicorp/helm/0.10.6/docs
provider "helm2" {
install_tiller = false
namespace = "kube-system"
kubernetes {
...
}
}
# you will find the doc at latest version https://registry.terraform.io/providers/hashicorp/helm/latest/docs
provider "helm" {
kubernetes {
...
}
}
在初始化 terraform 的时候,你会发现- Finding latest version of hashicorp/helm...
- Finding latest version of hashicorp/helm2...
- Installing hashicorp/helm v2.0.2...
- Installed hashicorp/helm v2.0.2 (signed by HashiCorp)
- Installing hashicorp/helm2 v0.10.6...
- Installed hashicorp/helm2 v0.10.6 (unauthenticated)
使用它从这一点来看,它非常简单。默认情况下,helm 资源将在 v2.0.2 中选择我们更新的 helm 提供程序。您必须明确使用
provider = helm2
对于旧资源(在我们的例子中是 helm_repository
和 helm_releases
)。迁移后,您可以将其删除以使用默认的 helm 提供程序。
关于terraform - 具有 Terraform 的多个提供程序版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61774501/