SaltStack: In a watch statement, how do I specify a directory where all files should be watched?

coffee-grinder picture coffee-grinder · May 17, 2014 · Viewed 14.1k times · Source

I would like the nginx service to restart whenever any file in the /etc/nginx/conf.d directory is created or modified.

There are a number of files in that directory, and rather than specifying particular files, I would like to watch for all changes.

I've tried this:

nginx:
  pkg.installed:
    - name: nginx
  service:
    - running
    - enable: True
    - restart: True
    - watch:
      - file: /etc/nginx/nginx.conf
      - file: /etc/nginx/conf.d
      - pkg: nginx

but the line - file: /etc/nginx/conf.d is not doing what I want.

This is the error:

      ID: nginx
Function: service.running
  Result: False
 Comment: The following requisites were not found:
                             watch:
                                 file: /etc/nginx/conf.d
 Changes: 

I've also tried a number of variations including a trailing slash, but none of them work.

What should - file: /etc/nginx/conf.d/ be changed to?

Answer

nmadhok picture nmadhok · May 22, 2014

I'm using a glob for matching:

file: /etc/nginx/conf.d/*

Here's the corrected snippet:

nginx:
  pkg.installed:
    - name: nginx
  service:
    - running
    - enable: True
    - restart: True
    - watch:
      - file: /etc/nginx/nginx.conf
      - file: /etc/nginx/conf.d/*
      - pkg: nginx

Also do note that salt can only watch other states that are already specified in your state file, so it will only watch files that are managed by salt itself.

If this doesn't work for you, then try to refer the following link for a different solution: http://intothesaltmine.org/blog/html/2012/12/18/using_watch_with_file_recurse.html