Onur Yasarlar
Search…
How to Install Vagrant on Centos 7/8 and Ubuntu 18

Getting Started

If you are running a home lab, it is inevitable that you will want to provision new VMs and would like to run your tests. There is always an option to create a golden image and then clone it if you need new VMs but why should you bother yourself to handle it all manually. Hashicorp already provides perfect automation for you, Vagrant. I do personally like it as you can keep all your infrastructure with Infrastructure as Code approach in your Vagrantfile. So let's see how we can install vagrant on top of our hypervisor.
There is no official VirtualBox package for Ubuntu 20 while I am writing this post so I am skipping it
There are also some manual steps required to install libvirt vagrant plugin on Centos 8. That's why I am also skipping it, but you can refer to the official documentation if you want to install the plugin on Centos 8

Prerequisites

The first prerequisite is the obvious one: hypervisor. We first need to have a running hypervisor and you can refer to my post to install either KVM or VirtualBox. I personally prefer KVM over VirtualBox.
I again collected all the prerequisites under tasks/prereq.yaml play:
tasks/prereq.yaml
---
- name: Setting OS version fact
set_fact:
osversion: "{{ ansible_distribution | lower }}{{ ansible_distribution_major_version }}"
- name: Check OS version
fail:
msg: "OS Version( {{ ansible_distribution }}{{ ansible_distribution_major_version }} ) is not certified for the role"
when:
- osversion != "ubuntu18"
- osversion != "centos7"
- osversion != "centos8"
- name: Check default vagrant provider variable
fail:
msg: "Default vagrant provider can be either libvirt or virtualbox"
when:
- default_provider != "libvirt"
- default_provider != "virtualbox"
- name: Check default provider for Centos 8
fail:
msg:
- "Centos 8 has some issues with vagrant pluging that cannot be automated"
- "Please refer to the original documentation for manual steps"
- "https://github.com/vagrant-libvirt/vagrant-libvirt"
when: (default_provider == "libvirt") and (osversion == "centos8")

Variables

As I prefer KVM, I made libvirt provider the default provider for the role. You can feel free to change the provider to virtualbox in vars/main.yml file.
vars/main.yml
---
# vars file for ansible-role-vagrant
vagrant_version: "2.2.14"
vagrant_base_url: "https://releases.hashicorp.com/vagrant/{{ vagrant_version }}"
default_provider: "libvirt"

Main Playbook

I will also follow my habit to divide plays into smaller pieces in my tasks/main.yml file. I prefer to include operating system based plays in my main play.
tasks/main.yml
---
# tasks file for ansible-role-vagrant
- name: Prereq check
include_tasks: prereq.yaml
- name: Install and Configure for CentOS
include_tasks: centos.yaml
when: ansible_distribution == "CentOS"
- name: Install and Configure for Ubuntu
include_tasks: ubuntu.yaml
when: ansible_distribution == "Ubuntu"

Vagrant Installation on Centos 7/8

If your vagrant provider will be VirtualBox, it is pretty straightforward. You will just need to install vagrant. If you will be installing vagrant for libvirt provider, then you will need to install some dependencies. Thanks to the open-source community, there is a module supported by 100s of contributors here. This module will allow you run vagrant commands under KVM without any hassle.
tasks/centos.yaml
---
- name: "Install vagrant binary on Centos 7"
yum:
name: "{{ vagrant_base_url }}/vagrant_{{ vagrant_version }}_x86_64.rpm"
state: present
when: ansible_distribution_major_version == "7"
# Fails if GPG check is enabled. Will find a better way.
- name: "Install vagrant binary on Centos 8"
dnf:
name: "{{ vagrant_base_url }}/vagrant_{{ vagrant_version }}_x86_64.rpm"
state: present
disable_gpg_check: true
when: ansible_distribution_major_version == "8"
- name: "Install libvirt vagrant plugin"
block:
- name: Install vagrant Plugin prereqs
yum:
name:
- libvirt
- libvirt-devel
- ruby-devel
- qemu-kvm
- libxslt-devel
- libxml2-devel
- libvirt-devel
- libguestfs-tools-c
- ruby-devel
- gcc
- name: Install vagrant libvirt plugin
command: "vagrant plugin install vagrant-libvirt"
become: false
when: default_provider == "libvirt"

Vagrant Installation on Ubuntu 18

If your vagrant provider will be VirtualBox, it is pretty straightforward. You will just need to install vagrant. If you will be installing vagrant for libvirt provider, then you will need to install some dependencies. Thanks to the open-source community, there is a module supported by 100s of contributors here. This module will allow you run vagrant commands under KVM without any hassle.
The module requires one of the source repositories to be enabled, but I couldn't find an easier way to only enable main source repository so I decided to enable all in /etc/apt/sources.list but feel free to comment if you have a better solution. I could have used replace module with backup option and then could have restored the file after the installation is finished but I will handle it in a better way later. My exposure with Ubuntu / Debian administration is fairly new so I am sure there is a proper way to do it.
tasks/ubuntu.yaml
---
- name: "Install Vagrant"
apt:
deb: "{{ vagrant_base_url }}/vagrant_{{ vagrant_version }}_x86_64.deb"
- name: Install libvirt vagrant dependencies
block:
# I couldn't find a better way to enable source repositories
- name: Enable source libraries
replace:
path: /etc/apt/sources.list
regexp: '^#( deb-src http.*)'
replace: '\1'
- name: Install build dependencies
apt:
name:
- vagrant
- ruby-libvirt
state: build-dep
update_cache: true
- name: Install prereq packages
apt:
name:
- qemu
- libvirt-bin
- ebtables
- dnsmasq-base
- libxslt-dev
- libxml2-dev
- libvirt-dev
- zlib1g-dev
- ruby-dev
- name: Install vagrant libvirt plugin
command: "vagrant plugin install vagrant-libvirt"
become: false
when: default_provider == "libvirt"

Putting things together

You can also use my Ansible roles to install vagrant along with the KVM or VirtualBox. You can download my roles with ansible-galaxyand then add it in a playbook as below:
Install KVM with Vagrant
---
- name: Install KVM with Vagrant
hosts: myhypervisor
become: yes
roles:
- { role: yasarlaro.kvm, libvirt_user: "admin" }
- { role: yasarlaro.vagrant, default_provider: "libvirt" }
or
Install VirtualBox with Vagrant
---
- name: Install VirtualBox with Vagrant
hosts: myhypervisor
become: yes
roles:
- { role: yasarlaro.virtualbox }
- { role: yasarlaro.vagrant, default_provider: "virtualbox" }