Search code examples
cronchef-infra

Convergently configure crontab with Chef


I would like to set my crontab in a convergent way using Chef. That is, I'd like to specify a list of cronjobs in my cookbook, and have Chef modify my crontab so that it includes only those entries, creating and deleting lines from the crontab as necessary.

How can I do this?

The built-in cron resource doesn't seem fit for the task; its resources are individual cron jobs, and take either a :create or :delete action; I can't have it automatically remove entries from the crontab when I remove them from my cookbook unless I explicitly include a :delete action, and I don't want to have to list :delete actions for every crontab I've removed from my cookbook throughout history.

The cron cookbook from the Chef Supermarket seems unlikely to solve this problem either, since it claims to support the same interface as the built-in cron resource.


Solution

  • This is not explicitly named, but there are two general schools of thought in Chef resource design: "managed resources" vs. "managed collections". With a managed collection, you are convergently defining the entire state of the collection rather than a single object within it. This collection approach seems to be the one you are looking for, but it is generally avoided by the Chef community (and all core code) because it is extremely error-prone. There are a lot of reasons an object might not be visible within the Chef run (partial runs, composite runs, etc) and as the saying goes "absence of evidence is not evidence of absence". That said, some users (Facebook) have used the collections pattern to great effect thanks to heavy code review and training about the pitfalls. Check out the https://github.com/nvwls/zap cookbook for an implementation of a zap_crontab resource that might fit your needs.