Reputation: 19257
How can I make the Vagrantfile append the contents of a file to the ~/.bashrc file ?
In my Vagrantfile, I am trying to append the contents of a file /vagrant/dev_env_config to the ~/.bashrc file.
When I run vagrant up
it outputs the echo statement AND it outputs the expected contents of the ~/.bashrc file.... so I know it's reading the file dev_env_config and APPEARS to be appending it.
However, when I then run vagrant ssh
and then cat ~/.bashrc
the ~/.bashrc file is unmodified, it's the default ~/.bashrc file
In other words the mods to ~/.bashrc file are lost somewhere between when the vagrant provison runs and when I run vagrant ssh
# Vagrantfile
Vagrant.configure("2") do |config|
... various cmds to set box and network...
$install_user_vars = <<SCRIPT
sudo cat /vagrant/dev_env_config >> ~/.bashrc
echo "*** here is the .bashrc file:"
cat ~/.bashrc
SCRIPT
config.vm.provision "shell", inline: $install_user_vars
end
Upvotes: 3
Views: 1087
Reputation: 10721
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.
Upvotes: 6
Reputation: 3921
A primitive solution is to set the path to .bashrc
explicitly. As a rule default username of a SSH user (which will be used for vagrant ssh
action) is vagrant
so:
$install_user_vars = <<SCRIPT
sudo cat /vagrant/dev_env_config >> /home/vagrant/.bashrc
echo "*** here is the .bashrc file:"
cat /home/vagrant/.bashrc
SCRIPT
Also I'm not sure that it's a necessary to use a sudo
command (in sudo cat ...
). Probably you don't need it, but it depends on which user is used to run a provision script. I guess it's also vagrant
.
So if it's really a vagrant
you could leave the path to .bashrc
unmodified (~/.bashrc
), but have to remove sudo cat ...
command and use simple cat ...
instead. And it's a more clean solution in my opinion. Because actually we shouldn't use sudo
(root
) permissions without need.
Upvotes: 2