Reuse consul-template template

Hello everyone,

Let’s imagine I have a template like the one below:

{{ with $myvar := key <some key>  }}
  <do something>
{{ end }}

I would like to reuse the above template by, somehow, passing a different key. In other words, I would like to pass different some key values and render the same template.

Is that possible? If so, how?

Hi @mccricardo,

You can use Consul template’s executeTemplate function to do this. For example (untested):

{{- define "key-template" -}}
  {{- with $myvar := key . -}}
    {{- /* Do something */ -}}
  {{ end }}
{{- end -}

{{- executeTemplate "key-template" "prefix/key_name" -}}

Hi @blake,

Thank you for your answer.

I’ve been messing around with executeTemplate. But what if I have a template in a file and want to pass a key to that template?

Hi @mccricardo,

Loading templates from a file is not currently supported. We’re tracking this feature request in hashicorp/consul-template#1402. I recommend leaving a :+1: reaction on the existing issue description so that we can track interest in this enhancement.

Thanks.

Thank you for the feedback!