Josh Nankin
Josh Nankin

Reputation: 2568

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.

Upvotes: 6

Views: 2898

Answers (1)

Kyle Kelley
Kyle Kelley

Reputation: 14144

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?

Upvotes: 17

Related Questions