Update .bashrc from provisioning shell script with Vagrant
You need to give the full path to the file.
E.g.
echo "source /usr/local/share/chruby/chruby.sh" >> /home/vagrant/.bashrc
Updating .bashrc and environment variables during Vagrant provisioning
The Vagrant script provisioner will run as root, so it's home dir (~) will be /root. In your script if you define BASHRC_PATH=/home/vagrant, then I believe your steps will work: appending to, then sourcing from /home/vagrant/.bashrc.
Update:
Scratching my earlier idea ^^ because BASHRC_PATH is already set correctly.
As an alternative we could use .profile or .bash_profile. Here's a simplified example which sets environment variable FOO, making it available during provisioning and after ssh login:
Vagrantfile
Vagrant.configure(2) do |config|
config.vm.box = "hashicorp/precise32"
$prov_script = <<SCRIPT
if ! grep -q "export FOO" /home/vagrant/.profile; then
sudo echo "export FOO=bar" >> /home/vagrant/.profile
echo "before source, FOO=$FOO"
source /home/vagrant/.profile
echo "after source, FOO=$FOO"
fi
SCRIPT
config.vm.provision "shell", inline: $prov_script
end
Results
$ vagrant up
...
==> default: Running provisioner: shell...
default: Running: inline script
==> default: before source, FOO=
==> default: after source, FOO=bar
$ vagrant ssh -c 'echo $FOO'
bar
$ vagrant ssh -c 'tail -n 1 ~/.profile'
export FOO=bar
why doesn't vagrant provisioner modify ~/.bashrc?
I think what's happening is the provisioning script is run as root (or sudo), so the "~" home location is actually /root rather than the default user home location /home/vagrant.
I can think of a couple ways to solve this:
First (and probably easiest) is to be explicit about the .bashrc path, like:
# Vagrantfile
Vagrant.configure("2") do |config|
... various cmds to set box and network...
$install_user_vars = <<SCRIPT
sudo cat /vagrant/dev_env_config >> /home/vagrant/.bashrc
echo "*** here is the .bashrc file:"
cat /home/vagrant/.bashrc
SCRIPT
config.vm.provision "shell", inline: $install_user_vars
end
The second option could be to run this part of the provisioning script as a non-privileged user. See the 'privileged' option on the Shell Scripts docs page.
Updating path in Vagrant using shell provisioning
The problem was resolved with the following added to the provision.sh
file based on this post:
echo PATH $PATH
[ -f ~/.profile ] || touch ~/.profile
[ -f ~/.bash_profile ] || touch ~/.bash_profile
grep 'PATH=/usr/local/x86_64/bin' ~/.profile || echo 'export PATH=/usr/local/x86_64/bin:$PATH' | tee -a ~/.profile
grep 'PATH=/usr/local/x86_64/bin' ~/.bash_profile || echo 'export PATH=/usr/local/x86_64/bin:$PATH' | tee -a ~/.bash_profile
. ~/.profile
. ~/.bash_profile
echo PATH $PATH
This works for the precise 64 box all the commands should be one line.
Why is `source /home/vagrant/.bashrc` not working in a Vagrant shell provisioning script?
You need to remove the “exit if not running interactively” bit (e.g. [ -z "$PS1" ] && return
) from the top of your .bashrc
.
execute commands as user after Vagrant provisioning
You should be able to do this using the Vagrant Shell provisioner, e.g.
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 is to specify privileged: false
so that it will use the default user and not root
.
Setting up Shell Script for Vagrant Setup
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?
Related Topics
How to Set Errno in Linux Device Driver
Difference Between Archiving and Compression
Compile Errors Using Bfd.H on Linux
How to Get CPU Serial Under Linux Without Root Permissions
Linux - Change The Hostname in The Cli
How to Read N-Th Line from a Text File in Bash
Setting The Umask of The Jenkins Process
Sshfs, Linux - How to Mount with Read-Only Access
Kill Bash Script Foreground Children When a Signal Comes
Generate Disk Usage Graphs/Charts with Cli Only Tools in Linux
What Are Coding Conventions for Using Floating-Point in Linux Device Drivers
Using Pthread Mutex Shared Between Processes Correctly
Linux Directory Starting with Dot
Prevent R from Using Virtual Memory on Unix/Linux
Why Does The -R Option (Relocatable) Make Ld Not Find Any Libraries
How to Get a Hex Color Code from a Solid-Color Image for a Script