Last Updated: February 25, 2016
· mlafeldt

Easiest way to parse a cookbook's metadata file

Assuming you have the chef gem installed, it's pretty simple to parse the metadata.rb file that comes with every Chef cookbook:

#!/usr/bin/env ruby

require 'chef/cookbook/metadata'

metadata_file = ARGV.first || 'metadata.rb'

# read in metadata
metadata = Chef::Cookbook::Metadata.new

# output cookbook name and version
puts "#{metadata.name} #{metadata.version}"

# output cookbook dependencies
metadata.dependencies.each { |cookbook, version| puts "#{cookbook} #{version}" }

With pry, we can see what the complete metadata API looks like:

[1] pry(main)> require 'chef/cookbook/metadata'
=> true
[2] pry(main)> metadata = Chef::Cookbook::Metadata.new
=> #<Chef::Cookbook::Metadata:0x00000002a81180
 @description="A fabulous new cookbook",

Last but not least, if you're interested in the actual implementation of Chef's Metadata class, here's the source code.