Vagrant provisioning command does not add aliases to ~/.profile - bash

I have a fairly vanilla Vagrant setup running trusty64. It's being configured by a single shell file. Among others, it contains an aliasing of python3 to python and pip3 to pip, respectively:
echo "Writing aliases to profile:"
echo "alias python=\"python3\"" >> ~/.profile
echo "alias pip=pip3" >> ~/.profile
. ~/.profile
For some mysterious reason, these lines never make it into ~/.profile. There is no error message, nor any other commotion, it's just that nothing happens. This being 2am, I am fairly sure I'm doing something wrong, I just can't figure out what it is.

I am pretty sure you're calling the provisioner with something like
config.vm.provision "shell", path: "bootstrap.sh"
This works well but its executed as root user so all the lines are added for this user only. You want to use the privileged option
privileged (boolean) - Specifies whether to execute the shell script
as a privileged user or not (sudo). By default this is "true".
config.vm.provision "shell", path: "bootstrap.sh", privileged: "false"
will execute as your vagrant user and will add lines in to /home/vagrant/.profile file

Related

Save environmental variables in vagrant

I have CentOS7 set up via vagrant and when I export $MYVAR=test I can successfully echo $MYVAR. The problem arises when I close the vagrant session and have to ssh back in, this variable gets wiped and I have to reset it every time.
Is there a workaround for this? Possibly in the .vagrantfile?
The easiest is to add your export line in your ~/.profile file so it will always be available when you start a new session
You can try adding following to your provisioning script.
echo "export MYVAR=test" >> ~/.profile
On Vagrantfile, It will look like:
config.vm.provision "shell", inline: <<-SHELL
echo "export MYVAR=test" >> ~/.profile
SHELL

Vagrant - Provisioning script not changing directory

New to vagrant, please help!
Vagrantfile
Vagrant.configure("2") do |config|
config.vm.box = "laravel/homestead"
config.vm.provision "shell", path: "vm-setup/provision.sh"
end
vm-setup/provision.sh
# Update apt-get
apt-get -y update
# Install tree
apt-get install tree
# Create .bash_aliases
sudo echo 'alias cls="clear"' >> ~/.bash_aliases
sudo chsh -s $(which zsh) vagrant
cd /vagrant
provision.sh file runs fine. When I run "vagrant provision" it updates apt-get, installs tree and even changes the shell to ZSH.
But sudo echo 'alias cls="clear"' >> ~/.bash_aliases and cd /vagrant lines do not work, not sure why. When I vagrant ssh into the machine, I am being taken to root directory (/home/vagrant). I would like to start in /vagrant folder.
Vagrant's shell provisioner by default runs with privileged = true:
privileged (boolean) - Specifies whether to execute the shell script
as a privileged user or not (sudo). By default this is "true".
When you perform vagrant ssh you login to a VM as vagrant user.
That's why:
1.
# Create .bash_aliases
sudo echo 'alias cls="clear"' >> ~/.bash_aliases
It writes to root's ~/.bash_aliases and it is really there:
root#vagrant:~# id
uid=0(root) gid=0(root) groups=0(root)
root#vagrant:~# cat .bash_aliases
alias cls="clear"
Solution: write to vagrant's home folder:
# Create .bash_aliases
echo 'alias cls="clear"' >> /home/vagrant/.bash_aliases
chown vagrant:vagrant /home/vagrant/.bash_aliases
2.
cd /vagrant
This means that folder was changed in provision script, nothing else.
Solution: add this statement to vagrant's .bash_aliases as well:
echo 'cd vagrant' >> /home/vagrant/.bash_aliases
Your final vm-setup/provision.sh is:
# Update apt-get
apt-get -y update
# Install tree
apt-get install tree
# Create .bash_aliases
echo 'alias cls="clear"' >> /home/vagrant/.bash_aliases
echo 'cd /vagrant' >> /home/vagrant/.bash_aliases
chown vagrant:vagrant /home/vagrant/.bash_aliases
chsh -s $(which zsh) vagrant
Even not being the case, just for the sake of completion:
I had struggled many times while trying to use Vagrant as testing tool for setup scripts and just now I realized the underlying reason:
Using this Vagrantfile statement:
config.vm.provision "shell", path: "myScript.sh"
myScript.sh is inlined to the virtual machine standard input. This is good in the sense you don't need access to the actual script from inside the virtual machine (typically through /vagrant path).
...but it comes with the drawback that any relative path won't work properly.
Of course: We can adjust it to absolute path based on /vagrant. But this requires to modify the script we are trying to test.
So in this case (and in my opinion in any case we are not going to disable /vagrant share), it is a better solution to use "inline:" option with the "machine-internal" path:
config.vm.provision "shell", inline: "/vagrant/myScript.sh"
...This will inline this statement instead of the contents of the file and relative paths (or even "script path based" ones such as $(dirname "${0}")/relative/path) are going to work properly.
Additionally, if the setup script you are going to test is intended to be executed by non privileged users (for example if it is going to set up some user configuration we will expect to work just after a vagrant ssh -with vagrant user-) it is also a good idea to add the privileged: false option, pointed out by #Nickolay:
config.vm.provision "shell", inline: "/vagrant/myScript.sh", privileged: false

