Vagrant – Passing Host’s Current User to Shell Provisioner

vagrant

I want to pass the current user within my Vagrantfile, but I'm not sure how to do it.

I've tried this:

config.vm.provision :shell, inline: "echo $(whoami) > /etc/profile.d/me"

But it results in 'root' being put into the file, which I assume is the vagrant host's user. I want to get the username for the host.

Best Answer

You can try the following:

username = "#{ENV['USERNAME'] || `whoami`}"
config.vm.provision :shell, inline: "echo #{username} > /etc/profile.d/me"

This way it will work on Linux or Microsoft systems.

Related Topic