metadata_startup_script 中的 Terraform 外部数据 [英] Terraform external data in metadata_startup_script
问题描述
我要将其他 .tf 文件中的令牌值解析到其他 .tf 文件中
I'm going to parsing a token value from other .tf file into other .tf file
I have tried to understand this link and also from this article
data.tf
data "external" "get_token" {
program = ["/bin/sh", "${path.module}/get-token.sh"]
}
get-token.sh
#!/bin/bash
token=$(kubectl -n kube-system exec [POD_NAME] cat /var/lib/kube-proxy/kubeconfig 2>/dev/null | grep token | awk '{print $2}'
proxy.tf
...
metadata_startup_script = <<-EOT
- name: kube-proxy
user:
token: ${lookup(data.external.get_token.result, "token")}
certificate-authority-data: ${google_container_cluster.new_container_cluster.master_auth.0.cluster_ca_certificate}
...
EOT
我的期望是token
的值与 certificate-authority-data
相同.certificate-authority-data
具有我期望的确切值,但 token
为 nil 或空白.我已经手动运行了我的 get-token.sh
,这很好.但是当 terraform 要解析它时,该值没有解析成功.我在变量 ${lookup(data.external.get_token.result, "token")}
前后添加了 '
.好像不行.
My expectation is
token
has the value as same as with certificate-authority-data
.
certificate-authority-data
has a exact value like i expect but the token
is nil or blank.
I have run my get-token.sh
manually and it's good. But when terraform want to parse it, the value is not parsed successfully. I have added '
before and after the variable ${lookup(data.external.get_token.result, "token")}
. Seems not to work.
推荐答案
https://www.terraform.io/docs/providers/external/data_source.html
然后程序必须在标准输出上产生一个有效的 JSON 对象,它将用于填充导出到其余部分的结果属性Terraform 配置.此 JSON 对象必须再次具有所有它的值作为字符串.成功完成后,它必须退出状态为零.
The program must then produce a valid JSON object on stdout, which will be used to populate the result attribute exported to the rest of the Terraform configuration. This JSON object must again have all of its values as strings. On successful completion it must exit with status zero.
所以脚本应该返回一个 json 对象.
So the script should return a json object.
#!/bin/bash
...
# add below line for make a json result
jq -n --arg token "$token" '{"token":$token}'
或者如果没有jq,
#!/bin/bash
...
#add below
echo -n "{"token":"${token}"}"
这篇关于metadata_startup_script 中的 Terraform 外部数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!