🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Chef Tutorials: How to set environment variable in Chef recipe


Method #1


If you need an env var set strictly within the Chef process, you can use ENV['foo'] = 'bar' since it's a ruby process.

Method #2
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

Chef Tutorials: How to set environment variable in Chef recipe
Method #1
If you need an env var set strictly within the Chef process, you can use ENV['foo'] = 'bar' since it's a ruby process.
Method #2
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
Method #3
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.
Method #4
If you want to set it on the system with Chef, checkout the magic_shell cookbook.magic_shell_environment is fantastic, with one gotcha. Any command invoked with system("/new/process/to/call") from a ruby script will not pull from /etc/profile.d/* automatically.
magic_shell_environment 'RAILS_ENV' do
value 'production'
end
Method #5
If you want to set it at the system level in /etc/environment, you can do so directly per the following example without adding an additional recipe (this adds two env variables for Java):
sys_env_file = Chef::Util::FileEdit.new('/etc/environment')
{
'JAVA_HOME' => '/usr/lib/jvm/java-1.7.0-openjdk-amd64',
'LD_LIBRARY_PATH' => '/usr/lib/jvm/java-1.7.0-openjdk-amd64/lib'
}.each do |name, val|
sys_env_file.insert_line_if_no_match /^#{name}\=/, "#{name}=\"#{val}\""
sys_env_file.write_file
end
Method #6 - in Windows
windows_env 'CHEF_LICENSE' do
value 'accept'
end
Subscribe
Notify of
guest


0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x