Skip to content

Instantly share code, notes, and snippets.

@cdracars
Forked from timdiggins/chef-roles-to-json-task.rb
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdracars/28a63642fc588b707621 to your computer and use it in GitHub Desktop.
Save cdracars/28a63642fc588b707621 to your computer and use it in GitHub Desktop.
rake task to convert existing ruby roles to json. place file outside of roles and run.
# ADD this to Rakefile and run it by issuing rake roles:to_json
require 'chef'
root_dir = File.dirname(__FILE__)
ROLE_DIR = File.join(root_dir, 'roles')
namespace :roles do
desc "Convert ruby roles from ruby to json, creating/overwriting json files."
task :to_json do
Dir.glob(File.join(ROLE_DIR, '*.rb')) do |rb_file|
role = Chef::Role.new
role.from_file(rb_file)
json_file = rb_file.sub(/\.rb$/,'.json')
File.open(json_file, 'w'){|f| f.write(JSON.pretty_generate(role))}
end
end
end
# relates to Chef roles. see http://wiki.opscode.com/display/chef/Roles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment