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 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