How to query cookbook versions on a node?

If you can ssh into the box you can look under /var/chef/cache/cookbooks/<cookbook name>/metadata.json to find the version.

Also, you can access it during a chef run by looking at @run_context.cookbook_collection, but that probably doesn't help.

Generally the cookbook version is defined by the environment, but since environments change over time, you can't really trust that to be the same set that was used when this node last converged (especially if it's been a while).

By far your safest option will be to look at the chef cache.


Came across this post and ended up working out a grep command to do this.

sudo grep -o -e '\"version\"\:\"[a-zA-Z0-9.]*\"' -e '\"version\"\: \"[a-zA-Z0-9.]*\"' /var/chef/cache/cookbooks/*/metadata.json

In our organisation we use a base cookbook to set an attribute on the node with the cookbook versions.

run_context.cookbook_collection.each do |key, cookbook|
  node.set['base_cookbook']['cookbook_versions'][cookbook.name] = cookbook.version
end

Then we can query the versions used by a node with

knife node show <node-name> -a base_cookbook.cookbook_versions

If you're using ohai (you probably are), you can do something like this:

knife search -i 'cookbooks:your-cookbook' -a cookbooks.your-cookbook.version

This will give you output that shows the hostname and the cookbook version:

1 items found

server.name.example:
  cookbooks.cs-redis.version: 0.3.2