Use hostname in template?

This cannot possibly be that difficult, but I cannot figure out how to get the hostname value in a consul (really, vault) template.

The basic problem is that I’m trying to use pkiCert to get a certificate for consul from vault. Since consul needs the certificate before it can talk to the consul server, I can’t use - afaik because consul isn’t up yet. I have to use the hostname if that’s what I want for the cert’s CN when I call vault’s .../issue

Some examples have shown using {{ with $host := env "HOSTNAME" }} - which seems to just give me an empty value and others have gone to creating custom go functions within the template itself(?), which I’m struggling to comprehend.

{{ with pkiCert "/pki/issue/node-dot-consul" "common_name=1-2-3-4.node.consul" }}
{{ .Data.Cert }}
{{ end }}

Is it possible to replace 1-2-3-4 with a variable of the hostname in that template code?

oi. $host wasn’t working because %q is the wrong placeholder I guess. Switching to %s was the trick.

This does in fact work:

{{ $cn_arg := printf "common_name=%s.node.consul" (env "HOSTNAME") }}
{{ with pkiCert "/pki/issue/node-dot-consul" $cn_arg }}
{{ .Data.Cert }}
{{ end }}
1 Like