如何在Terraform中使用多提供商版本?
只是想知道是否可以使用多个提供商版本? 例如,当使用Terraform Azure提供商部署资源时 资源的一部分是由Azurerm提供商版本2.55.0部署的,其REST与提供商版本2.99.0一起部署
吗?我尝试了下面的代码,但没有工作,
terraform {
required_version = ">= 0.12.29, < 2.0"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = ">=2.99.0"
# version = "~> 2.64"
}
azurermlatest = {
source = "hashicorp/azurerm"
version = "= 3.3.0"
}
}
}
│ Error: Failed to query available provider packages
│
│ Could not retrieve the list of available versions for provider hashicorp/azurermlatest: provider registry registry.terraform.io
│ does not have a provider named registry.terraform.io/hashicorp/azurermlatest
│
│ 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/azurermlatest, run the following command:
│ terraform providers
我知道Terraform将使用符合配置版本约束的最新版本的限制。但是在最新文档中,该语句已被删除,因此不确定现在是否有多个版本提供商
https://www.terraform.io/language/configuration-0-11/providers
更新:
清理缓存和后端后,错误消失了 但是多提供者仍然不起作用,代码和错误如下 有人会检查并提供帮助吗?
main.tf
terraform {
required_version = ">= 0.12.29, < 2.0"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = ">=2.99.0"
# version = "~> 2.64"
}
azurermlatest = {
source = "hashicorp/azurerm"
version = "= 3.3.0"
}
}
}
provider "azurerm" {
alias = "old"
features {}
}
provider "azurermlatest" {
alias = "latest"
features {}
}
module "data" {
source = "../modules"
rg_settings = var.rg_settings
vnet_settings = var.vnet_settings
nsg_settings = var.nsg_settings
lb_settings = var.lb_settings
vm_settings = var.vm_settings
avset_settings = var.avset_settings
providers = {
azurerm.old = azurerm.old
azurerm.latest = azurermlatest.latest
}
}
模块
locals {
lb_frontend_config_public_ip = {
ipa = {
name = "test"
resource_group_name = "test"
location = "eastasia"
allocation_method = "Static"
}
}
lb_frontend_config_public_ip_latest = {
ipa = {
name = "test2"
resource_group_name = "test"
location = "eastasia"
allocation_method = "Static"
}
}
}
resource "azurerm_public_ip" "pip" {
provider = azurerm.old
for_each = local.lb_frontend_config_public_ip
name = each.value.name
resource_group_name = each.value.resource_group_name
location = each.value.location
allocation_method = each.value.allocation_method
sku = lookup(each.value, "sku", "Standard")
sku_tier = lookup(each.value, "sku_tier", "Regional")
ip_version = lookup(each.value, "ip_version", "IPv4")
idle_timeout_in_minutes = lookup(each.value, "idle_timeout_in_minutes", null)
domain_name_label = lookup(each.value, "domain_name_label", null)
reverse_fqdn = lookup(each.value, "reverse_fqdn", null)
availability_zone = lookup(each.value, "availability_zone", null)
# edge_zone = lookup(each.value, "edge_zone", null)
tags = lookup(each.value, "tags", null)
public_ip_prefix_id = lookup(each.value, "public_ip_prefix_id", null)
ip_tags = lookup(each.value, "ip_tags", null)
}
resource "azurerm_public_ip" "piplatest" {
provider = azurerm.latest
for_each = local.lb_frontend_config_public_ip_latest
name = each.value.name
resource_group_name = each.value.resource_group_name
location = each.value.location
allocation_method = each.value.allocation_method
sku = lookup(each.value, "sku", "Standard")
sku_tier = lookup(each.value, "sku_tier", "Regional")
ip_version = lookup(each.value, "ip_version", "IPv4")
idle_timeout_in_minutes = lookup(each.value, "idle_timeout_in_minutes", null)
domain_name_label = lookup(each.value, "domain_name_label", null)
reverse_fqdn = lookup(each.value, "reverse_fqdn", null)
# availability_zone = lookup(each.value, "availability_zone", null)
zones = lookup(each.value, "availability_zone", null)
# edge_zone = lookup(each.value, "edge_zone", null)
tags = lookup(each.value, "tags", null)
public_ip_prefix_id = lookup(each.value, "public_ip_prefix_id", null)
ip_tags = lookup(each.value, "ip_tags", null)
}
错误:
Error: Unsupported argument
│
│ on ..\modules\load_balancer.tf line 63, in resource "azurerm_public_ip" "pip":
│ 63: availability_zone = lookup(each.value, "availability_zone", null)
│
│ An argument named "availability_zone" is not expected here.
似乎Terraform仍在使用提供商3.3.0,即使我将提供商设置为azurerm.old
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经尝试了您的代码,并且您的提供商块似乎已正确设置。
重现错误的唯一方法是是否已更新源以引用无效的路径:
因此,错误肯定来自其他地方。我建议您查看您的代码,也许清理缓存或
.terraform
文件夹以及任何其他临时文件,然后重新运行Terraform Init
和plan 再次。
参见此页面提供者最佳实践。
I've tried your code on my end and your providers block seems to be set correctly.
The only way to reproduce your error was if the source is updated to reference an invalid path:
So the error definitely comes from somewhere else. I'd suggest reviewing your code, maybe cleaning up the cache or the
.terraform
folder and any other temp files and re-runningterraform init
andplan
again.See this page for more information about the providers best practice.
您提供的整个概念和答案是完全错误的。
提供程序
示例中的阻止仅是因为两个约束:&gt; = 2.99.0
= 3.3.0
实际上等于<<<<代码> = 3.3.0 和Terraform正在下载它。
用
quired_providers
block命名同一提供商并不是一个好练习,实际上,Terraform会在使用Terraform Init
时提醒您:以前需要“ Azurerm”的本地名称“ Azurermlatest”。
如果您要执行不允许Terraform下载版本高于2.x的约束(如您所注释的那样),则会看到:
“无法检索提供者Hashicorp/azurerm的可用版本列表:否可用的发行版匹配给定约束2.99.0,3.3.0“
从理论上讲,您可以将
azure
提供商复制到内部的私人注册表,重命名并在每个资源名称中使用其名称,但这听起来很恐怖和错。更好的想法是将项目分为两个,并使用较旧的提供商版本运行“旧版”代码,以及带有新的提供商版本的新代码。您可以使用
remote_state
对象组合这两个项目()。Your entire concept and answers provided are totally wrong.
The
providers
block in your example works only because the two constraints:>=2.99.0
=3.3.0
are in fact equal to
=3.3.0
and terraform is downloading it.Naming the same provider with different key in
required_providers
block is NOT a good practice, and in fact Terraform will remind you of that while doingterraform init
:"Provider hashicorp/azurerm with the local name "azurermlatest" was previously required as "azurerm". A provider can only be required once within required_providers."
If you wold do the constraint that would not allow Terraform to download version higher than 2.x (like you have commented out), you would see the error of:
"Could not retrieve the list of available versions for provider hashicorp/azurerm: no available releases match the given constraints 2.99.0, 3.3.0"
In theory, you could copy the
azure
provider to an in-house private registry, rename it, and use its name in every resource name, but this sounds horrible and wrong.Better idea is to split the project into two and run the "legacy" code with older provider version, and the new code with fresh provider version. You can combine those two projects using
remote_state
objects (https://www.terraform.io/language/state/remote-state-data).