Reputation: 5278
I have two chef cookbooks (and potentially more) that needs a unique id (integer) when executed, in best case they are sequential.
Are there any possibilities to have a central storage of variables, a global variable which is not a constant? That can be incremented each time my default recipe of a cookbook is executed?
Upvotes: 0
Views: 257
Reputation: 54267
There is no good way to do this with purely Chef. The usual approaches are either to allocate the ID numbers by hand up front in node attributes or to use a hash of the node name or some other stable value. node.name.hash % 1000
would give you something between 0 and 1000 that is roughly evenly distributed. Unfortunately the name hashing isn't designed with collision resistance in mind so on smaller ranges (like 0-256) it can have an unfortunate streak.
If you really want to do this "properly" look at tools like Consul and ZooKeeper which can issue sequential numbers safely in a distributed situation.
Upvotes: 1