6

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?

1
  • 1
    Is the script executed in the context of the current user? Could it be that ~ points elsewhere when the script runs? For debugging, add the following to your script and capture its stderr output as well: PS4='>$LINENO: '; set -xv Commented May 24, 2014 at 5:26

2 Answers 2

8

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?

Sign up to request clarification or add additional context in comments.

Comments

2

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

 su vagrant -l -c "echo Hello world"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.