I want to use vagrant to setup developer machines. Since the machine will talk to the servers inhouse, I thought it a good idea that they are setup with the same usernames the developers have on their host machine. I'm having trouble figuring out how to handle this in the provisioning step.
My simple Vagrantfile looks like this:
VAGRANT_COMMAND = ARGV[0]
Vagrant.configure(2) do |config|
user = ENV['USER']
config.vm.box = "ubuntu/trusty64"
config.vm.provision :shell, :path => "bootstrap.sh", :args => user
config.ssh.username = user
config.ssh.password = "heimskringla"
config.vm.synced_folder "~/src/", "/home/" + user + "/src"
config.vm.provision "file", source: "~/.gitconfig", destination: "/home/" + user + "/.gitconfig"
config.vm.provision "file", source: "~/.ssh", destination: "/home/" + user + "/.ssh"
end
bootstrap.sh takes $USER from the host machine as an input. If the user does not exist in the Vagrant machine, it is created and added to /etc/sudoers.d.
If I start with a clean slate and run "Vagrant up" on this, it starts using $USER at once, and since it does not exist yet, the setup fails.
As a test I've tried doing this:
if VAGRANT_COMMAND != "up"
config.ssh.username = user
config.ssh.password = "changeme"
end
Then the provisioning in bootstrap.sh works. The user is created, and my packages are installed. When it gets to the file and synced folder provisioning, however, it fails because of permission issues.
Failed to upload a file to the guest VM via SCP due to a permissions error. This is normally because the SSH user doesn't have permission to write to the destination location. Alternately, the user running Vagrant on the host machine may not have permission to read the file.
I've tried doing "su $USER" in the bottom of bootstrap.sh, but that is apparently not the way it works.
Anyone know how I can fulfill my needs?
EDIT: possible solution
I decided not to work so hard to change Vagrant, and tried to use the vagrant user. Now I have the following Vagrantfile:
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.provision :shell, :path => "vagrant/bootstrap.sh"
config.vm.synced_folder "~/src/", "/home/vagrant/src"
config.vm.provision "file", source: "~/.gitconfig", destination: ".gitconfig"
config.vm.provision "file", source: "~/.ssh", destination: ".ssh-from-host-machine"
config.vm.provision "file", source: "vagrant/.bash_aliases", destination: ".bash_aliases"
config.vm.provision :shell, privileged: false, :path => "vagrant/bootstrap_late.sh"
end
bootstrap.sh installs required packages, and bootstrap_late.sh does necessary setup for the vagrant user. This includes adding the ssh configs that makes it use $USER when talking to the server.