Chef cookbook - reload PATH

梦想与她 提交于 2019-12-01 07:12:35

问题


I just installed java using chef cookbook and updated PATH environment variable for all users (added new file to /etc/profile.d/).

Is it possible to tell chef to reload PATH variable?

When I do something like this:

execute "java_check" do
  command "java -version"
end

Is says that java could not be found.

It works fine when I log out, log in again and then run chef recipe.


回答1:


I'm not 100% sure you can update the PATH variable for future chef runs, but you can set it up manually using the environment attribute within the execute stanza. This can also be used on other Resources as well. See: http://docs.opscode.com/chef/resources.html#execute

From the Chef Docs,

environment
A hash of environment variables: {"ENV_VARIABLE"=>"VALUE"}.
(These environment variables must exist for a command to execute successfully.)
Default value: nil.

Run a command which requires an environment variable

execute "slapadd" do
  command "slapadd < /tmp/something.ldif"
  creates "/var/lib/slapd/uid.bdb"
  action :run
  environment ({'HOME' => '/home/myhome'})
end



回答2:


I found that it is not possible to update ENV variables pemanently (to be available after chef finishes), but it is possible to update variables for future commands of current chef run.

ruby_block  "set-env-" do
  block { ENV[variable_name] = variable_value }
  not_if { ENV[variable_name] == variable_value }
end



回答3:


execute "run_updated_bash" do
    command "bash /etc/profile.d/myscript.sh"
end

Have you tried something like this? It could be run after you place your file in /etc/profile.d/



来源:https://stackoverflow.com/questions/17313587/chef-cookbook-reload-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!