0

I have to assign some variable depending on other variable and some conditions

I've tried to code this but with no luck

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

I wanted to have value in $region_for_link AAA when $region is a, but after that $region_for_link is still a. How to code that properly, probably it is obvious case, but I can't get it.

Please help, I'm waiting for your answer

Best Regards

konradm
  • 73
  • 1
  • 12

1 Answers1

0

Consul-template uses a version of the Go Template engine. According to the documentation at https://golang.org/pkg/text/template/#hdr-Variables :

A pipeline inside an action may initialize a variable to capture the result. The initialization has syntax

$variable := pipeline

where $variable is the name of the variable. An action that declares a variable produces no output.

Variables previously declared can also be assigned, using the syntax

$variable = pipeline

The below code, resulted in printing of 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 }}
Community
  • 1
  • 1