Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace string with concatenate variables #534

Open
Amonlol opened this issue Mar 27, 2024 · 1 comment
Open

Replace string with concatenate variables #534

Amonlol opened this issue Mar 27, 2024 · 1 comment

Comments

@Amonlol
Copy link

Amonlol commented Mar 27, 2024

Description

I'm having a hard time understanding levant templating for my nomad job. Let's say I have a variable .shortJobNames (can be true of false). I want to construct my prometheus config out of local file template and replace blackbox exporter service name placeholder with the actual one for prometheus to use consul service mesh (can be "blackbox" if .shortJobNames or [[ .jobName ]]-blackbox-[[ .instanceName ]] if not)

So this:

      template {
        destination = "local/prometheus.yml"
        data = <<EOH
[[fileContents "monitoring/templates/prometheus/config.yml" ]]
EOH
      }

Turns into this:

      template {
        destination = "local/prometheus.yml"
        data = <<EOH
[[fileContents "monitoring/templates/prometheus/config.yml" | replace "$BLACKBOXSERVICENAME" [[ if .shortJobNames ]][[ with $blackboxservicename := "blackbox" ]][[ else ]][[ with $blackboxservicename := [[ .jobName ]]-blackbox-[[ .instanceName ]] ]]
EOH
        [[ end ]]
      }

Also tried with variable, but could not find in docs how to construct such a string:

      template {
        destination = "local/prometheus.yml"
        [[ if .shortJobNames ]]
        [[ with $blackboxservicename := "blackbox" ]]
        [[ else ]]
        [[ with $blackboxservicename := ".jobName-blackbox-.instanceName" ]]
        [[end]]
        data = <<EOH
[[fileContents "monitoring/templates/prometheus/config.yml" | replace "$BLACKBOXSERVICENAME" $blackboxservicename ]]
EOH
      }

UPD: I do not want to hardcode name in config and\or job itself to utilize DRY. I have multiple environments which are independantly deployed

Output of levant version:

Levant v0.3.2
@Amonlol Amonlol changed the title Construct a string out of Mar 27, 2024
@angrycub
Copy link
Contributor

You would want to use the printf function to concatenate the long form of your variable. The printf function is useful for generating composed values that can be arguments for functions like env and index as well.

Here's how I would do it. Note, the [[- and -]] are whitespace controlling delimiters and remove any non-explicitly generated whitespace outside of them. You can also solve for the variable very early in the template rendering so that you can use the built service name in other places as well.

[[- $BSN := "blackbox" -]]
[[- if .longJobNames -]]
  [[- $BSN =  printf "%s-blackbox-%s" .jobName .instanceName -]]  
[[- end -]]
[[ fileContents "config.yml" | replace "$BLACKBOXSERVICENAME" $BSN ]]

A note about false(ish) values: the text/template library treats values that match go's floor values of a type as implicitly false. The .shortJobNames or .longJobNames, cases that you and I have as the target of the if are only false when unset or "". Any other value evaluates to true because the string is non-empty. If you are expecting the value to be populated, you should use the eq function and check it against a known value or convert it from string to boolean

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants