How can you use a Chef recipe to set an environment variable?

If you need an env var set strictly within the Chef process, you can use ENV[‘foo’] = ‘bar‘ since it’s a ruby process. If you need to set one for an execute provider, Chef exposes an environment hash: execute ‘Bootstrap the database’ do cwd “#{app_dir}/current” command “#{env_cmd} rake db:drop db:create db:schema:load RAILS_ENV=#{rails_env}” environment ‘HOME’ => … Read more

How to move/copy files locally with Chef

How to copy single file First way I use file statement to copy file (compile-time check) file “/etc/init.d/someService” do owner ‘root’ group ‘root’ mode 0755 content ::File.open(“/home/someService”).read action :create end here : “/etc/init.d/someService” – target file, “/home/someService” – source file Also you can wrap ::File.open(“/home/someService”).read in lazy block … lazy { ::File.open(“/home/someService”).read } … Second … Read more

How can I change a file with Chef?

Chef actually allows and uses this. You can find an example in the opscode’s cookbooks/chef-server/recipes/default.rb: ruby_block “ensure node can resolve API FQDN” do block do fe = Chef::Util::FileEdit.new(“/etc/hosts”) fe.insert_line_if_no_match(/#{node[‘chef-server’][‘api_fqdn’]}/, “127.0.0.1 #{node[‘chef-server’][‘api_fqdn’]}”) fe.write_file end not_if { Resolv.getaddress(node[‘chef-server’][‘api_fqdn’]) rescue false } end Here’s the use-case. After the installation from source I had to uncomment lines in some … Read more