The legacy method of running crontab jobs from files located at /var/spool/cron (edited using ‘crontab -e’) is slowly giving way to files located in /etc/cron.d.
This way of creating a file entry for each job is easily handled by Ansible using the cron module as shown below.
- name: Creates a cron file under /etc/cron.d cron: name: from-inside-cron day: "*" minute: "*" hour: "*" user: root job: "foo=bar test=this /tmp/from-inside-cron.sh" cron_file: from-inside-cron state: present
The ‘cron_file’ parameter dictates the file created under ‘/etc/cron.d’, and the ‘job’ parameter allows specifying the script, parameters, and environmental variables you may wish to pass.
As with all scripts called from cron, remember you will not have access to shell level environment variables and paths, so be sure to use full paths and be explicit in passing your environment variables.
Here is a playbook-ubuntu-cron.yml demonstrating a full test. It creates a local cron that executes /tmp/from-inside-cron.sh every minute that will log to /tmp/from-inside-cron.log.
REFERENCES