Run commands as a user after providing Vagrant

There are several commands that should be executed as a normal user after the initial initialization. I thought I could do this using a separate script shell and the su --login -c <command> vagrant command su --login -c <command> vagrant , but not getting the user path or other environment parameters from .bashrc.

eg:.

 #!/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 any way to do this? Maybe you need to do this with another resource provisioning tool like Puppet or Chef? I thought about creating another shell script that sends .bashrc , copying it to a field, using the file security tool: and executing such commands, but it seems like a hack.

What is the right way to do this?

+68
shell vagrant
Mar 20 '14 at 23:53
source share
3 answers

You must do this using Vagrant Shell Provender , for example

 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 must specify privileged: false so that it uses the default user, not root .

+138
Mar 21 '14 at 11:03
source share

I wanted to document a solution for situations where the shell provider should run commands as a non-root user in the login shell:

Put your provisioning commands in a shell script (for example, "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 path /vagrant/bootstrap.sh with the correct path for your initialization script on a stray computer.

I used this solution specifically to make rvm work when initializing with Vagrant.

+3
Jan 23 '18 at 22:20
source share

I tried to answer that @jabclab and @evanhsu do not work with me

To install rbenv and use it in the Vagrant Provisioning process.

Using the following commands before using rbenv commands

 export PATH="$HOME/.rbenv/bin:$PATH" export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH" eval "$(rbenv init -)" 

Bootstrap.sh File

 #!/usr/bin/env bash sudo apt-get update echo "========================= install dependencies for install rbenv ===========================" sudo apt-get install -y autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm3 libgdbm-dev echo "========================= install rbenv ==========================================" git clone https://github.com/rbenv/rbenv.git ~/.rbenv echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc echo 'eval "$(rbenv init -)"' >> ~/.bashrc echo "========================= install ruby build plugin for rbenv =======================" git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc echo "========================= install ruby v2.5.0 ==========================================" export PATH="$HOME/.rbenv/bin:$PATH" export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH" eval "$(rbenv init -)" rbenv install 2.5.0 rbenv global 2.5.0 ruby -v gem -v echo "========================= install bundler dependencies manager for ruby =====================" gem install bundler rbenv rehash 

Then a vagrant provision string will be included in the VagrantFile

 deploy_config.vm.provision :shell, privileged: false, path: "bootstrap.sh" 

Source of my answer from Gits from @creisor

Another way to use rbenv commands in the process of providing vagrant on answers to this question

0
Jul 28 '19 at 4:08
source share



All Articles