Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Terraform 地形。如何使用variable.tf文件中的列表(字符串)在命令行中传递多个值?_Terraform_Terraform Provider Azure - Fatal编程技术网

Terraform 地形。如何使用variable.tf文件中的列表(字符串)在命令行中传递多个值?

Terraform 地形。如何使用variable.tf文件中的列表(字符串)在命令行中传递多个值?,terraform,terraform-provider-azure,Terraform,Terraform Provider Azure,我有一个简单的主文件和变量文件,用于在Azure中部署webapp for containers 但我希望terraform plan使用命令行中的变量来选择如下名称: terraform plan -var resource_group_name=my-rg 这样对RG的默认值的名称进行注释非常有效 main.tf data "azurerm_resource_group" "my-rg" { name = var.resource_group_n

我有一个简单的主文件和变量文件,用于在Azure中部署webapp for containers

但我希望terraform plan使用命令行中的变量来选择如下名称:

terraform plan -var resource_group_name=my-rg
这样对RG的默认值的名称进行注释非常有效

main.tf

data "azurerm_resource_group" "my-rg" {
  name = var.resource_group_name
}
resource "azurerm_app_service" "azure-webapp" {
  count = length(var.webapp_server_name)
  name  = var.webapp_server_name[count.index]
变量.tf

variable "resource_group_name" {

    
#    default = "Search-API"

}
variable "webapp_server_name" {
  
    description = "Create Webapp with following names"
    type        = list(string)
    #default     = ["webapp-a", "webapp-b", "webapp-c"]
但是,如果我想对列表字符串执行相同的操作,我不知道如何执行。我希望能够做一些事情,如果我把2个名字,2个网络应用程序将被创建,如果我把3个,3个网络应用程序等等

我尝试了以下方法(也对默认值进行了注释):

main.tf

data "azurerm_resource_group" "my-rg" {
  name = var.resource_group_name
}
resource "azurerm_app_service" "azure-webapp" {
  count = length(var.webapp_server_name)
  name  = var.webapp_server_name[count.index]
变量.tf

variable "resource_group_name" {

    
#    default = "Search-API"

}
variable "webapp_server_name" {
  
    description = "Create Webapp with following names"
    type        = list(string)
    #default     = ["webapp-a", "webapp-b", "webapp-c"]
但是我得到了:

 terraform plan -var webapp_server_name=webapp-a

Error: Variables not allowed

  on <value for var.webapp_server_name> line 1:
  (source code not available)

Variables may not be used here.
有没有一种方法可以用terraform做这样的事情?要定义空列表并从命令传递值(一个、两个或多个)

谢谢


更新 像这样尝试过,但现在要求输入值,即使我通过命令行传递它

terraform plan -var 'listvar=["webapp-a"]'

var.webapp_server_name
  Create Webapp with following names

  Enter a value:

当您使用
-var webapp\u server\u name=webapp-a
从命令行传入变量时,您将其作为字符串传入

但是您已经将变量定义为一个列表。因此,基于此,您希望命令行看起来像:

terraform plan -var='webapp_server_name=["webapp-a"]'

如果存在变量声明:

variable "webapp_server_name" {
  
    description = "Create Webapp with following names"
    type        = list(string)
    #default     = ["webapp-a", "webapp-b", "webapp-c"]
}
您可以将其与
\
一起使用,以转义引号

例如,它使用最新的terraform provider版本
terraform v0.13.4


对于CLI中的非字符串类型变量输入,我认为您必须以JSON格式传递值。只是尝试了不同的方法,检查更新的问题。它还要求输入一个值您的变量名为
webapp\u server\u name
,而不是
listvar
。这不起作用。尝试了不同的方法读取其他帖子,但结果不同.检查更新的问题-