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


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

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.

It is awesome, but not for me.

File.txt