我有以下三个文件: main.tf、variables.tf和dev.auto.tfvars
main.tf片段
module "sql_vms" {
source = "git::git@github.com:xxxxxxxxxxxx/terraform-modules//azure/"
rg_name = var.resource_group_name
location = module.resource_group.external_rg_location
vnet_name = var.virtual_network_name
subnet_name = var.sql_subnet_name
app_nsg = var.application_nsg
vm_count = var.count_vm
base_hostname = var.sql_host_basename
sto_acc_suffix = var.storage_account_suffix
vm_size = var.virtual_machine_size
vm_publisher = var.virtual_machine_image_publisher
vm_offer = var.virtual_machine_image_offer
vm_sku = var.virtual_machine_image_sku
vm_img_version = var.virtual_machine_image_version
username = var.username
password = var.password
}variables.tf片段
variable "app_subnet_name" {
type = string
}
variable "sql_subnet_name" {
type = string
}来自dev.auto.tfvars的片段
app_subnet_name = "subnet_1"
sql_subnet_name = "subnet_2"
application_nsg = "test_nsg"但是,我遇到了如下错误
Error: Unsupported argument
on main.tf line 7, in module "sql_vms":
7: subnet_name = var.sql_subnet_name
An argument named "subnet_name" is not expected here.
Error: Unsupported argument
on main.tf line 8, in module "sql_vms":
8: app_nsg = var.application_nsg
An argument named "app_nsg" is not expected here.我的模块目录结构如下所示
$ ls -R terraform-modules/
terraform-modules/:
aws azure gcp
terraform-modules/aws:
alb ec2-instance-rhel
terraform-modules/aws/alb:
terraform-modules/aws/ec2-instance-rhel:
main.tf
terraform-modules/azure:
compute resourcegroup sqlserver
terraform-modules/azure/compute:
main.tf README.md variable.tf
terraform-modules/azure/resourcegroup:
data.tf outputs.tf variables.tf
terraform-modules/azure/sqlserver:
main.tf README.md variables.tf
terraform-modules/gcp:
compute
terraform-modules/gcp/compute:
main.tf知道这里出了什么问题吗?
发布于 2020-06-15 07:25:14
我认为问题是,您没有引用与源的确切模块。我看到源代码中有三个模块:
source = "git::git@github.com:xxxxxxxxxxxx/terraform-modules//azure/"他们是compute,resourcegroup和sqlserver。但是你想把它们加载到一个模块中。因此无法找到模块的相关变量。我也不认为这是正确的方式加载所有这样的模块。我建议您一个一个地加载模块,如下所示:
module "compute" {
source = "git::git@github.com:xxxxxxxxxxxx/terraform-modules//azure/compute"
...
}
module "resourcegroup" {
source = "git::git@github.com:xxxxxxxxxxxx/terraform-modules//azure/resourcegroup"
...
}
module "sqlserver" {
source = "git::git@github.com:xxxxxxxxxxxx/terraform-modules//azure/sqlserver"
...
}发布于 2020-09-07 22:23:01
如果您从Terraform开始使用Terraform,如果您的模块参数引用资源属性而不是变量名称,您将得到错误消息(“一个名为”“的参数在这里不是预期的),请参见下面的示例:
要从模块调用的Terraform模块"example_mod.tf“的示例:
variable "sg_name" { } # Usually in a separate file
variable "sg_desc" { } # called variables.tf
resource "example_resource" "example_name" {
name = var.sg_name
description = var.sg_desc
...
}正确方式:
module "my_module" {
source = "./modules/example_mod.tf"
sg_name = "whatever" # NOTE the left hand side "sg_name" is the variable name
sg_desc = "whatever"
...
}不正确的方式:(给出错误“这里不需要一个名为"name”的参数)
module "my_module" {
source = "./modules/example_mod.tf"
name = "whatever" # WRONG because the left hand side "name" is a resource property
description = "whatever" # WRONG for the same reason
...
}发布于 2020-06-14 07:43:38
在不知道模块的详细信息的情况下,通常很难说出错误的原因,但在这种特殊情况下,您要导入的模块中似乎没有使用这两个参数(subnet_name和app_nsg)的要求,或者更确切地说,您使用的是模块的版本,它不要求它们出现。帮助解决这类错误的是检查是否有模块的版本确实有这样的要求。在Terraform模块源文档,选择修订部分中解释了使用Github的特定模块版本的语法:
module "vpc" {
source = "git::https://example.com/vpc.git?ref=v1.2.0"
}您可能使用SSH来获取模块,因此推荐的方法是:
When using Git over SSH, we recommend using the ssh://-prefixed URL form for consistency with all of the other URL-like git address forms.在您的示例中,这将转换为:
module "sql_vms" {
source = "git::ssh://git@github.com/org/terraform-modules-repo.git//azure/module-name?ref=v1.2.0"在org是您组织的(或您的私有) Github帐户的地方,terraform-modules-repo是模块所在的回购中心,module-name是您正在使用的模块,ref=v1.2.0代表模块的修订号。
错误An argument named "example" is not expected here.意味着模块不会看到带有该名称的输入参数。将Terraform模块视为编程语言中的函数:为了使函数提供结果,您需要传递函数一组必需的参数。如果您提供的输入参数比该函数调用所需的更多(或更少),您将得到一个错误。(有一些特殊情况,但不属于这个问题的范围。)
模块和函数之间的另一个相似之处是Terraform模块除了创建指定的资源之外,还可以提供输出值。在输出可以用作其他模块或资源的输入的情况下,这是非常方便的。行module.resource_group.external_rg_location正是这样做的:从另一个模块获取输出值,并使用它为参数location分配一个值。
https://stackoverflow.com/questions/62361263
复制相似问题