WATCH REQUISITES IN SALTSTACK:
We are going to see the simple example for watch requisites in saltstack here, The watch statements are used to add additional behavior when there are changes in other states
A good example of using watch is with a service.running state.
When a service watches a state, then the service is reloaded/restarted when the watched state changes, in addition to Salt ensuring that the service is running. To verify the minion is responding,
salt ‘first-minion’ test.ping
Our state file looks like,
# cat httpd_watch.sls
apache_service:
service.running:
– name: httpd.service
– require:
– pkg: apache_service
– watch:
– file: apache_service
– file: modify_content
pkg.installed:
– name: httpd
file.managed:
– source: salt://rnd1.conf
– name: /etc/httpd/conf.d/rnd1.conf
– require:
– pkg: apache_service
modify_content:
file.managed:
– source: salt://index.html
– name: /home/rnd1/index.html
– user: apache
– group: apache
– mode: 644
– require:
– pkg: apache_service
watched state files:
rnd1.conf file contains:
<VirtualHost 192.168.233.21:8081>
# ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /home/rnd1
DirectoryIndex index.html
# ServerName server1.times.com
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
<Directory />
AllowOverride none
Require all denied
</Directory>
<Directory /home/rnd1>
AllowOverride none
Require all granted
</Directory>
</VirtualHost>
Index.html file contains:
<h1> My new page </h1>
watch requisites,
apache_service:
service.running:
– name: httpd.service
– require:
– pkg: apache_service
– watch:
– file: apache_service
– file: modify_content
In the state file we have placed two states under the watch requisites, As per the state file if any changes on the apache_service or modify_content state then the apache service will be restarted / reloaded.
- We have already installed apache in the salt-minion and done few configurations like,
- Added Listen server_IP:8081
- Allowed 8081 in firewall
Let us trigger the state file on the target by,
salt ‘first-minion’ state.sls httpd_watch
Apache service has been restarted since the apache_service and modify_content states were changed.
Let us check the web page:
When we execute the state file without any modification on watched state then the result looks like,
Let us modify the watched states:
salt ‘first-minion’ state.sls httpd_watch
Modification on the rnd1.conf file:
Modification on the index.html file:
Result on the web page:
As we already mentioned if there is no changes on the watched states then the service will not restarted instead of showing the service’s status.