Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chef create file in user's home directory

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?

like image 359
Jeff Storey Avatar asked Oct 12 '12 14:10

Jeff Storey


1 Answers

Thread is old, but I faced this problem a moment ago.

You can create attribute for user for example: default['mycookbook']['user']

Then in your recipe:

user = node['mycookbook']['user'] # user set in cookbook attrubute
# user = node['current_user'] # user running chef cookbook (on provisioned host)

home = node['etc']['passwd'][user]['dir'] # Chef DSL
# home = Dir.home(user) # It's Ruby

file "#{home}/somefile" do
  action :create_if_missing
end
like image 122
kopiczko Avatar answered Sep 22 '22 09:09

kopiczko