Configuring a Script Shell to Configure Vagrant

Trying to write a shell script to configure my server environment on Ubuntu through Vagrant, and I ran into a problem when the script ends unexpectedly. I added the shell script path in the Vagrant provisioning configuration.

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 starting, I expect the repository to be cloned into the .rbenv folder and rbenv will be added to $ PATH in ~ / .bashrc along with the init rbenv function evaluated and placed in ~ / .bashrc. However, when the script is executed at the Vagrant submission stage, I end up with the script just cloning the git repository, and then exiting 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 exits and ~ / .bashrc remains unchanged. I was wondering how I can change my shell script so that it performs the required action (which adds rbenv to ~ / .bashrc). Any ideas?

+6
source share
2 answers

As mklement0 said, the script does not work as a vagrant user: it works as root .

If you want to run the script as a 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 preparation scripts.

If you want to work as another user, do not forget that su user will not work: How to use su to execute the rest of the bash script as this user?

+8
source

If you want to execute any script as a vagrant user, try this.

  su vagrant -l -c "echo Hello world" 
+2
source

Source: https://habr.com/ru/post/969790/


All Articles