how to make chef execute with a specific user and

2019-06-16 07:19发布

问题:

Hi I am creating a WCS instance, for which i have to execute the create instance command using the wcs user (webadmin), its failing to connect to DB as its not able to get the required env variables.

so i put some sample code to check

I am using the below code

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

For whoami i am getting the user

webadmin

But for env i am getting the env of the user "root", its not sourcing the .bash_profile for the env variables. Any ideas

回答1:

There is an environment attribute in the bash resource. Or you can source the .bash_profile in the script. That's one of the things you can do with bash (last example)



回答2:

Seems like adding flags '-l' to tell bash to act as a login shell also does the trick.

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

Or using the execute block:

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