如何让厨师执行特定用户并加载其环境值

时间:2013-07-12 11:55:05

标签: chef chef-recipe

您好我正在创建一个WCS实例,我必须使用wcs用户(webadmin)执行create instance命令,因为它无法获取所需的env变量而无法连接到DB。

所以我提供了一些示例代码来检查

我正在使用以下代码

bash "wcs-create-instance" do
    user "webadmin"
    group "webspher"
    code <<-EOH        
        ###{node[:websphere][:wcs][:wcs_installLocation]}/bin/config_ant.sh -DinstanceName=#{node[:websphere][:wcs][:wcs_instance]} CreateInstance  
    whoami > /tmp/whoami
    env > /tmp/env              
EOH
    notifies :run, "bash[fix-permission]", :immediately 
    #This not_if is just temporary, a proper mechanism has to be implemented here to loop through all the WCS APars,
    #For the POC keeping it neat and simple such that this does not rerun on execution
    not_if {File.directory?("#{node[:websphere][:wcs][:wcs_installLocation]}/instances/#{node[:websphere][:wcs][:wcs_instance]}/starterstores")}
    #action :nothing
end

对于whoami我正在获取用户

  

webadmin的

但是对于env我得到了用户“root”的env,它没有为env变量获取.bash_profile。任何想法

2 个答案:

答案 0 :(得分:7)

bash resource中有environment个属性。或者您可以在脚本中获取.bash_profile。这是one of the things you can do with bash(最后一个例子)

答案 1 :(得分:0)

看起来像添加flags '-l'告诉bash充当登录shell一样。

bash 'do something' do
  code 'my_command'
  flags '-l'
end

或使用execute块:

execute 'foo' do
  command 'bash -l -c "my_command"'
end