Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

I need to set an environment variable using a Chef recipe. Can you provide an example of how to accomplish this?

like image 303
Brandon Avatar asked Jun 08 '11 20:06

Brandon


People also ask

How will you set an environmental variable?

On the Windows taskbar, right-click the Windows icon and select System. In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables. Click New to create a new environment variable.

What are some examples of environmental variables?

SCRATCH is an example of an environment variable. When used as part of a command they need to be preceded by a '$', hence '$SCRATCH'. They are called environment variables because they are used to modify the environment in which some programs run on the central system.


2 Answers

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' => "/home/#{app_user}"   user app_user   action :run   not_if %[psql -U postgres -c "\\l" | grep #{db_name}] end 

If you're looking to set a persistent environment variable then you may want to have Chef edit /etc/profile.d/chef.sh, /etc/environment, a users' profile, etc.

like image 80
jodell Avatar answered Sep 20 '22 12:09

jodell


If you want to set it on the system with Chef, checkout the magic_shell cookbook.

magic_shell_environment 'RAILS_ENV' do   value 'production' end 
like image 29
sethvargo Avatar answered Sep 19 '22 12:09

sethvargo