SaltStack - Using Cron with Salt



Salt can be used along with the Cron application. Using both applications together provides a great opportunity to automate Salt. While Salt provides an option to execute commands remotely, Cron enables it to run in a pre-scheduled or automated manner. Let us learn how to use Cron and Salt together in this chapter.

What is Cron?

Cron is very useful application in the Linux Environment. It enables to preset a command or script to run in a specific date and time. It also enables to run an application in a regular interval, say daily, weekly or every first day of the month.

Cron starts when the system starts and check the /etc/crontab file for configuration details. The /etc/crontab has every application and its schedule in a separate line as shown below.

15 * * * * root echo "This command runs at 15 minutes past every hour"
15 10 * * * root echo "This command is run daily at 10:15 am"

Every line has the following seven entry points, which are separated by space and they are as follows −

  • minute − minute of the hour and is between ‘0’ and ‘59’.

  • hour − hour and is specified in the 24-hour clock.

  • day_of_month − Day of the Month and is between 1 and 31. For example, the 10th of each month is 10.

  • month − A month specified and is specified numerically (0-12), or as the name of the month (e.g. May).

  • day_of_week − Day of the week is specified numerically (0-7) or as the name of the day (e.g. Sun).

  • user − User account under which the command runs.

  • cmd − The actual command and its arguments.

Here, * replaces, if nothing is assigned.

Salt Caller (salt-call)

Salt provides a CLI (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 salt command, but run locally.

Salt Caller was initially designed to support debugging, but now, it can be used as a standalone application.

salt-call test.ping

Using salt-call in cron

The salt-call CLI is useful to schedule salt operation 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.

/etc/crontab

PATH = /bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/opt/bin
0 0 * * * salt-call state.apply

Here,

  • 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.

In the next chapter, we will learn Remote Execution, which is a core concept of Salt.

Advertisements