I'm building a cron server that will basically just serve as a central server to run cron jobs from. I'm having trouble planning/implementing the cron functionality in ansible. There will likely be many jobs under many categories, so I would rather not make a play for each job, especially as they could change often. I was thinking about making a template for a cron file and using the template module with some variables to make categorized cron files under /etc/cron.d/. I can't seem to figure out how the variables will work without needing to make separate templates for each category just to get the variables as a whole object. Example:
./vars/main.yml
---
footasks:
- name: Google Test
- name: Dev Test
bartasks:
- name: Foo
command: /bin/foo
- name: Bar
command: /bin/bar
./templates/cron.j2
MAILTO=em...@domain.tld
{% for job in item %}
{{ job.minute|default("*") }} {{ job.hour|default("*") }} {{ job.dom|default("*") }} {{ job.month|default("*") }} {{ job.dow|default("*") }} {{ job.user|default("root") }} {{ job.command }}
{% endfor %}
./tasks/main.yml
---
- name: Setup Foo Cron Jobs
template: src=cron.j2 dest=/etc/cron.d/foo
with_items: footasks
- name: Setup bar Cron Jobs
template: src=cron.j2 dest=/etc/cron.d/bar
with_items: bartasks
The problem I get is that this error when I try to run the template play:
fatal: [test] => {'msg': "One or more undefined variables: 'str object' has no attribute 'name'", 'failed': True}
If I just change the template to {{item}} to output whatever is input, the play loops over each job in the variable and the end file only contains the last job. I have tried every method I can find to attempt to pass the whole variable/list into the template and can't figure it out. I've tried nesting the variables as well as using with_nested. with_nested got me the closest, but I couldn't get it to work still. Any ideas on how I can pass the whole variable? Am I going about this all wrong?
Thanks