Is it possible to restart a machine when provisioning a machine using Vagrant and pickup where the script left off?

I was reading a tutorial in bash where they said to restart the machine, there was no option to restart a service directly, it was a matter of restarting the machine, and then there were more commands after that that still needed to be run when provisioning.
So is there any way to restart a box amid provisioning and then pick up where you left off after that?
As far as I know you can't have a single script/set of commands that would carry on where it left off if it attempts to restart the OS, such as:
config.vm.provision "shell", inline: <<-SHELL
echo $(date) > ~/rebootexample
reboot
echo $(date) >> ~/rebootexample
SHELL
In this example the second echo call would not be carried out.
You could split the script/commands up and use a plugin such as vagrant reload.
An example snippet of a Vagrantfile to highlight its possible use:
# execute code before reload
config.vm.provision "shell", inline: <<-SHELL
echo $(date) > ~/rebootexample
SHELL
# trigger reload
config.vm.provision :reload
# execute code after reload
config.vm.provision "shell", inline: <<-SHELL
echo $(date) >> ~/rebootexample
SHELL
I've never done this, but if I had to I would split the script into two pieces, one before restart that includes the restart command, then another that's post install.
The first one would also create a lock file.
The overall script would run the first script if the lock file didn't exist or run the second one if the file exists. This overall script would be set up for startup.
One trick you can employ is to send restart signal and save rest of the provisioning work as a script to be run on boot:
config.vm.provision "shell", inline: <<-SHELL
echo "Do your thing... DONE"
cat <<-RCLOCAL | sed -s 's_^ __' > /etc/rc.local
#!/bin/bash
echo "This will be run once on next boot and then it's destroyed and never run again"
rm /etc/rc.local
RCLOCAL
chmod o+x /etc/rc.local
shutdown -r now #restart
SHELL
This was tested to work on debian 9, so you may need to enable services or find another way to get your code bootsrapped to run on the next boot if you're running something else.
Unfortunately you can't simply do:
config.vm.provision "shell", inline: "shutdown -r now"
config.vm.provision "shell", inline: "echo 'hello world'"
results in ==>
The SSH connection was unexpectedly closed by the remote end. This
usually indicates that SSH within the guest machine was unable to
properly start up. Please boot the VM in GUI mode to check whether
it is booting properly.
Vagrant has a reboot option for provisioning, however, the reboot guest capabilities is currently not support for Linux.
You can check my plugin out here, https://github.com/secret104278/vagrant_reboot_linux/tree/master , I've implement the function for Linux to reboot.
This can be done like so:
config.vm.provision 'shell', path: 'part1.sh'
config.vm.provision 'shell', reboot: true
config.vm.provision 'shell', path: 'part2.sh'
https://developer.hashicorp.com/vagrant/docs/provisioning/shell#reboot

why doesn't vagrant provisioner modify ~/.bashrc?

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
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.
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.

Automatically chdir to vagrant directory upon "vagrant ssh"

