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
1
---
2
- name: Setting OS version fact
3
set_fact:
4
osversion: "{{ ansible_distribution | lower }}{{ ansible_distribution_major_version }}"
5
6
- name: Check OS version
7
fail:
8
msg: "OS Version( {{ ansible_distribution }}{{ ansible_distribution_major_version }} ) is not certified for the role"
9
when:
10
- osversion != "ubuntu18"
11
- osversion != "centos7"
12
- osversion != "centos8"
13
14
- name: Check default vagrant provider variable
15
fail:
16
msg: "Default vagrant provider can be either libvirt or virtualbox"
17
when:
18
- default_provider != "libvirt"
19
- default_provider != "virtualbox"
20
21
- name: Check default provider for Centos 8
22
fail:
23
msg:
24
- "Centos 8 has some issues with vagrant pluging that cannot be automated"
25
- "Please refer to the original documentation for manual steps"
26
- "https://github.com/vagrant-libvirt/vagrant-libvirt"
27
when: (default_provider == "libvirt") and (osversion == "centos8")
28
Copied!

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
1
---
2
# vars file for ansible-role-vagrant
3
vagrant_version: "2.2.14"
4
vagrant_base_url: "https://releases.hashicorp.com/vagrant/{{ vagrant_version }}"
5
default_provider: "libvirt"
6
Copied!

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
1
---
2
# tasks file for ansible-role-vagrant
3
- name: Prereq check
4
include_tasks: prereq.yaml
5
6
- name: Install and Configure for CentOS
7
include_tasks: centos.yaml
8
when: ansible_distribution == "CentOS"
9
10
- name: Install and Configure for Ubuntu
11
include_tasks: ubuntu.yaml
12
when: ansible_distribution == "Ubuntu"
13
Copied!

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
1
---
2
- name: "Install vagrant binary on Centos 7"
3
yum:
4
name: "{{ vagrant_base_url }}/vagrant_{{ vagrant_version }}_x86_64.rpm"
5
state: present
6
when: ansible_distribution_major_version == "7"
7
8
# Fails if GPG check is enabled. Will find a better way.
9
- name: "Install vagrant binary on Centos 8"
10
dnf:
11
name: "{{ vagrant_base_url }}/vagrant_{{ vagrant_version }}_x86_64.rpm"
12
state: present
13
disable_gpg_check: true
14
when: ansible_distribution_major_version == "8"
15
16
- name: "Install libvirt vagrant plugin"
17
block:
18
- name: Install vagrant Plugin prereqs
19
yum:
20
name:
21
- libvirt
22
- libvirt-devel
23
- ruby-devel
24
- qemu-kvm
25
- libxslt-devel
26
- libxml2-devel
27
- libvirt-devel
28
- libguestfs-tools-c
29
- ruby-devel
30
- gcc
31
32
- name: Install vagrant libvirt plugin
33
command: "vagrant plugin install vagrant-libvirt"
34
become: false
35
when: default_provider == "libvirt"
36
Copied!

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
1
---
2
- name: "Install Vagrant"
3
apt:
4
deb: "{{ vagrant_base_url }}/vagrant_{{ vagrant_version }}_x86_64.deb"
5
6
- name: Install libvirt vagrant dependencies
7
block:
8
# I couldn't find a better way to enable source repositories
9
- name: Enable source libraries
10
replace:
11
path: /etc/apt/sources.list
12
regexp: '^#( deb-src http.*)'
13
replace: '\1'
14
15
- name: Install build dependencies
16
apt:
17
name:
18
- vagrant
19
- ruby-libvirt
20
state: build-dep
21
update_cache: true
22
23
- name: Install prereq packages
24
apt:
25
name:
26
- qemu
27
- libvirt-bin
28
- ebtables
29
- dnsmasq-base
30
- libxslt-dev
31
- libxml2-dev
32
- libvirt-dev
33
- zlib1g-dev
34
- ruby-dev
35
36
- name: Install vagrant libvirt plugin
37
command: "vagrant plugin install vagrant-libvirt"
38
become: false
39
when: default_provider == "libvirt"
40
Copied!

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
1
---
2
- name: Install KVM with Vagrant
3
hosts: myhypervisor
4
become: yes
5
roles:
6
- { role: yasarlaro.kvm, libvirt_user: "admin" }
7
- { role: yasarlaro.vagrant, default_provider: "libvirt" }
8
Copied!
or
Install VirtualBox with Vagrant
1
---
2
- name: Install VirtualBox with Vagrant
3
hosts: myhypervisor
4
become: yes
5
roles:
6
- { role: yasarlaro.virtualbox }
7
- { role: yasarlaro.vagrant, default_provider: "virtualbox" }
Copied!