Vagrant

‘reload’ provisioner

Install with vagrant plugin install vagrant-reload

Use with config.vm.provision :reload, privileged: true in Vagrantfile

prepare debian/ubuntu base box

Inspired by this page and this also.

You should run on fresh machine (under user vagrant) scripts provided below.

Long way:

#!/bin/bash

cat  << EOF
Do not forget that during install:
1. Users are: root/vagrant and vagrant/vagrant (both passwords are vagrant)
2. sudo package is installed, vagrant added to sudo group (usermod -aG sudo vagrant)
EOF


# install vbox additions
echo "vagrant ALL=(ALL) NOPASSWD: ALL" | sudo tee -a /etc/sudoers
sudo apt -y update
sudo apt -y install linux-headers-$(uname -r) bzip2 tar gcc make perl openssh-server
read -p "Insert virtual box additions CD and press any key"
sudo mkdir -p /media/dvd
sudo mount /dev/cdrom /media/dvd
sudo /media/dvd/VBoxLinuxAdditions.run
sudo umount /media/dvd
read -p "Remove virtual box additions CD and press any key"

# configure locale
sudo dpkg-reconfigure locales

# rewrite grub default configuration
sudo tee /etc/default/grub > /dev/null <<EOT
GRUB_DEFAULT=0
GRUB_TIMEOUT=0
GRUB_HIDDEN_TIMEOUT=0
GRUB_HIDDEN_TIMEOUT_QUIET=true
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT=""
GRUB_CMDLINE_LINUX=""
EOT
sudo update-grub


# allow key-based auth for ssh
sudo tee -a /etc/ssh/sshd_config > /dev/null <<EOT
PubkeyAuthentication yes
AuthorizedKeysFile      .ssh/authorized_keys 
EOT

# add vagrant standard authorized keys
ssh-keygen -q -t dsa -f ~/.ssh/id_rsa -N ''
wget --no-check-certificate https://raw.github.com/hashicorp/vagrant/master/keys/vagrant.pub -O /home/vagrant/.ssh/authorized_keys
chmod 0600 /home/vagrant/.ssh/authorized_keys

echo "DONE"

Shorter way:

wget --no-check-certificate https://static.mszuyev.xyz/vbox_init.sh
chmod a+x vbox_init.sh
./vbox_init.sh
Export base box as file to upload to vagrant cloud (provider - virtualbox)
$ vboxmanage list vms
"ubuntu-gui" {283b0e56-4b0d-481d-a21c-649f07a749af}
"ubuntu-focal" {204f5103-fcd5-46ac-81d3-f294c6c37cf8}
$ vagrant package --base 204f5103-fcd5-46ac-81d3-f294c6c37cf8 --output ubuntu-focal-gui.box
  ==> 204f5103-fcd5-46ac-81d3-f294c6c37cf8: Exporting VM...
  ...
Vagrantfiles

Example 1 (provision/before.sh and provision/main.sh) are two external provisioner scripts.

VAGRANTFILE_API_VERSION = "2"
HOSTDIR=ENV['HOME']
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
	config.vm.provision "shell", path: "provision/before.sh", privileged: true
	config.vm.provision :reload, privileged: true # run before `vagrant plugin install vagrant-reload`
	config.vm.provision "shell", path: "provision/main.sh", privileged: true, env: {"MINIO_PWD": ENV['MINIO_ANSIBLE_PWD']}
	config.vm.provision :reload, privileged: true # last apply all settings, groups, etc
    config.vm.box = "mszuyev2/focal20"
    config.vm.box_version = "0.2"
	
	config.vm.define "kgui", primary: true do |kgui|
		kgui.vm.hostname = "kgui"
		kgui.vm.network "private_network", ip: "192.168.33.10"
		kgui.vm.synced_folder HOSTDIR, "/home/host", owner: "vagrant", group: "vagrant", automount: true
		kgui.vm.provider :virtualbox do |vb|
			vb.gui = true
			vb.customize [
			  'modifyvm', :id,
			  '--natdnshostresolver1', 'on',
			  '--memory', '5120',
			  '--cpus', '3'
			]
		end
	end
end

Example 2 (aux/vagrant-provision.sh is an external provisioner script)

VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
	config.vm.provision "shell", path: "aux/vagrant-provision.sh", privileged: true, env: {"MINIO_ANSIBLE_PWD": ENV['MINIO_ANSIBLE_PWD']}
	config.vm.box = "mszuyev2/stretch"
    config.vm.box_version = "0.2"
    config.vm.provider :virtualbox do |vb|
        vb.gui = false
        vb.customize [
          'modifyvm', :id,
          '--natdnshostresolver1', 'on',
          '--memory', '1600',
          '--cpus', '2'
        ]
    end
	
	config.vm.define "vm1" do |vm1|
		vm1.vm.hostname = "vm1"
		vm1.vm.network "private_network", ip: "192.168.33.33"
	end

	config.vm.define "vm2" do |vm2|
		vm2.vm.hostname = "vm2"
		vm2.vm.network "private_network", ip: "192.168.33.34"
	end

    config.vm.define "vm3" do |vm3|
        vm3.vm.hostname = "vm3"
        vm3.vm.network "private_network", ip: "192.168.33.35"
    end

    config.vm.define "vm4" do |vm4|
        vm4.vm.hostname = "vm4"
        vm4.vm.network "private_network", ip: "192.168.33.36"
        # special params for this machine
        vm4.vm.provider :virtualbox do |vb|
            vb.customize [
              'modifyvm', :id,
              '--memory', '3000',
            ]
        end
    end

end