Salt stack using cron
In saltstack, cron is a very useful application in the Linux environment. It enables preset a command or script to run on a specific date and time. It also enables to run an application in a regular interval, says daily, weekly, or every first day of the month. cron start when the system starts and check the /etc/crontab file for configuration details. The /etc/crontab has every application and its schedule.
Salt provides a command-line interface, salt-call to run the modules in the local minion system itself instead of from the master server using the salt command. The salt call CLI supports all the options supported by the salt command but runs locally. Salt Caller was initially designed to support debugging, but now, it can be used as a standalone application.
Using cron with salt
The Salt Minion can initiate its own high state using the salt-call command.
This will cause the minion to check in with the master and ensure it is in the correct "state".
Using salt-call in cron
The salt-call CLI is useful to schedule salt operations using Cron. For example, to check the state of the minion every day at midnight, we can use salt-call along with the option – state.apply as shown below.
0 0 * * * salt-call state.apply
*The state.apply function will check the salt configuration file for the minion and check whether all action defined for the minion is properly configured.
*Setting the path is a good practice because sometimes the salt command may not be available in the system path.
Let's set up a basic crontab in Salt and distribute it to one of our minions.
Create a directory for cron and an empty cron SLS file
- user: root
- minute: '*/2'
This will execute bash /opt/scripts/randomscript.sh command every 2 minutes
By default, if we don't specify a timing value, Salt will implement * for each value in the schedule.
we can implement the cron via
#salt 'ubuntu-01' state.sls cron.testcronjob
And you can view it by