gpt4 book ai didi

azure-virtual-machine - 表达式解析成功,但后面发现多余的字符

转载 作者:行者123 更新时间:2023-12-04 10:58:01 34 4
gpt4 key购买 nike

我有一个“create_vm”模块,它正在创建 VM 以及存储帐户、NIC 等。

我的要求是我想将 IP 地址列表从根模块传递到 create_vm 模块,以便任何人都可以根据他们的要求限制与 VM 的绑定(bind)连接。纠正我我没有使用正确的模块术语。

目录结构如下所示:

subscription_1  subscription_2  Modules  README.md  

./subscription_1:

./subscription_2:
main.tf sqlvm.tf.bak terraform.tfstate terraform.tfstate.backup variables.tf

./Modules:
create_vm

./Modules/create_vm:
main.tf variable.tf

cat ./Modules/create_vm/main.tf
resource "azurerm_network_security_rule" "tf-nsr-5986" {
...
source_address_prefixes = "${var.allowed_source_ips}"
...
}

cat ./Modules/create_vm/variable.tf
variable "allowed_source_ips" {
description = "List of ips from which inbound connection to VMs is allowed"
type = "list"
}

在根模块中使用它
猫 ./subscription_2/main.tf
module "vm_app" {
...
allowed_source_ips = "${var.ip_list}"
...
}

猫 ./subscription_2/variable.tf
variable "ip_list" {
description = "List of ips from which inbound connection to VMs is allowed"
type = "list"
}

现在,我通过传递参数从本地 vm 运行 terraform,就像在 Azure DevOps 管道上一样
terraform plan -var "resource_group_name=nxt-grp-prd-manage-rgp-au-se" -var "virtual_network_name=virtual_network_1" -var "sql_subnet_name=subnet_1" -var "app_subnet_name=subnet_2" -var "application_nsg=test_nsg" -var "count_vm=2" -var "sql_host_basename=sqlvms" -var "app_host_basename=appvms" -var "storage_account_suffix=sta" -var "virtual_machine_size=Standard_B1ms" -var "virtual_machine_image_publisher=MicrosoftWindowsServer" -var "virtual_machine_image_offer=WindowsServer" -var "virtual_machine_image_sku=2012-R2-Datacenter" -var "virtual_machine_image_version=latest" -var "username=devopsadmin" -var "password=Angular12#$%" -var "ip_list="a.b.c.d","p.q.r.s","x.y.z.l""

不幸的是,我收到如下错误消息:
Error: Invalid number literal

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

Failed to recognize the value of this number literal.


Error: Extra characters after expression

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

An expression was successfully parsed, but extra characters were found after
it.

有没有人解决过这种类型的挑战?不太确定它为什么提示,我给出的方式与之前在 create_vm 模块中给出的方式相同。

任何帮助将非常感激。

最佳答案

您应该为 ip_list 变量分配一个列表,而不是一个字符串。您期待一个列表,但您分配了一个字符串(这在技术上很好,它被解析为一个包含单个字符串的列表。)然后是一个逗号,这是额外的字符。

相反,您应该传递一个正确的列表作为参数,注意 'array'/[](方括号)表示法。

 ... -var "ip_list=["a.b.c.d","p.q.r.s","x.y.z.l"] ...

我也强烈建议您改用变量文件。然后,您将获得一些 linting/terraform 语言支持。这将使您在 ci/cd 中运行 tf apply 之前更容易发现错误。

您将创建一个文件 variables.tfvars :
...
variable "ip_list" {
description = "List of ips ..."
default = ["a.b.c.d","p.q.r.s","x.y.z.l"]
}
...

然后您可以将整个文件作为参数传递给 terraform apply -var-file="./variables.tfvars" .

关于azure-virtual-machine - 表达式解析成功,但后面发现多余的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59045182/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com