chef create file in user's home directory
12,614
Solution 1
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
Solution 2
This is because Chef recipes are run by the root
user. How about this workaround
if File.exists? "/vagrant"
user = "vagrant"
else
user = "deployer"
end
file "/home/#{user}/foo" do
..
end
Author by
Jeff Storey
Updated on July 01, 2022Comments
-
Jeff Storey about 1 year
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:
- The file is interpreted relative to /, so using ~/ ends up putting the file in /~/
- 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?