Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

execute commands as user after Vagrant provisioning

Tags:

shell

vagrant

There are some commands that have to be run as a normal user after the initial provisioning. I thought I could do this using a separate shell script and the command su --login -c <command> vagrant, but it's not getting the user's path or other environment settings from .bashrc.

e.g.:

#!/usr/bin/env bash su --login -c "rbenv install 2.0.0-p353" vagrant su --login -c "rbenv global 2.0.0-p353" vagrant su --login -c "gem update --system" vagrant su --login -c "yes | gem update" vagrant su --login -c "gem install rdoc" vagrant su --login -c "gem install rails pg" vagrant 

Is there a way to do this? Maybe it has to be done with another provisioning tool like Puppet or Chef? I've thought of creating another shell script that sources the .bashrc, copying it to the box using a :file provisioner and executing the commands like that, but it seems sort of like a hack.

What's the right way to do this?

like image 358
Vince Avatar asked Mar 20 '14 23:03

Vince


People also ask

What is the provision that used in vagrant by default?

By default, provisioners are only run once, during the first vagrant up since the last vagrant destroy , unless the --provision flag is set, as noted above.

What is vagrant Linux sh?

The Vagrant Shell provisioner allows you to upload and execute a script within the guest machine.

How do I reload my vagrant?

Command: vagrant reload [name|id] After making any modifications to the Vagrantfile, a reload should be called. The configured provisioners will not run again, by default. You can force the provisioners to re-run by specifying the --provision flag.


Video Answer


2 Answers

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.

like image 88
jabclab Avatar answered Sep 19 '22 01:09

jabclab


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.

like image 45
evanhsu Avatar answered Sep 22 '22 01:09

evanhsu