chef-simple proposal (support for yaml like ansible)

1 min read Original article ↗

I didn't read chef-apply source code. I just explain my idea here (for newbies ?):

How to run: ./chef-simple.rb nginx.yml | sudo chef-apply -s

  • nginx.yml
#apt_repository:
#  ruby-ng:
#    uri: "ppa:brightbox/ruby-ng"

package:
#  nginx:
#  ruby2.2:
  nginx-full:
#  php5-fpm:

directory:
  "/opt/site/1":
    recursive: true
  "/opt/site/2":

service: 
  nginx:
    supports: ":status => true, :restart => true, :reload => true"
#    provider: Chef::Provider::Service::Upstart
    action:
     - :enable
     - :start

file:
  "/tmp/motd":
    owner: '"root"'
    group: '"root"'
    mode: '"0755"'
    content: '"this is a sample file"'
  • chef-simple.rb
#!/usr/bin/env ruby

require "psych"

recipe_path = ARGV[0]
funcs = Psych.load(File.read(recipe_path))

#puts funcs

funcs.each do |func, params|
  params.each do |param,opts|
    if opts
    #p opts
    options = opts.map { |k,v| "#{k} #{v}" }.join("\n  ")
    puts <<-EOH.gsub(/^ {6}/,'')
      #{func} "#{param}" do
        #{options}
      end
      EOH
    else
    puts <<-EOH.gsub(/^ {6}/,'')
      #{func} "#{param}"
      EOH
    end
  end
end
# `chef-apply -s #{STDIN}`