How to set environment variable using Chef?
Have you tried setting environment variable through Ruby just before the execute block? Chef actually recommends using ENV (See the note on that page).
ENV['JVMFLAGS'] = "-Xmx#{heap_jvm} -Xms#{heap_jvm}"
Another possibility is to add JVMFLAGS to the command itself.
execute "start zookeeper" do
[...]
command "JVMFLAGS=-Xmx#{heap_jvm} -Xms#{heap_jvm} ./zkServer.sh start"
[...]
end
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' => "/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.
Chef: set an environment variable to an attribute
As far as the information given in question is concerned, it should work like that by changing default with node.
Define an attribute file , such as:
default['mycookbook']['myvar'] = '3'
And then an environment variable in the recipe, should be like:
# not "default['mycookbook']"['myvar']
ENV['MY_VAR'] = node['mycookbook']['myvar']
Chef: Setting environment variables
The other question doesn't really explain the underlying problem. Unix environment variables are inherited from parent process to child process during a fork()
. So when you set things via ENV
in your Chef code, it does affect subprocesses that come under Chef like things run via an execute
resource. When you log in via SSH, however, your shell is not a subprocess of chef-client
so those variables aren't visible. Unfortunately Unix has no general purpose solution for global environment variables so you need to pick one of a number of compromises, which are listed in the other question.
How can you use a Chef recipe to set an environment variable by reading from Shell script?
If you want the output (stdout) of the get_password.sh
script into an environment variable, you can simply do:
ENV['password'] = `/opt/get_password.sh root`
Then this environment variable can be referenced in other places of the recipe.
Unable to set environment variables using Chef tomcat
The env_vars
property of tomcat_service
custom resource sets the environment variables for the Tomcat service instance_name
. These are not SHELL environment variables.
tomcat_service sets up the installed tomcat instance to run using the appropriate init system (sys-v, upstart, or systemd)
In CentOS services are handled by systemctl
and hence these variables are added in the respective Systemd service file.
Example:
/etc/systemd/system/tomcat_helloworld.service
Snipped contents:
Environment="CATALINA_BASE=/opt/tomcat_helloworld"
Environment="CATALINA_PID=/opt/tomcat_helloworld/bin/non_standard_location.pid"
Environment="SOMETHING=some_value"
Related Topics
How to Write Bom Marker to a File in Ruby
Rails: Wkhtmltopdf Runtimeerror (Location of Wkhtmltopdf Unknown)
Exec': String Contains Null Byte (Argumenterror)
Need Help Maximizing 3 Factors in Multiple, Similar Objects and Ordering Appropriately
Alter $Path in Vim/Macvim So as to Find the Right Ruby Binary
Ruby Copy a Paperclip Attachment from One Model to Another
Find Case-Insensitive Word Matches in a Line
How to Round a Float to a Specified Number of Significant Digits in Ruby
HTML Is Read Before Fully Loaded Using Open-Uri and Nokogiri
How to Input Multibyte Characters in Rails Console (Or Irb)
Could Not Find Rake-10.0.4 in Any of the Sources (Bundler::Gemnotfound)
How to Use Regex for Utf8 in Ruby
Configuration in Allure to Cucumber in Ruby
Ruby Elegant Way to Return Min/Max If Value Outside Range
How to Know the Current Rake Task