skip to Main Content

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"

2

Answers


  1. 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.

    Login or Signup to reply.
  2. From what I understand, CRON_TZ is an environment variable in your crontab, which you can set with Ansible thanks to the parameters env, name and value of the cron module.

    So, in your case, you need an extra task that would state:

    - name: Creates an entry like "CRON_TZ=Europe/Brussels" on top of crontab
      ansible.builtin.cron:
        name: CRON_TZ
        env: yes
        value: Europe/Brussels
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search