Skip to content

Instantly share code, notes, and snippets.

@sneal
Created February 6, 2014 02:43
Show Gist options
  • Save sneal/8837600 to your computer and use it in GitHub Desktop.
Save sneal/8837600 to your computer and use it in GitHub Desktop.
Script to generate a new Chef environment file using the latest cookbook versions from your Chef Server
require 'json'
env_name = ARGV[0]
raise "environment name is a required parameter" unless env_name
environment = Hash.new
environment[:name] = env_name
environment[:description] = "The #{env_name} environment"
cookbook_versions = Hash.new
`knife cookbook list`.split("\n").each do |line|
cookbook_parts = line.strip.split(/\s+/)
cookbook_versions[cookbook_parts[0]] = cookbook_parts[1]
end
environment[:cookbook_versions] = cookbook_versions
environment[:json_class] = "Chef::Environment"
environment[:chef_type] = "environment"
environment[:default_attributes] = Hash.new
environment[:override_attributes] = Hash.new
puts JSON.pretty_generate(environment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment