【问题标题】:Consul Template variable assinging with conditionConsul 模板变量分配条件
【发布时间】:2019-09-06 09:54:41
【问题描述】:

我必须根据其他变量和一些条件分配一些变量

我尝试编写此代码,但没有成功

{{- $region_for_link := $region}}
{{- if eq $region "a"}} {{- $region_for_link := "AAA"}} {{- end}}
{{- if eq $region "b"}} {{- $region_for_link := "BBB"}} {{- end}}

$regiona 时,我想在$region_for_link AAA 中具有价值,但在那之后$region_for_link 仍然是a。如何正确编码,可能很明显,但我无法理解。

请帮忙,我在等你的答复

最好的问候

【问题讨论】:

    标签: haproxy consul consul-template


    【解决方案1】:

    Consul-template 使用 Go Template 引擎的一个版本。根据https://golang.org/pkg/text/template/#hdr-Variables 的文档:

    动作中的管道可能会初始化一个变量来捕获结果。初始化有语法

    $variable := pipeline

    其中 $variable 是变量的名称。声明变量的操作不会产生任何输出。

    之前声明的变量也可以赋值,使用语法

    $variable = pipeline

    下面的代码,导致打印AAA:

    {{ $region := "a" }}
    {{- $region_for_link := $region}}
    {{- if eq $region "a"}} {{- $region_for_link = "AAA"}} {{- end}}
    {{- if eq $region "b"}} {{- $region_for_link = "BBB"}} {{- end}}
    {{ $region_for_link }}
    

    【讨论】:

      猜你喜欢
      • 2021-09-21
      • 2015-05-25
      • 2019-06-18
      • 1970-01-01
      • 2012-01-21
      • 1970-01-01
      • 2018-02-06
      • 2023-04-03
      • 2012-11-08
      相关资源
      最近更新 更多