execute commands as user after Vagrant provisioning
I wanted to document a solution for situations where the shell provisioner must run commands as a non-root user in a login shell:
Put your provisioning commands into a shell script (e.g. 'bootstrap.sh'):
#! /bin/bash
rbenv install 2.0.0-p353
rbenv global 2.0.0-p353
gem update --system
yes | gem update
gem install rdoc
gem install rails pg
Then in your Vagrantfile:
Vagrant.configure(2) do |config|
$script = "/bin/bash --login /vagrant/bootstrap.sh"
config.vm.provision :shell, privileged: false, inline: $script
end
You should replace the /vagrant/bootstrap.sh
path with the correct path for your provisioning script inside the vagrant machine.
I've used this solution specifically to get rvm
commands to work while provisioning with Vagrant.
You should be able to do this using the Vagrant Shell provisioner, e.g.
Vagrant.configure("2") do |config|
$script = <<-SCRIPT
rbenv install 2.0.0-p353
rbenv global 2.0.0-p353
gem update --system
yes | gem update
gem install rdoc
gem install rails pg
SCRIPT
config.vm.provision "shell", inline: $script, privileged: false
end
The key is to specify privileged: false
so that it will use the default user and not root
.