.profile没有在厨师客户端执行

时间:2014-04-07 09:58:28

标签: chef chef-recipe chef-solo

我的厨师服务器上有以下食谱:

# Insert the environment variables in .profile
execute "set-env-path" do
  command "echo 'export JAVA_HOME=/home/node/jdk1.6.0_45' >> /home/node/.profile && echo 'export PATH=$PATH:$JAVA_HOME/bin' >> /home/node/.profile"
end

# Execute .profile
execute "execute .profie" do
  command ". $HOME/.profile"
end 

当我在客户端上运行配方时,环境变量会插入到.profile中,但它们没有被设置。

node@node-virtual-machine:~$ sudo chef-client
[2014-04-07T14:59:57+05:30] INFO: Forking chef instance to converge...
Starting Chef Client, version 11.10.4
[2014-04-07T14:59:57+05:30] INFO: *** Chef 11.10.4 *** 
[2014-04-07T14:59:57+05:30] INFO: Chef-client pid: 24177
[2014-04-07T14:59:57+05:30] INFO: Run List is [recipe[remote_file]]
[2014-04-07T14:59:57+05:30] INFO: Run List expands to [remote_file]
[2014-04-07T14:59:57+05:30] INFO: Starting Chef Run for node-virtual-machine
[2014-04-07T14:59:57+05:30] INFO: Running start handlers
[2014-04-07T14:59:57+05:30] INFO: Start handlers complete.
[2014-04-07T14:59:58+05:30] INFO: HTTP Request Returned 404 Object Not Found: 
resolving cookbooks for run list: ["remote_file"]
[2014-04-07T14:59:58+05:30] INFO: Loading cookbooks [remote_file]
Synchronizing Cookbooks:
[2014-04-07T14:59:58+05:30] INFO: Storing updated cookbooks/remote_file/recipes/default.rb in the cache.
  - remote_file
    Compiling Cookbooks...
Converging 2 resources
Recipe: remote_file::default
  * execute[set-env-path] action run[2014-04-07T14:59:58+05:30] INFO: Processing execute[set-env-path] action run (remote_file::default line 39)
[2014-04-07T14:59:58+05:30] INFO: execute[set-env-path] ran successfully

- execute echo 'export JAVA_HOME=/home/node/jdk1.6.0_45' >> /home/node/.profile && echo 'export PATH=$PATH:$JAVA_HOME/bin' >> /home/node/.profile

* execute[set-env-path2] action run[2014-04-07T14:59:58+05:30] INFO: Processing execute[set-env-path2] action run (remote_file::default line 44)
[2014-04-07T14:59:58+05:30] INFO: execute[set-env-path2] ran successfully

- execute . $HOME/.profile

[2014-04-07T14:59:58+05:30] INFO: Chef Run complete in 0.511487679 seconds

Running handlers:
[2014-04-07T14:59:58+05:30] INFO: Running report handlers
Running handlers complete

[2014-04-07T14:59:58+05:30] INFO: Report handlers complete
Chef Client finished, 2/2 resources updated in 1.150640709 seconds
node@node-virtual-machine:~$ echo $JAVA_HOME

node@node-virtual-machine:

为什么.profile文件没有在厨师客户端执行,为什么我的环境变量没有通过厨师设置?

1 个答案:

答案 0 :(得分:0)

当Chef运行execute个资源(包括bashscript等)时,它们将在子shell中执行。您只是为子shell设置环境,而不是为Chef Client进程设置环境。如果需要设置环境变量,则需要使用Ruby块和ENV哈希:

ruby_block 'set environment' do
  block do
    ENV['MY_VARIABLE'] = 'MY_VALUE'
  end
end