Consul-Template:循环 k/v 对并在另一个键中使用结果

标签 consul-template

给定领事键:

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-Template:循环 k/v 对并在另一个键中使用结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45702169/

相关文章:

go - 如何从 map 中获取 key

bash - 使用 shell 脚本解析 consul 模板文件中的行

Consul 1.2.1 在命令参数中使用管道定义健康检查

ansible - Consul 的 Key-Value 存储有哪些具体用例?

consul-template - 如何在 consul-template 范围构造中使用变量?

templates - 如何用逗号连接领事模板的服务元数据

templates - 在 consul-template 的循环中覆盖变量

loops - 在go模板中获取迭代器索引(consul-template)

linux - 使 ExecStartPost 命令在后台运行