Cron File Management

146 views
Skip to first unread message

Joe Adams

unread,
Aug 13, 2014, 11:13:08 AM8/13/14
to ansible...@googlegroups.com
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
    command: curl -s https://www.google.com > /root/google.html
  - name: Dev Test
    command: curl -s http://dev.example.com > /dev/null

bartasks:
  - name: Foo 
    command: /bin/foo
  - name:  Bar 
    command: /bin/bar

./templates/cron.j2
MAILTO=em...@domain.tld
{% for job in item %}
# {{ job.name }}
{{ 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

Michael DeHaan

unread,
Aug 14, 2014, 7:44:58 AM8/14/14
to ansible...@googlegroups.com
"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."

If you are writing to different cron.d files, the template that you have there in cron.j2 should be fine.

Also take a look at the using "cron" module which should simplify some things.




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/4f842474-1fe8-4010-9df2-282e88c4242d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages