Optional secrets in template stanza

Hi @bjornicus,

The Nomad template block uses consul-template internally to perform the rendering and API calls. The documentation on the secret function includes the following which I believe would help solve you problem:

You can also guard against empty values using if or with blocks.

Specifically as you mention the use of an empty string I believe the following code example would be suited:

{{ with secret "secret/my/optional/key"}}
{{ if .Data.value }}
MY_OPTIONAL_SECRET = "{{ .Data.value }}"
{{ else }}
MY_OPTIONAL_SECRET = ""
{{ end }}
{{ end }}

Thanks,
jrasell and the Nomad team

1 Like