How to run a shell script with Vagrant Digital Ocean provider?

September 22, 2015 3.4k views
Deployment Miscellaneous Ubuntu

I've been running an application on Vagrant using VirtualBox as my provider and a shell script to provision the environment once the VM is running.

My shell script uses a number of commands starting with "su -vagrant -c" such as:

su - vagrant -c "rvm mount -r $1"

These all work perfectly when running the VirtualBox provider in my local environment.

However, now that I'm running this using the Vagrant Digital Ocean provider, I'm getting the following error:

==> default: No passwd entry for user 'vagrant'

Here is my Vagrantfile:

Vagrant.configure('2') do |config|

  config.vm.provider :digital_ocean do |provider, override|
    override.ssh.private_key_path = '~/.ssh/id_rsa'
    override.vm.box = 'digital_ocean'
    override.vm.box_url = "https://github.com/smdahlen/vagrant-digitalocean/raw/master/box/digital_ocean.box"
    override.vm.hostname = 'digitalocean-vagrant'

    provider.ssh_key_name = 'the name associated with my ssh_key on my existing account'
    provider.token = 'Here I have my token from the Digital Ocean API'
    provider.image = 'ubuntu-14-04-x64'
    provider.region = 'nyc2'
    provider.size = '512mb'

  config.vm.provision "shell" do |s|
      s.path = "setup.sh"
      # argument mapping ["ruby binary url", "Ruby Version", "RVM Gemset name", "MySQL or PostgreSQL", "Rails version", "ImagineMagick required"]
      s.args = ["https://rvm.io/binaries/ubuntu/14.04/x86_64/ruby-2.1.2.tar.bz2", "ruby-2.1.2", "Customer Name", "MySQL", "4.1.4", "true"]

Here is my setup.sh file:


echo "****** Provisioning virtual machine... ******"
echo "****** Installing Ruby Dependencies ******"
cd /vagrant
sudo apt-get update
sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev -y
echo "****** Installing RVM ******"
sudo apt-get install libgdbm-dev libncurses5-dev automake libtool bison libffi-dev -y
curl -L https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
echo "****** Installing Ruby $1 ******"
#su - vagrant -c "rvm install $1" - this works but must compile ruby
su - vagrant -c "rvm mount -r $1" #use this if there is a binary available to avoid need to compile ruby
source /home/vagrant/.rvm/scripts/rvm
su - vagrant -c "rvm --default use $2"
su - vagrant -c "rvm gemset list"
su - vagrant -c "rvm gemset create $3"
su - vagrant -c "rvm use $2@$3 --default"
su - vagrant -c "rvm gemset list"
su - vagrant -c "ruby -v"
echo "****** Installing $4 ******"
if [ "$4" == "MySQL" ]; then
  sudo apt-get install mysql-server mysql-client libmysqlclient-dev -y
    cd /etc
    sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main" >> /etc/apt/sources.list.d/postgresql.list'    
    wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
    sudo apt-get update
    sudo apt-get install postgresql-9.3 pgadmin3 -y
echo "****** Installing Nginx ******"
  sudo apt-get update
  sudo apt-get install nginx -y
echo "****** Installing Nodejs ******" 
  sudo apt-get update
  sudo apt-get install nodejs
  sudo apt-get install npm
echo "****** Installing Rails ******"
    su - vagrant -c "gem install rails --version=$5 --no-rdoc --no-ri"
  su - vagrant -c "rails -v"
if [ "$6" == "true" ]; then
    echo "****** Installing ImageMagick ******"
    sudo apt-get install imagemagick -y
    echo "****** ImageMagick Not Required ******"
echo "****** Completed provisioning virtual machine! ******"
# echo "****** Be sure to uncomment the passenger_root & passenger_ruby lines in /etc/nginx/nginx.conf file!!!  ******"
echo "****** Then do a 'sudo service nginx restart' ******"

I've tried several different options based on the issues I saw posted on the Digital Ocean Provider Github Repo

here: https://github.com/smdahlen/vagrant-digitalocean/issues/174

and here: https://github.com/smdahlen/vagrant-digitalocean/issues/183

Also another post that I am unable to find now suggested including provider.setup = false

But, none of these worked.

Any other ideas what I need to do to make this work?


1 Answer


It appears that the plugin doesn't create the vargrant user. I added a sudo useradd vagrant to my setup.sh script and it worked.

My setup.sh script wasn't as complex as yours, but it did run several commands as the vagrant user with su - vagrant -c "command-here" without any issue.

Have another answer? Share your knowledge.