Terraform 错误:模板插值值无效:不能在字符串中包含给定值模板:字符串必需

Terraform 错误:模板插值值无效:不能在字符串中包含给定值模板:字符串必需,terraform,Terraform,我正在编写一个脚本,用于在oracle cloud中创建kubernetes集群。 当我运行terraform plan时,它不会返回任何错误。但当我运行terraform apply时,它会返回错误: Error: Invalid template interpolation value: Cannot include the given value in a string template: string required. 我启用了跟踪和日志文件生成,以尝试确定问题所在,在跟踪中它返回:

我正在编写一个脚本,用于在oracle cloud中创建kubernetes集群。 当我运行terraform plan时,它不会返回任何错误。但当我运行terraform apply时,它会返回错误:

Error: Invalid template interpolation value: Cannot include the given value in a string template: string required.
我启用了跟踪和日志文件生成,以尝试确定问题所在,在跟踪中它返回:

2019/11/21 20:31:15 [TRACE] EvalMaybeTainted: null_resource.k8sworker-ad1 [0] encountered an error during creation, so it is now marked as tainted
2019/11/21 20:31:15 [ERROR] : eval: * terraform.EvalApplyPost, err: 1 error occurred:

    Invalid template interpolation value: Cannot include the given value in a template: string required string.

2019/11/21 20:31:15 [TRACE] EvalMaybeTainted: null_resource.k8sworker-ad1 [1] encountered an error during creation, so it is now marked as tainted
2019/11/21 20:31:15 [ERROR] : eval: * terraform.EvalSequence, err: Invalid template interpolation value: Cannot include the given value in a string template: string required.
我相信问题出在代码的这一部分:

resource "null_resource" "k8sworker-ad1" {
  count      = var.k8sWorkerAd1Count
  depends_on = [module.instances-k8sworker-ad1]

  triggers = {
    worker_id       = module.instances-k8sworker-ad1.ids[0][count.index]
    build_source_id = null_resource.build_source.id
  }

  provisioner "local-exec" {
    command = "echo 'alias ${var.label_prefix}workerad1-${count.index}=\"ssh -i ${path.root}/generated/instances_id_rsa opc@${element(module.instances-k8sworker-ad1.public_ips, count.index)}\"' >> source.sh"
  }
}
地形版本为:

Terraform v0.12.16
+ provider.null v2.1.2
+ provider.oci v3.52.0
+ provider.random v2.2.1
+ provider.template v2.1.2
+ provider.tls v2.1.1
跟踪输出为:


请帮我解决这个问题。

我刚刚遇到了一个类似的问题,并找到了一个解决方案,我将在这里分享

基本上,我尝试获取的
元素(module.instances-k8sworker-ad1.public\u ips,count.index)的等价物在插值时不可用。我所做的修复是删除了我在VM的资源声明中输入的
wait\u for\u guest\u net\u timeout=0
参数


如果我使用此参数,当我使用
terraform console
并尝试在创建后评估我的VM资源时,输出中没有名为
default\u ip\u address
的键(这是我试图在
元素中获取的资源键),这一定是terraform返回OP错误(值为空)的原因。当我删除参数时,键和值被正确填充。我的虚拟机资源的类型为
vsphere\u virtual\u machine
。我猜OP使用的是不同类型的资源,但问题可能是相同的。

如果运行
terraform console
并在提示符下输入
元素(module.instances-k8sworker-ad1.public\u ips,count.index)
,您会看到什么?感谢您在return for命令下面的回复Martin<代码>地形控制台>元素(module.instances-k8sworker-ad1.public_ips,count.index)错误:在第1行的非计数上下文中引用“count”:(源代码不可用)“count”对象只能在“resource”和“data”块中使用,并且只能在设置“count”参数时使用。>元素(module.instances-k8sworker-ad1.public\u ips,0)
。此外,最好编辑您的问题以包含这些附加信息,而不是将答案粘贴到注释中,因为这样您就可以共享完整的输出,而无需将其全部折叠到一行。