Is it possible to restart a Nomad job when one or more Vault keys change? I would like to store Let’s Encrypt certificates and keys in Vault, and have the Nomad jobs that depend on these restart automatically whenever the certificates and keys change.
I found a GitHub issue (https://github.com/hashicorp/nomad/issues/5052) where someone asks essentially the same question, and one person answered that “Nomad client agents (via consul-template) watch for changes”. However, I tried it out and I cannot get it to work. I can’t tell if I’m misunderstanding how it all works or if I’m just doing something wrong.
Here is how I tested:
I have a some keys defined in the secret/infra/change-test
path in Vault: key test1
and key test2
. I also have the following defined in my job spec file:
template {
data = <<EOF
{{ with secret "secret/infra/change-test" }}TEST1={{ .Data.test1 }}{{ end }}
EOF
destination = "secrets/file.env"
env = true
}
template {
data = <<EOF
{{ with secret "secret/infra/change-test" }}{{ .Data.test2 }}{{ end }}
EOF
destination = "local/test"
}
When I start the job it downloads and runs a small program that echoes the value of the TEST1
environment variable, reads and outputs the content of the local/test
file, sleeps ten seconds and then goes through the sequence again.
Now, based on answers in #5052 I expected that if I changed either the value of test1
or test2
in Vault, Nomad would register the change and restart the task based on the default value of the change_mod
parameter, which is "restart"
. However, the task is not restarting, and the test1
environment variable value and the contents of the local/test
file do not change either.
Am I misunderstanding how this is supposed to work? If so, is it at all possible for Vault key changes to be reflected in a Nomad template at runtime?
Thanks,
-Martin