如何使用Chef设置环境变量?

时间:2022-06-23 23:17:06

Theres a similar question to this, but cant manage it to work: I want to simply set an env variable, then use it:

这是一个类似的问题,但无法管理它:我想简单地设置一个env变量,然后使用它:

execute "start zookeeper" do
    cwd "/opt/zookeeper-3.4.5/bin"
    command "./zkServer.sh start"
    environment "JVMFLAGS" => "-Xmx#{heap_jvm} -Xms#{heap_jvm}"
    user "root"
    action :run
end

I've also tried using bash to "export JVMFLAGS='-blabla'" but still it runs the sh with none set to the variable. Is there some issue preventing my sh script from checking the variable? I could use the sh like a template and replace the ocurrence of JVMFLAGS... But i want to check if theres a better solution..

我也尝试使用bash“导出JVMFLAGS =' - blabla'”但仍然运行sh而没有设置变量。是否存在阻止我的sh脚本检查变量的问题?我可以像模板一样使用sh并替换JVMFLAGS的发生...但我想检查是否有更好的解决方案..

1 个解决方案

#1


7  

Have you tried setting environment variable through Ruby just before the execute block? Chef actually recommends using ENV (See the note on that page).

您是否尝试在执行块之前通过Ruby设置环境变量? Chef实际上建议使用ENV(参见该页面上的注释)。

ENV['JVMFLAGS'] = "-Xmx#{heap_jvm} -Xms#{heap_jvm}"

Another possibility is to add JVMFLAGS to the command itself.

另一种可能性是将JVMFLAGS添加到命令本身。

execute "start zookeeper" do
  [...]
  command "JVMFLAGS=-Xmx#{heap_jvm} -Xms#{heap_jvm} ./zkServer.sh start"
  [...]
end

#1


7  

Have you tried setting environment variable through Ruby just before the execute block? Chef actually recommends using ENV (See the note on that page).

您是否尝试在执行块之前通过Ruby设置环境变量? Chef实际上建议使用ENV(参见该页面上的注释)。

ENV['JVMFLAGS'] = "-Xmx#{heap_jvm} -Xms#{heap_jvm}"

Another possibility is to add JVMFLAGS to the command itself.

另一种可能性是将JVMFLAGS添加到命令本身。

execute "start zookeeper" do
  [...]
  command "JVMFLAGS=-Xmx#{heap_jvm} -Xms#{heap_jvm} ./zkServer.sh start"
  [...]
end