Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cant get chef to run bash under a certain user

Given the following chef cookbook snippet:

bash "source a file" do
  user "someUser"
  cwd "/tmp"
  code <<-EOH
source /tmp/test.sh
  EOH
end

where /tmp/test.sh contains:

echo $USER >> /tmp/out.log

/tmp/out.log then contains "root" not "someUser"

This is causing issues for me since I need all source and bash commands to run as someUser.

like image 674
Josh Nankin Avatar asked Nov 19 '13 23:11

Josh Nankin


1 Answers

Something I had to learn the hard way with Chef: It may actually be running as the user, but the environment variables aren't set.

To see this for yourself, run

echo `whoami`

If you need the environment variables set, just set them (or source a .bash_profile):

bash "source a file" do
  user "someUser"
  cwd "/tmp"
  environment ({'HOME' => '/home/someUser', 'USER' => 'someUser'})
  code <<-EOH
source /tmp/test.sh
  EOH
end

As a side note, have you created the user?

like image 56
Kyle Kelley Avatar answered Sep 22 '22 07:09

Kyle Kelley