【发布时间】: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