So, I've got a bunch of vagrant VMs running some flavor of Linux (centos, ubuntu, whatever). I would like to automatically ensure that a "vagrant ssh" will also "cd /vagrant" so that no-one has to remember to do that whenever they log in.
I've figured out (duh!) that echo "\n\ncd /vagrant" >> /home/vagrant/.bashrc will do the trick. What I don't know is how to ensure that this only happens if the cd command isn't already there. I'm not a shell expert, so I'm completely confused here. :)
You can do this by using the config.ssh.extra_args setting in your Vagrantfile:
config.ssh.extra_args = ["-t", "cd /vagrant; bash --login"]
Then anytime you run vagrant ssh you will be in the /vagrant directory.
I put
echo "cd /vagrant_projects/my-project" >> /home/vagrant/.bashrc
in my provision.sh, and it works like a charm.
cd is a Bash shell built-in, as long as a shell is installed it should be there.
Also, be aware that ~/.bash_profile is for interactive login shell, if you add cd /vagrant in ~vagrant/.bashrc, it may NOT work.
Because distros like Ubuntu does NOT have this file -> ~/.bash_profile by default and instead use ~/.bashrc and ~/.profile
If someone creates a ~/.bash_profile for vagrant user on Ubuntu, ~vagrant/.bashrc will not be read.
You need to add cd /vagrant to your .bashrc in the vm. The best way to do this is in your provisioner script.
If you don't have a provisioner script, make one by adding this line to your Vagrantfile before end:
config.vm.provision "shell", path: "scripts/vagrant/provisioner.sh", privileged: false
Path is relative to the project root where the Vagrantfile is, and privileged depends on your project and what else is in your provisioner script which might need to be privileged. I use priveleged false and sudo explicitly when necessary.
And in the provisioner script:
if ! grep -q "cd /vagrant" ~/.bashrc ; then
echo "cd /vagrant" >> ~/.bashrc
fi
This will add cd /vagrant to .bashrc, but only if it isn't there already. This is useful if you reprovision, as it will prevent your .bashrc from getting cluttered.
Some answers mention a conflict with .bash_profile. If the above code doesn't work, you can try the same line with .bash_profile or .profile instead of .bashrc. However, I've been using vagrant with ubuntu guests. My Laravel/homestead box based on Ubuntu has a .bash_profile and a .profile but having cd /vagrant in .bashrc did work for me when using vagrant ssh without changing or deleting the other files.
You can add cd /vagrant to your .bashrc and it will run the command when you ssh. The /bashrc you want is in /home/vagrant (the user you login as when you vagrant ssh.) You can just stick the new line at the bottom of the file.
You can also do it this way:
vagrant ssh -c "cd /vagrant && bash"
And you could include it in a script to launch it (like ./vagrant-ssh).
May be this can help. Edit the Vagrantfile as replace your username with vagrant
`
config.vm.provision "shell" do |s|
s.inline = <<-SHELL
# Change directory automatically on ssh login
if ! grep -qF "cd /home/vagrant/ansible" /home/vagrant/.bashrc ;
then echo "cd /home/vagrant/ansible" >> /home/vagrant/.bashrc ; fi
chown vagrant. /home/vagrant/.bashrc
`
Ideally we just want to alter the vagrant ssh behaviour.
In my case, I wanted something that didn't affect any other processes in the environment, so we can do something like this in the vagrant file-
VAGRANT_COMMAND = ARGV[0]
if VAGRANT_COMMAND == "ssh"
config.ssh.extra_args = ["-t", "cd /vagrant; bash --login"]
end
You can use Ansible to assert that your .bashrc file contains cd /vagrant.
If you are not already using the Ansible provisioner for your VM, add the following lines to your Vagrantfile:
config.vm.provision "ansible_local" do |ansible|
ansible.playbook = "provisioning/playbook.yml"
end
And in your playbook, add the following task/play:
---
- hosts: all
gather_facts: no
tasks:
- name: chdir to vagrant directory
ansible.builtin.lineinfile:
path: /home/vagrant/.bashrc
line: cd /vagrant
According to this Q&A, I would recommend to modify .bashrc instead of .profile or .bash_profile.

Resources