chef-infrarecipe

Dynamically add current user name in Chef file resource


I have the following chef recipe to write some data to a file in the current user's home directory. As you can see, vagrant user is hardcoded in my case. What do I have to do so that the code gets the user dynamically based on who is currently logged in?

file '/home/vagrant/.ssh/id_rsa' do
  content "This is the super secret"
  mode '0755'
  owner 'vagrant'
  group 'vagrant'
end

I tried doing something like the following but it does not work:

file '/home/#{node['user']}/.ssh/id_rsa'


Solution

  • In almost all cases chef-client or chef-solo is being run as root, either as a system service or via sudo. If this is for setting up a test environment in Test Kitchen, just hardcode. If this is for workstation set up then then you can find the username in ENV['USER'].