diff --git a/tasks/job.yml b/tasks/job.yml index bf5221e..4ba7823 100644 --- a/tasks/job.yml +++ b/tasks/job.yml @@ -1,22 +1,20 @@ -- set_fact: +- name: store job data + set_fact: job: '{{ {}|combine(cron.defaults, item.value, {"name": item.key}, recursive=True) }}' randomseed: "{{ inventory_hostname + item.key }}" -- when: job.special_time == "random_daily" +- name: expand special random keywords to real values + when: job.special_time in ["random_daily", "random_weekly"] set_fact: - job: '{{ job|combine({"weekday": "*"}) }}' + job_random_values: + weekday: "{% if job.special_time == 'random_weekly' %}{{ job.random_options.weekday | random(seed=(randomseed + 'weekday')) }}{% else %}*{% endif %}" + hour: "{{ job.random_options.hour | random(seed=(randomseed + 'hour')) }}" + minute: "{{ job.random_options.minute | random(seed=(randomseed + 'minute')) }}" -- when: job.special_time == "random_weekly" +- name: merge random values back into job + when: job.special_time in ["random_daily", "random_weekly"] set_fact: - job: '{{ job|combine({"weekday": job.random_options.weekday | random(seed=(randomseed + "weekday"))}) }}' - -- when: job.special_time == "random_daily" or job.special_time == "random_weekly" - set_fact: - job: '{{ job|combine({"hour": job.random_options.hour | random(seed=(randomseed + "hour"))}) }}' - -- when: job.special_time == "random_daily" or job.special_time == "random_weekly" - set_fact: - job: '{{ job|combine({"minute": job.random_options.minute | random(seed=(randomseed + "minute"))}) }}' + job: '{{ {}|combine(job, job_random_values, recursive=True) }}' - name: add cron job users to node-exporter-textfile group when: cron.enable_monitoring @@ -25,7 +23,8 @@ groups: node-exporter-textfile append: yes -- when: cron.enable_monitoring +- name: prepend cronjob monitoring command + when: cron.enable_monitoring set_fact: job: '{{ job|combine({"job": "/usr/local/bin/monitor-cronjob.py %s %s %s"%(job.name|quote, ("%s %s * * %s"%(job.minute, job.hour, job.weekday))|quote, job.job|quote)}) }}'