Search code examples
cronansible

Manage whole crontab files in Ansible


I have a crontab containing around 80 entries on a server. And I would like to manage that crontab using Ansible.

Ideally I would copy the server's crontab to my Ansible directory and create an Ansible task to ensure that crontab is set on the server.

But the cron module only seems to manage individual cron entries and not whole crontab files.

Manually migrating the crontab to Ansible tasks is tedious. And even if I find or make a tool that does it automatically, I feel the YAML file will be far less readable than the crontab file.

Any idea how I can handle that big crontab using Ansible?


Solution

  • I managed to find a simple way to do it. I copy the crontab file to the server and then update the crontab with the shell module if the file changed.

    The crontab task:

    ---
    - name: Ensure crontab file is up-to-date.
      copy: src=tasks/crontab/files/{{ file }} dest={{ home }}/cronfile
      register: result
    - name: Ensure crontab file is active.
      shell: crontab cronfile
      when: result|changed
    

    In my playbook:

    - include: tasks/crontab/main.yml file=backend.cron