Alex Rivera | Logout

chef create file in user's home directory

Asked 2012-10-12T14:37:36.170
12

I'm using chef to create a file resource

  file "somefile" do
     action :create_if_missing
  end

And I want to put this in the user's home directory. I am having two issues:

  1. The file is interpreted relative to /, so using ~/ ends up putting the file in /~/
  2. I'm launching this chef recipe through vagrant and these files are being created by root. So even if I could get ~/ to work, it would end up in root's home. I don't want to hard code to use the username vagrant since it won't always be that (we may run these with chef client as well), and node[:user] appears to be empty.

Is there a way to create a file in the non-root user (in this case vagrant) home directory?

Edit
Report

1 Answer

0

As I understand Chef conception - each recipe should be self contained. That means you should be able to add just one recipe (any of them) into run_list and it should be successfully installed on an empty machine.

What seems to me in your case, you assume that your non-root user already exists on machine, which is in contradiction to my previous statement. Your recipe should include another recipe, that configures your non-root user. Provided you use a data_bag for storing the user (and recipe 'user::my_user' should configure your user), your recipe should look something like that.

include_recipe 'user::my_user'
my_user = data_bag_item('user', 'my_user')

file ::File.join( my_user['home'], 'somefile' ) do
  action :create_if_missing
end
answered 2012-10-13T09:56:27.633

Your Answer