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

时间:2019-11-22 13:34:04

标签: terraform

我正在编写一个脚本,用于在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

请帮助我解决这个问题。

1 个答案:

答案 0 :(得分:0)

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

基本上,我要获得的element(module.instances-k8sworker-ad1.public_ips, count.index)等效项在插值时不可用。我要解决的问题是删除我在虚拟机的资源声明中添加的wait_for_guest_net_timeout = 0参数。

如果让此参数生效,则当我使用terraform console并尝试在创建后评估我的VM资源时,输出中没有称为default_ip_address的密钥(这是我尝试使用的资源密钥)进入element),这必须是terraform返回OP错误(值是null)的原因。当我删除参数时,键和值已正确填充。我的VM资源的类型为vsphere_virtual_machine。我猜OP在使用不同类型的资源,但是问题可能是相同的。