openstack-ansible/playbooks/roles/system_crontab_coordination/templates/crontab.j2
Jimmy McCrory 953938a29d Make system_crontab_coordination idempotent
Use a seed in the system_crontab_coordination role template's random
filter so that each host's results are idempotent when re-running.
This prevents the constant changing of the crontab whenever the
playbook is run.

Change-Id: I283f7562adf5984498ad712cf030bbb21a0e6a9c
2018-10-19 11:28:27 +00:00

52 lines
3.7 KiB
Django/Jinja

# {{ ansible_managed }}
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
{% macro normalise(value, start, end, minimum, maximum, period) -%}
{% set v = ((value | int) % (period | int)) + (start | int) %}
{%- if ((v | int) > (maximum | int)) -%}
{{ ((v | int) - (maximum | int) + (minimum | int) - 1) }}
{%- else -%}
{{ v }}
{%- endif -%}
{%- endmacro %}
{% macro set_period(start, end, maximum, minimum) -%}
{{ (((start | int) > (end | int)) | ternary(((maximum | int) - (minimum | int) - (start | int) + (end | int) + 2), ((end | int) - (start | int) + 1))) | int }}
{%- endmacro %}
{% macro set_offset(period, hosts) -%}
{%- if ((hosts | length) | int) > (period | int) -%}
0
{%- else -%}
{{ (((period | int) / ((hosts | length) | int)) | int) | random(seed=inventory_hostname) }}
{%- endif -%}
{%- endmacro %}
{% for host in ansible_play_hosts %}
{% if host == inventory_hostname %}
{% set minute_period = set_period(minute_start, minute_end, 59, 0) | int %}
{% set minute_offset = set_offset(minute_period, ansible_play_hosts) | int %}
{% set number_of_minute_jobs = (4 | int) %}
{% set m = normalise((((minute_period * loop.index) / loop.length) + ((minute_offset | int) / (number_of_minute_jobs | int))), minute_start, minute_end, 0, 59, minute_period) | int %}
{% set hour_period = set_period(hour_start, hour_end, 23, 0) | int %}
{% set hour_offset = set_offset(hour_period, ansible_play_hosts) | int %}
{% set number_of_hour_jobs = (3 | int) %}
{% set h = normalise((((hour_period * loop.index) / loop.length) + ((hour_offset | int) / (number_of_hour_jobs | int))), hour_start, hour_end, 0, 23, hour_period) | int %}
{% set day_of_week_period = set_period(day_of_week_start, day_of_week_end, 6, 0) | int %}
{% set day_of_week_offset = set_offset(day_of_week_period, ansible_play_hosts) | int %}
{% set dow = normalise((((day_of_week_period * loop.index) / loop.length) + (day_of_week_offset | int)), day_of_week_start, day_of_week_end, 0, 6, day_of_week_period) | int %}
{% set day_of_month_period = set_period(day_of_month_start, day_of_month_end, 28, 1) | int %}
{% set day_of_month_offset = set_offset(day_of_month_period, ansible_play_hosts) | int %}
{% set dom = normalise((((day_of_month_period * loop.index) / loop.length) + (day_of_month_offset | int)), day_of_month_start, day_of_month_end, 1, 28, day_of_month_period) | int %}
{{ m }} * * * * root cd / && run-parts --report /etc/cron.hourly
{{ normalise((m + (minute_period * 1 / (number_of_minute_jobs | int))), minute_start, minute_end, 0, 59, minute_period) }} {{ h }} * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
{{ normalise((m + (minute_period * 2 / (number_of_minute_jobs | int))), minute_start, minute_end, 0, 59, minute_period) }} {{ normalise((h + (hour_period * 1 / (number_of_hour_jobs | int))), hour_start, hour_end, 0, 23, hour_period) }} * * {{ dow }} root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
{{ normalise((m + (minute_period * 3 / (number_of_minute_jobs | int))), minute_start, minute_end, 0, 59, minute_period) }} {{ normalise((h + (hour_period * 2 / (number_of_hour_jobs | int))), hour_start, hour_end, 0, 23, hour_period) }} {{ dom }} * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
{% endif %}
{% endfor %}
#