I'm just starting to deploy SaltStack to my servers. So, I have the following file /srv/salt/postfix/init.sls
:
# Ensure postfix installed and set to autostart on boot
postfix:
pkg.installed: []
service.running:
- require:
- pkg: postfix
- enable: True
# Restart on change to main.cf or any of the *.db files
postfix.restart:
service.running:
- require:
- pkg: postfix
- name: postfix
- watch:
- file: "/etc/postfix/main.cf"
- file: "/etc/postfix/*.db"
# Without this, first watch above fails
/etc/postfix/main.cf:
file.exists: []
/etc/postfix/canonical:
file.managed:
- require:
- pkg: postfix
- source: salt://postfix/canonical
- template: jinja
/etc/postfix/canonical.db:
cmd.wait:
- name: /usr/sbin/postmap /etc/postfix/canonical
- watch:
- file: /etc/postfix/canonical
Basically, I want to restart Postfix whenever the /etc/postfix/canonical.db
file changes.
But whenever I run this state, I always get an error for the postfix.restart
state:
ID: postfix.restart
Function: service.running
Name: postfix
Result: False
Comment: The following requisites were not found:
watch:
file: /etc/postfix/*.db
Started:
Duration:
Changes:
Other states run perfectly.
I'm a beginner w.r.t. SaltStack, so please kindly assist me in finding out where did I go wrong… and if I actually have written a 'proper' SaltStack formula.
Thank you!
PS: salt --version
returns salt 2015.5.0 (Lithium)
, and I'm on Ubuntu.
PPS: Changing from *.db
to canonical.db
does not change the result: Still error on that watch:
requisite.
PPPS: I originally put the cmd.wait
stanza under the /etc/postfix/canonical
job, and separated it into a different job because I thought the error was due to watch:
not finding the job.
UPDATE: EPILOGUE
I have given up the original strategy, and use a strategy based on GNU make
instead.
Reference: http://www.unixwiz.net/techtips/postfix-makefiles.html
So the init.sls
now looks like this:
postfix:
pkg.installed: []
service.running:
- require:
- pkg: postfix
- enable: True
make:
pkg.installed: []
/etc/postfix/Makefile:
file.managed:
- require:
- pkg: postfix
- pkg: make
- source: salt://postfix/Makefile
refresh_db:
cmd.run:
- require:
- pkg: postfix
- pkg: make
- name: make
- cwd: /etc/postfix
- order: last ## IMPORTANT! This will force this particular job to run at the very last
/etc/postfix/canonical:
file.managed:
- require:
- pkg: postfix
- source: salt://postfix/canonical
- template: jinja
Thank you for trying to answer my question!
Best Answer
The
watch: file:
requisite doesn't refer to a file change on the filesystem, but to a change in your defined states during their execution.In this case, you have to watch the
state
:If you want to react on an external change on the
*.db
files, you must use another way. You may have a look at the Reactor system: https://docs.saltstack.com/en/latest/topics/reactor/index.html. I don't know much about it but I think it can be used to react on filesystem changes.