I'm having problems with a shell provisioning script used by Vagrant, as it's not executing source /home/vagrant/.bashrc
. I've reduced the problem down to this...
Within my VM I have a file at /home/vagrant/testfile
that contains this:
echo "In testfile"
And at the end of /home/vagrant/.bashrc
I have this:
echo "In .bashrc"
Both files are owned by the vagrant
user.
In one of my Vagrant provisioning shell scripts I have this:
echo "Hello"
source /home/vagrant/testfile
source /home/vagrant/.bashrc
echo "Goodbye"
Running vagrant provision
gives this:
Hello
In testfile
Goodbye
When I do vagrant ssh
then /home/vagrant/.bashrc
is run as usual, and I automatically see:
In .bashrc
So why does doing source /home/vagrant/.bashrc
have no effect from within my provisioning script?
Shell provisioning is ideal for users new to Vagrant who want to get up and running quickly and provides a strong alternative for users who are not comfortable with a full configuration management system such as Chef or Puppet. For POSIX-like machines, the shell provisioner executes scripts with SSH.
As bash_profile runs before bashrc is picked up, if you don't have a source command in bash_profile, bashrc won't run. There are two ways to tackle this, either delete bash_profile or if you use bash_profile, just add the following line anywhere in the file Highly active question.
From an interactive shell session, the BASH_ENV variable does not have to be set in this way, and the script does not need to source ~/.bashrc nor ~/.profile since these have already been sourced.
Additionally, if you are running Vagrant on something like Cygwin or WSL where bash is available, then you should be able to use an extension like ".sh". To run a script already available on the guest you can use an inline script to invoke the remote script on the guest. You can parameterize your scripts as well like any normal shell script.
You need to remove the “exit if not running interactively” bit (e.g. [ -z "$PS1" ] && return
) from the top of your .bashrc
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With