bash_profile没有生效,厨师跑 [英] bash_profile is not taking effect with chef-run

查看:229
本文介绍了bash_profile没有生效,厨师跑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我用厨师运行以下食谱,条目将被添加到〜/ .bash_profile 中,但是当我执行 echo $ PATH echo $ JAVA_HOME ,命令行返回空值。发生了什么?

If I run the below recipe with chef, entries are getting added in ~/.bash_profile, but when I do echo $PATH or echo $JAVA_HOME, the command line returns blank values. What's going on there?

ruby_block  "set-env-java-home" do
  block do
    ENV["JAVA_HOME"] = node['java']['path']
    ENV['PATH'] = "#{ENV['PATH']}:#{node['java']['path']}/bin"
  end
  not_if { $JAVA_HOME == "#{ENV['JAVA_HOME']}" && $PATH = "#{ENV['PATH']}:#{node['java']['path']}/bin" }
end

bash 'set-env-bashrc' do
  code <<-EOH
    echo -e "export JAVA_HOME=$JAVA_HOME" >> ~/.bash_profile
    echo -e "export PATH=$PATH" >> ~/.bash_profile
    source ~/.bash_profile
  EOH
end


推荐答案

这里有一些事情:

not_if { $JAVA_HOME == "#{ENV['JAVA_HOME']}" && $PATH = "#{ENV['PATH']}:#{node['java']['path']}/bin" }

你在这里混淆Ruby和bash。 IN Ruby $ JAVA_HOME 是指一个全局常量。我不清楚你的 not_if guard正在试图完成,但是按原样使用它肯定是不正确的。

You are confusing Ruby and bash here. IN Ruby $JAVA_HOME refers to a global constant. It's unclear to me what your not_if guard is trying to accomplish, but using it as-is is certainly not correct.

您正在使用Bash将内容回送到文件中。对于文件模板资源,这是一个完美的工作。这将是幂等的,并正确处理通知:

You are using Bash to echo content into a file. This is a perfect job for the file or template resources. The will be idempotent and handle notifications properly:

file '~/.bash_profile` do
  content <<-EOH
    export JAVA_HOME=$JAVAHOME
    export PATH=$PATH
  EOH
end

但是,值得注意的是,脚本与您没有任何关系。您正在为自己设置 JAVA_HOME PATH 的值。你可能想要使用#{ENV ['JAVA_HOME']}

However, it is worth noting that the script as you have it does nothing. You are setting the values of JAVA_HOME and PATH to themselves. It is likely that you want to use #{ENV['JAVA_HOME']} instead.

当您使用 bash (或任何执行资源)时,您正在子shell中执行,所以 source export 之类的东西不会保留到父进程。

When you use the bash (or any execute resource), you are executing in a subshell, so things like source or export are not persisted to the parent process.

这篇关于bash_profile没有生效,厨师跑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