Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up Shell Script for Vagrant Setup

Tags:

shell

vagrant

Trying to write a shell script to setup my server environment on Ubuntu through Vagrant and am running into a problem where the script ends unexpectedly. I added the path to the shell script in Vagrant's provision config option.

Vagrant:

# Specify our provision script
config.vm.provision "shell", path: "scripts/bootstrap.sh"

My script:

#!/bin/bash

# Install dependencies for Ruby
sudo apt-get update
sudo apt-get install -y git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common

# Setting up rbenv
echo 'Setting up rbenv'
git clone git://github.com/sstephenson/rbenv.git .rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL

After running, I expect the repository to be cloned into the .rbenv folder and to have rbenv added to $PATH in ~/.bashrc along with the rbenv init function evaluated and put into ~/.bashrc. However, when the script is executed on Vagrant's provision step I end up with the script just cloning the git repository and then terminating without executing anything else in my script.

Output:

==> default: Processing triggers for libc-bin (2.19-0ubuntu6) ...
==> default: Setting up rbenv
==> default: Cloning into '.rbenv'...

And then the script terminates and ~/.bashrc is left unchanged. I was wondering how I can change my shell script so that it will perform the action I want (which is adding rbenv to ~/.bashrc). Any ideas?

like image 960
Josh Black Avatar asked May 24 '14 04:05

Josh Black


2 Answers

As mklement0 said, the script does not run as the vagrant user: it runs as root.

If you want to run the script as the vagrant user you need privileged: false.

config.vm.provision :shell, privileged: false, path: "scripts/bootstrap.sh"

As mklement0 said: use set -xv to debug your provisioning scripts.

If you want to run as another user, don't forget that su user won't work: How do I use su to execute the rest of the bash script as that user?


If you want execute some script like vagrant user, try this.

 su vagrant -l -c "echo Hello world"
like image 23
Adam Michna Avatar answered Sep 28 '22 00:09

Adam Michna