consul-template icon indicating copy to clipboard operation
consul-template copied to clipboard

How to listening the template file change?

Open yangzw1024 opened this issue 3 years ago • 1 comments

I'm use consul-template for consul kv, when the kv changed, it's render the new file through the template, but when the template changed and kv not changed, it's cann't rended, how to listening the template file change ?

yangzw1024 avatar Oct 26 '21 18:10 yangzw1024

Hello @yzhengwei, thanks for the question.

Consul-template doesn't monitor the template for changes normally, but you can sort of hack around this with the file template function. As a super simple example say you have a template foo.tmpl with the contents:

{{ key "bar" }}

And you want to have it pick up on changes to that file you can use the file template function to watch it. The file template function returns the contents of the file (normally used as an include-like field) but you can limit the output by piping through len and just having that in a comment (we'll assume the file supports # for comments in our example). So if we add this to foo.tmpl we end up with something like..

{{ key "bar" }}
# {{ file "foo.tmpl" | len -}}

Another option could be to have a consul-template with another consul-template as it's Exec managed process. Then you could have the template of the second consul-template as a target for the first consul-template and trigger restarts that way.

Hope this helps.

eikenb avatar Oct 28 '21 22:10 eikenb