【问题标题】:Error: Invalid template interpolation value: Cannot include the given value in a string template: string required错误:无效的模板插值:不能在字符串模板中包含给定值:需要字符串
【发布时间】:2020-03-18 14:12:54
【问题描述】:

我正在编写一个用于在 Oracle 云中创建 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 版本是:

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

跟踪输出为:

https://gist.github.com/RuyCury/bc0dbccc65bbbadf2eb90569fd438286

请帮我解决这个问题。

【问题讨论】:

  • 如果你运行terraform console并在提示符处输入element(module.instances-k8sworker-ad1.public_ips, count.index),你会看到什么?
  • 感谢Martin的回复,在return for command下方。 terraform console > element(module.instances-k8sworker-ad1.public_ips, count.index) Error: Reference to "count" in non-counted context on <console-input> line 1: (source code not available) The "count" object can be used only in "resource" and "data" blocks, and only when the "count" argument is set. >
  • 哦,抱歉,在问这个问题之前我没有仔细阅读这个表达。请改用element(module.instances-k8sworker-ad1.public_ips, 0)。此外,最好编辑您的问题以包含此附加信息,而不是将答案粘贴到评论中,因为这样您就可以共享完整的输出,而无需将其全部折叠到一行中。

标签: terraform


【解决方案1】:

我刚刚遇到了类似的问题并找到了解决方法,所以我会在这里分享。

基本上,我试图获得的 element(module.instances-k8sworker-ad1.public_ips, count.index) 等价物在插值时不可用。我所做的修复它是删除我在 VM 的资源声明中放入的 wait_for_guest_net_timeout = 0 参数。

如果我让这个参数,当我使用terraform console 并在创建后尝试评估我的VM 资源时,输出中没有名为default_ip_address 的键(这是我试图在@ 中获取的资源键987654325@),这就是 terraform 返回 OP 错误(值为空)的原因。当我删除参数时,键和值已正确填充。我的 VM 资源类型为 vsphere_virtual_machine。我猜 OP 正在使用不同类型的资源,但问题可能是相同的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-22
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 2021-03-18
    • 2022-11-04
    相关资源
    最近更新 更多