Consul-template: how to run exec command, if file changed? Create a "watcher" via consul-template.
You can create a "watcher" via consul-template using file function. But for this function we need to create source and destination files. If you want to simply monitor one file and destination file is unwanted, you can do simple trick using optional command in consul-template, something like this:
template {
contents = "{{ file \"$WATCHING_FILE\" }}"
destination = "$DEV_NULL_FILE"
command = "/bin/sh -c 'rm -f $DEV_NULL_FILE'"
}
In this template we monitor $WATCHING_FILE. If it changed, consul-template creates new $DEV_NULL_FILE and triggers command "/bin/sh -c 'rm -f $DEV_NULL_FILE'" which delete this file.
So finally we can have exec block, which is triggered after this template.
template {
contents = "{{ file \"$WATCHING_FILE\" }}"
destination = "$DEV_NULL_FILE"
command = "/bin/sh -c 'rm -f $DEV_NULL_FILE'"
}
In this template we monitor $WATCHING_FILE. If it changed, consul-template creates new $DEV_NULL_FILE and triggers command "/bin/sh -c 'rm -f $DEV_NULL_FILE'" which delete this file.
So finally we can have exec block, which is triggered after this template.
Комментарии
Отправить комментарий