terraform计划返回错误:参数不受支持

时间:2020-06-13 14:37:21

标签: terraform-provider-azure

我有以下三个文件,如下所示: 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

你知道这里出了什么问题吗?

5 个答案:

答案 0 :(得分:1)

在不了解有关模块的详细信息的情况下,通常很难说出错误的原因是什么,但是在这种特殊情况下,似乎导入的模块中并不需要使用这两个参数( subnet_nameapp_nsg),或者您使用的是模块的 版本 ,不需要它们出现。解决此类错误的方法是检查是否存在确实有此要求的模块版本。 Terraform Module Sources文档的Selecting a Revision部分中说明了使用Github中特定模块版本的语法:

module "vpc" {
  source = "git::https://example.com/vpc.git?ref=v1.2.0"
}

您可能正在使用SSH来获取模块,因此recommended way to do that是:

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模块除了创建指定的资源外,还可以提供output values。在将输出用作其他模块或资源中的输入的情况下,这很方便。行module.resource_group.external_rg_location正是这样做的:从另一个模块获取输出值,并使用它为参数location赋值。

答案 1 :(得分:1)

我认为问题是您没有使用源引用确切的模块。我看到源代码中包含三个模块:

source = "git::git@github.com:xxxxxxxxxxxx/terraform-modules//azure/"

它们是computeresourcegroupsqlserver。但是您想将它们加载到一个模块中。因此,它找不到模块的相关变量。我也不认为这是加载所有模块的正确方法。我建议您像下面这样一个一个地加载模块:

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"
  ...
}

答案 2 :(得分:0)

可能由于多种原因而发生。 我建议您进行一些验证:


  1. 检查您是否使用了正确的源URL,路径或修订分支/标记。

我不确定您的实现方式,但是您可能要仔细检查所引用的修订版,其中包含这些变量声明。

GitHub模块寻址允许 ref 参数。

请参阅GitHub Module Addressing for Terraform Documentationhow to specify a revision.

  1. 检查是否在每个模块(包括根模块)上都声明了所有必需的变量。

您是否同时在根目录和模块上下文/路径的 variables.tf 文件中声明了这些变量?

我知道这很累赘且重复,但是每个模块都应设计为“ 独立项目”。 每个模块**必须都有自己的声明变量.tf **,它们用作该模块的输入,还希望它具有自己的映射输出。tf,provider.tf,后端.tf等,尽管不需要这些最后一个。


FYI::这样做可以保证可伸缩性,可重用性以及可靠性,以便与每个模块的不同 tfstate 文件甚至不同的存储库一起使用,以保证原子性和最低权限,从而防止您的基础结构被不希望的代码更改破坏。

我强烈建议this read了解独立模块化设计的重要性。

此外,keeping your code DRY ( Don't Repeat Yourself )可以使Terragrunt,Terratest等工具减轻这项工作的负担。

  1. 检查相关变量的**类型约束是否匹配。**

如果不是这种情况,请尝试查看类型约束是否匹配用作参数的所有变量声明(在根 variables.tf 上)和输入(在模块级别 variables.tf )。

答案 3 :(得分:0)

我也将分享我的痛苦。

这样的写块配置

=

代替(注意 vpc_config { subnet_ids = [aws_subnet.example1.id, aws_subnet.example2.id] } 等号):

An argument named "vpc_config" is not expected here

将给出scanf("%f", &dollars);的错误,并将浪费您几个小时。

答案 4 :(得分:0)

如果您从Terraform开始,则会收到该错误消息(“ 此处不应使用名为“ example”的参数“),如果您的模块参数引用了资源属性而不是变量名称,请参见下面的示例:

要从模块中调用的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   
...
}