Scheduling with the at Module
Quick one-time scheduling is done with the at module. You create the job for a future time to run and it is held until that time comes to execute. There are six parameters that come with this module. They are: command, count, script_file, state, unique, and units.
The at Module Example
- name: remove tempuser. at: command: userdel -r tempuser count: 20 units: minutes unique: yes
|command||Null||A command that is scheduled to run.|
|count||Null||The count of units. (Must run with units)|
|script_file||Null||An existing script file to be executed in the future.|
|state||absent, present||The state adds or removes a command or script.|
|unique||yes, no||If a job is already running, it will not be executed again.|
|units||minutes/hours/days/weeks||The time denominations.|
Appending Commands with the cron Module
When setting a jobs scheduled task the cron module is used. The cron module will append commands directly into the crontab of the user you designate.
The cron module example
- cron: name: "Flush Bolt" user: "root" minute: 45 hour: 11 job: "php ./app/nut cache:clear"
This play uses a company’s cache:clear command immediately flushes Bolt cache, removing cached files and directories.flushes cache of the CMS server every morning at 11:45. Ansible will write the play to the crontab using the correct syntax as the user stated. Checking the crontab will verify that it has been appended to. Some commonly used parameters for the cron module are:
|special_time||reboot, yearly, annually, monthly, weekly, daily, hourly||A set of reoccurring times.|
|state||absent, present||If set to present, it will create the command. Absent will remove it.|
|cron_file||Null||If you have large banks of servers to maintain then sometimes it is better to have a pre-written crontab file.|
|backup||yes, no||Backs up the crontab file prior to being edited.|