Vagrant deploys VMs (and containers) and vagrant-libvirt is a provider for Vagrant which allows Vagrant to create libvirt VMs.
vagrant-libvirt is a pain to install.
I tried in RockyLinux and gave up.
Seems like if your distribution is not supported is better to not try.
Conventions and Terminology
- Box: package format for Vagrant environments. Find boxes
- Provider: VirtualBox, Hyper-V, Docker or vagrant-libvirt the services that Vagrant uses to set up and create virtual environments.
- Provisioners: Puppet, Chef or Ansible tools that allow users to customize the configuration of virtual environments.
- Share: Remote access to the environment over HTTP, SSH or vagrant connect.
Install Vagrant in Debian
# curl -fsSL https://apt.releases.hashicorp.com/gpg | apt-key add -
# apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
# apt-get update
# apt-get install -y vagrant vagrant-libvirt
$ vagrant plugin install vagrant-libvirt
# systemctl enable --now libvirtd
# usermod -a -G libvirt $user
The plugin instalation has to be run as user.
Useful commands
vagrant upStarts the VM. The first time also creates and provisions the VM.vagrant up --provisionStart and provision the VM when it is stopped.vagrant haltStop the VM.vagrant destroyDelete the VM.vagrant reloadLikehaltfollowed byupvagrant reload --provisionReloads the VM and provisions again.vagrant ssh-configGenerate the necessary configuration for ssh to connect. Append to~/.ssh/configvagrant sshConnect over ssh to the VM.vagrant provisionRun provisioning in the VM.
To save the ssh config vagrant ssh-config >> ~/.ssh/config
When you update the playbook run vagrant provision or vagrant up --provision if the VM is stopped.
When you update the Vagrantfile run vagrant reload.
Some options like the hostname is required to recreate the VM with vagrant destroy and vagrant up.
Create a Vagrant file
Basic Vagrant file.
Vagrant.configure("2") do |config|
config.vm.box = "rockylinux/8"
config.vm.provider "libvirt" do |v|
v.title = "RockyLinux"
end
end
Custom settings for the VM. More info
Vagrant.configure("2") do |config|
config.vm.box = "rockylinux/8"
config.vm.provider "libvirt" do |v|
v.title = "RockyLinux"
v.memory = 1024
v.cpus = 2
v.nic_model_type = "virtio"
v.machine_virtual_size = 10
end
end
Add Ansible to the mix
Install Ansible
# echo "deb http://ppa.launchpad.net/ansible/ansible/ubuntu focal main" | tee -a /etc/apt/sources.list.d/ansible.list
# apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 93C4A3FD7BB9C367
# apt update
# apt install -y ansible
More advanced Vagrant file with an Ansible playbook.
Vagrant.configure("2") do |config|
config.vm.define :testvm do |testvm|
testvm.vm.box = "rockylinux/8"
testvm.vm.hostname = "rockylinux"
testvm.vm.provider "libvirt" do |v|
v.title = "RockyLinux"
v.memory = 1024
v.cpus = 4
v.nic_model_type = "virtio"
v.machine_virtual_size = 10
v.storage_pool_name = "vms"
end
testvm.vm.provision :ansible do |ansible|
ansible.playbook = "ansible_playbook.yml"
end
testvm.vm.synced_folder ".", "/mnt",
type: "nfs",
linux__nfs_options: ['rw','no_subtree_check','all_squash','async']
end
end
Conclusion
Unless you need to deploy many VMs, it is not so useful. How many times do you deploy a VM? A container maybe, but a VM only once. Maybe a few times if you mess up.
- Default credentials
root/vagrant vagrant-libvirtis not officially supported and it shows. If it works it is awesome.vagrant-libvirtcan control a local or a remote instance of libvirt.- Creating images is easy, but you need to configure them internally like password-less sudo, ssh key, possible dependencies (like rsync), etc. There are online guides on how to create images.
- You have to allow DHCP and forwarding in the firewall.
- Provisioning only happens when you create the VM by default.
It is awesome, but not for me.