Salt: Service state: watch directory for changes

Created on 9 Feb 2012  路  15Comments  路  Source: saltstack/salt

In the service state, it is only possible to watch a single file (e.g. configuration file) for changes to restart the corresponding service afterwards. Some services (like munin) keep their configuration files split up into a lot of text files, or (like munin) they use a plugin system that keeps all plugins in a directory. In these cases, restarting the service is necessary when a file is added or changed in this directory. It should be possible for salt to watch a directory (not just a single file) for changes.

Example:
munin-node:
pkg:
- installed
service:
- running
- watch: /etc/munin/plugins

Most helpful comment

Would it be a good idea to add a new argument for watch, like

httpd:
  service:
    - running
    - watch:
      - directory: /etc/httpd

It would pretty much be the same behaviour as globbing but would add semantics and it would be possible to extend the behaviour of directory such that if there is any permission change of a directory, there is also a restart.

All 15 comments

Salt can watch multiple files, you just need to list them all:

munin-node:
pkg:

  • installed
    service:
  • running
  • watch:

    • file: /etc/munin/plugins/file1

    • file: /etc/munin/plugins/file2

But I agree, having a feature to watch all files salt is managing in a directory will be a welcome feature

We need to remember to watch recurse for file changes as well

Done:
a36b3d1858ad127bfe87b73c9faf737d7309bfb1

Now requisites can use globs for matching:

httpd:
  service:
    - running
    - watch:
      - file: /etc/httpd/*

this works, but does it also work recursive?

Yes, it works globally :)
let me know if there is unexpected behavior.

Would it be a good idea to add a new argument for watch, like

httpd:
  service:
    - running
    - watch:
      - directory: /etc/httpd

It would pretty much be the same behaviour as globbing but would add semantics and it would be possible to extend the behaviour of directory such that if there is any permission change of a directory, there is also a restart.

Sadly I'm seeing this fail.. for a directory that exists even. Below I show the fail and then show a file command sees the directory as existing. The directory has files in it..

ubuntu@ausw2-mongomanager03:~$ sudo salt-call state.highstate
local:
----------
          ID: start_datadog
    Function: service.running
        Name: datadog-agent
      Result: False
     Comment: The following requisites were not found:
                                 watch:
                                     file: /etc/dd-agent/conf.d/*
     Started:
    Duration:
     Changes:

Summary for local
-------------
Succeeded: 83
Failed:     1
-------------
Total states run:     84
ubuntu@ausw2-mongomanager03:~$ file /etc/dd-agent/conf.d/
/etc/dd-agent/conf.d/: directory

Seems like a regression.

sls

start_datadog:
  service.running:
    - name: datadog-agent
    - watch:
      - file: /etc/dd-agent/datadog.conf
      - file: /etc/dd-agent/conf.d/*

Version

Salt Version:
           Salt: 2016.3.1

Dependency Versions:
           cffi: Not Installed
       cherrypy: Not Installed
       dateutil: 2.2
          gitdb: Not Installed
      gitpython: Not Installed
          ioflo: Not Installed
         Jinja2: 2.7.2
        libgit2: Not Installed
        libnacl: Not Installed
       M2Crypto: 0.22
           Mako: Not Installed
   msgpack-pure: Not Installed
 msgpack-python: 0.4.7
   mysql-python: Not Installed
      pycparser: Not Installed
       pycrypto: 2.6.1
         pygit2: Not Installed
         Python: 2.7.6 (default, Jun 22 2015, 17:58:13)
   python-gnupg: Not Installed
         PyYAML: 3.10
          PyZMQ: 14.0.1
           RAET: Not Installed
          smmap: Not Installed
        timelib: Not Installed
        Tornado: 4.3
            ZMQ: 4.0.4

System Versions:
           dist: Ubuntu 14.04 trusty
        machine: x86_64
        release: 3.13.0-83-generic
         system: Linux
        version: Ubuntu 14.04 trusty

Changing the sls down to one file watch seems to have remedied this particular issue.. but shouldn't multiple files be supported?

start_datadog:
  service.running:
    - name: datadog-agent
    - watch:
      - file: /etc/dd-agent/*

@jakehilton your sls is not complete, can you post the rest? Forgive me if you already know, but if you have a require: file: /some/path then you must have a state in-scope for file: /some/path or you will get a requisites were not found error.

That's the whole sls file.. and I'm not seeing any errors. It works with that single file in the watch. It only breaks down when there are multiple "files" under the watch tag.

And I'm not using require at all.

I'm having this issue as well.

This is failing for me.

/etc/raddb:
  file.directory:
    - user: root
    - group: radiusd
    - mode: '2750'
    - makedirs: True

radiusd:
  service.running:
    - enable: True
    - watch:
      - file: /etc/raddb/*

I get:

          ID: radiusd
    Function: service.running
      Result: False
     Comment: The following requisites were not found:
                                 watch:
                                     file: /etc/raddb/*
     Changes:   

versions-report"

Salt Version:
           Salt: 2016.11.4

Dependency Versions:
           cffi: 1.6.0
       cherrypy: 3.2.2
       dateutil: 1.5
      docker-py: Not Installed
          gitdb: 0.6.4
      gitpython: 1.0.1
          ioflo: Not Installed
         Jinja2: 2.7.2
        libgit2: Not Installed
        libnacl: Not Installed
       M2Crypto: 0.21.1
           Mako: Not Installed
   msgpack-pure: Not Installed
 msgpack-python: 0.4.8
   mysql-python: 1.2.5
      pycparser: 2.14
       pycrypto: 2.6.1
   pycryptodome: 3.4.3
         pygit2: Not Installed
         Python: 2.7.5 (default, Aug  2 2016, 04:20:16)
   python-gnupg: Not Installed
         PyYAML: 3.11
          PyZMQ: 15.3.0
           RAET: Not Installed
          smmap: 0.9.0
        timelib: Not Installed
        Tornado: 4.2.1
            ZMQ: 4.1.4

System Versions:
           dist: redhat 7.3 Maipo
        machine: x86_64
        release: 3.10.0-514.el7.x86_64
         system: Linux
        version: Red Hat Enterprise Linux Server 7.3 Maipo

Found solution here 14047

the watched file name must be the same as the nae/id in file state:

/etc/raddb:
  file.directory:
    - user: root
    - group: radiusd
    - mode: '2750'
    - makedirs: True

radiusd:
  service.running:
    - enable: True
    - watch:
      - file: /etc/raddb*

So removing '/' at the end fixed it :)

It would be nice if glob matching was also supported inside the *_in requisites (and maybe other types?).

Was this page helpful?
0 / 5 - 0 ratings