Consul-Template:循环 k/v 对并在另一个键中使用结果
Consul-Template: Looping over a k/v pair and using the result in another key
给定 Consul 密钥:
flyway/tweedle/server: postgres
flyway/beetle/server: postgres
flyway/battle/server: mysql
service/tweedle/repo: fox/tweedle.git
service/beetle/repo: fox/beetle.git
service/battle/repo: fox/battle.git
我正在尝试遍历顶部集合,使用键查找底部集合中的值,使用此代码(不起作用):
{
{{ range $key, $pairs :=tree "flyway/" | explode }}
$key: {{ key "service/{{$key}}/repo" }}
{{ end }}
}
得到:
tweedle: fox/tweedle.git
beetle: fox/beetle.git
battle: fox/battle.git
我的同事 Brian 在 HashiCorp 的支持票中回答了这个问题。
只是想在这里分享答案,以便其他人也能受益。这是如何完成的:
{
{{ range $key, $pairs := tree "flyway/" | explode }}
{{ $name := $key }}
{{ range $key, $pairs := tree ($name | printf "service/%s/") | explode }}
{{ $name}}: {{ $pairs }}
{{ end }}
{{ end }}
}
给定 Consul 密钥:
flyway/tweedle/server: postgres
flyway/beetle/server: postgres
flyway/battle/server: mysql
service/tweedle/repo: fox/tweedle.git
service/beetle/repo: fox/beetle.git
service/battle/repo: fox/battle.git
我正在尝试遍历顶部集合,使用键查找底部集合中的值,使用此代码(不起作用):
{
{{ range $key, $pairs :=tree "flyway/" | explode }}
$key: {{ key "service/{{$key}}/repo" }}
{{ end }}
}
得到:
tweedle: fox/tweedle.git
beetle: fox/beetle.git
battle: fox/battle.git
我的同事 Brian 在 HashiCorp 的支持票中回答了这个问题。
只是想在这里分享答案,以便其他人也能受益。这是如何完成的:
{
{{ range $key, $pairs := tree "flyway/" | explode }}
{{ $name := $key }}
{{ range $key, $pairs := tree ($name | printf "service/%s/") | explode }}
{{ $name}}: {{ $pairs }}
{{ end }}
{{ end }}
}