Search code examples
amazon-web-servicesautomationansiblecroncron-task

How do you set the timezone in the Ansible cron module?


I have an Ansible playbook that sets up cron jobs.
Because of the structure of the project, we don't use cron.conf files. We need to run a job twice a day at specific times for different EC2 instances in AWS. However, when the clocks go forward and back it causes issues.

I want to ensure that the cron jobs can take place without having to reconfigure the cron jobs when the clocks change.

I'm aware of the CRON_TZ variable however without the cron.conf file, but, I'm unsure where I can insert that variable.
The project uses the Ansible cron module, and the parameters listed in the documentation don't list a section for the CRON_TZ parameter.

Any suggestions on how I can work around this restriction?

My task for the set up of the cron job:

- name: Set-up cron jobs
  cron:
    name: "{{ item.name }}"
    user: "{{ item.username }}"
    minute: "{{ item.time.split(' ')[0] }}"
    hour: "{{ item.time.split(' ')[1] }}"
    day: "{{ item.time.split(' ')[2] }}"
    month: "{{ item.time.split(' ')[3] }}"
    weekday: "{{ item.time.split(' ')[4] }}"
    job: "{{ item.job }}"
    state: "{{ item.state }}"
  with_items: "{{jobs}}"
  notify: "restart cron"

Solution

  • The project uses the Ansible cron module, and the parameters listed in the following link don't list a section for the CRON_TZ parameter.

    Whereby it is possible for some Linux distributions to Use TimeZone with cron tab, for Red Hat based distribution it might not be the case according Where can I set environment variables that crontab will use?. That's probably why there is no such parameter or option.

    Any suggestions on how I can work around this restriction?

    Maybe configure it afterwards with an other module in a second tasks, in example lineinfile.