hashicorp / consul-template

Template rendering, notifier, and supervisor for @HashiCorp Consul and Vault data.
https://www.hashicorp.com/
Mozilla Public License 2.0
4.75k stars 783 forks source link

using stdin/stdout for templates? #84

Open bryanlarsen opened 9 years ago

bryanlarsen commented 9 years ago

Is it possible to read the template from stdin and/or write the output to stdout rather than from a file? It would be nice to avoid managing extraneous temporary files.

Using stdin/stdout without the -once flag would be nonsensical.

the -dry flag almost works for standard output, but it adds an extra leading line.

P.S. Do you have another mechanism for these sorts of questions, such as a mailing list? This is the third time I've (ab)used the issue tracker for usage questions like this.

sethvargo commented 9 years ago

Is it possible to read the template from stdin

Unfortunately that is not possible at this time... I also think it would not be an ideal user experience to specify the template contents via the command line. The escaping and logic would be rather complex.

write the output to stdout rather than from a file

You can use the -dry flag to output to stdout.

the -dry flag almost works for standard output, but it adds an extra leading line.

I'm not sure what your use case is, but it also adds the name of the file as a header (in case you have multiple templates). In general, another system is going to consume the resulting template, so it needs to persist on disk somewhere. If you are just setting things in the environment from Consul, you should take a look at envconsul.

Do you have another mechanism for these sorts of questions, such as a mailing list?

You can use the Consul Mailing list, but here is fine as well.

prologic commented 5 years ago

I don't really find the explanation of why this isn't supported to sit well with me. I deal with UNIX tools a lot and supporting stdin/stdout wouldn't really be that hard. You would special case the -- filename like a lot of other tools do or assume to operate as a standard pipe if-template` is omitted.

NB: This only makes sense with the -once option (which already exists for a similar purpose).

You would thereby be able to do:

cat /path/to/template | consul-template -template '-:-' > /path/to/output

Without this support I am forced to create temporary files just to I can satisfy the fact consul-template -template ... wants full paths to files on disk.

eikenb commented 5 years ago

Hey @prologic ... being an old unix-head, I also find it odd that this wouldn't be possible. A file is a file. If you'd like to re-submit an issue about this, please do and I'll look into it (I prefer a new ticket to re-opening a zombie ticket from 4-5 year ago). Thanks.

prologic commented 5 years ago

Hey @prologic ... being an old unix-head, I also find it odd that this wouldn't be possible. A file is a file. If you'd like to re-submit an issue about this, please do and I'll look into it (I prefer a new ticket to re-opening a zombie ticket from 4-5 year ago). Thanks.

I've obviously since worked around this missing feature; However I'm still happy to see this situation improved.

How about I just put up a PR? WOuld that work for you?

eikenb commented 5 years ago

A PR would be great. I'd love to see what you have in mind.

prologic commented 5 years ago

Done

eikenb commented 3 years ago

Reopening this as I do plan on adding it once I have the new library migration completed and I don't want to forget.

jeff-minard-ck commented 3 years ago

I found myself at this thread as I was looking to integrate Flux with consul-template as a post-processor so that I could fetch secrets from Hashicorp Vault and then have flux apply the rendered YAML-files-with-secrets into Kubernetes, but flux expects the YAML to be emitted on stdout from the script call out.

Instead I am currently doing something to the tune of:

# /callout.sh
# mktmp -d to get a temp dir
# loop through template files, make a consul-template.hcl config file which renders out to above tmpdir
consul-template -config consul-template.hcl
find $tmpdr -exec cat +;

Which...works, I guess, but it does feel slightly silly.

(I also tried -template input.ctmpl:- and, of course, got a - file :D)

It's not a huge issue, because I have to dynamically create that templates set of directives anyway, though. Now, if there was a -once -templatesdir allTheCtmpls/ -stdout option...

rgajason commented 2 months ago

Similar to other workarounds in this thread for a single file you can do:

consul-template \
    -template=my-template-file.ctmpl \
    -once -dry | tail -n +2

The tail -n +2 strips the first line from the output (which is a header with the file name when using -dry).