The cron state module allows for user crontabs to be cleanly managed.
Cron declarations require a number of parameters. The timing parameters need to be declared: minute, hour, daymonth, month, and dayweek. The user whose crontab is to be edited also needs to be defined.
By default, the timing arguments are all * and the user is root. When making changes to an existing cron job, the name declaration is the unique factor, so if an existing cron that looks like this:
date > /tmp/crontest:
cron.present:
- user: root
- minute: 5
Is changed to this:
date > /tmp/crontest:
cron.present:
- user: root
- minute: 7
- hour: 2
Then the existing cron will be updated, but if the cron command is changed, then a new cron job will be added to the user's crontab.
Additionally, the temporal parameters (minute, hour, etc.) can be randomized by using random instead of using a specific value. For example, by using the random keyword in the minute parameter of a cron state, the same cron job can be pushed to hundreds or thousands of hosts, and they would each use a randomly-generated minute. This can be helpful when the cron job accesses a network resource, and it is not desirable for all hosts to run the job concurrently.
/path/to/cron/script:
cron.present:
- user: root
- minute: random
- hour: 2
New in version 0.16.0.
Since Salt assumes a value of * for unspecified temporal parameters, adding a parameter to the state and setting it to random will change that value from * to a randomized numeric value. However, if that field in the cron entry on the minion already contains a numeric value, then using the random keyword will not modify it.
Verifies that the specified cron job is absent for the specified user; only the name is matched when removing a cron job.
Provides file.managed-like functionality (templating, etc.) for a pre-made crontab file, to be assigned to a given user.
The source file to be used as the crontab. This source file can be hosted on either the salt master server, or on an HTTP or FTP server. For files hosted on the salt file server, if the file is located on the master in the directory named spam, and is called eggs, the source string is salt://spam/eggs.
If the file is hosted on a HTTP or FTP server then the source_hash argument is also required
Verifies that the specified cron job is present for the specified user. For more advanced information about what exactly can be set in the cron timing parameters, check your cron system's documentation. Most Unix-like systems' cron documentation can be found via the crontab man page: man 5 crontab.