How to provision Vagrantboxes with Ansible

Vagrant is an open-source software product for building and maintaining portable virtual software development environments for VirtualBox, KVM, Hyper-V, Docker containers, VMware, and AWS.

It’s a lightweight toolset that allows to define and describe the layout and structure of a virtual machine in a single file, the Vagrantfile. We can describe what base-box we want to use for our virtual machine (a base-box is a minimal virtual machine with a minimal set of tools on which we use as a base on top of which we will build our own environment), the number of CPUs we want to assign to the VM, the amount of RAM, the network configuration, etc. Once configured we can start our VM by simply executing the command vagrant up. This will configure the virtual machine using the configured virtual machine manager like virtualbox.

# Ansible

Ansible is a radically simple IT automation engine that automates cloud provisioning, configuration management, application deployment, intra-service orchestration, and many other IT needs. It uses no agents and no additional custom security infrastructure, so it’s easy to deploy – and most importantly, it uses a very simple language (YAML, in the form of Ansible Playbooks) that allow you to describe your automation jobs in a way that approaches plain English.

# Privisioning

Provisioning is the concept of preparing and equipping a machine to allow it to provide (new) services to its users. 2 Vagrant provides a number of different provisioning modules. For example, it provides a file provisioning module, which allows to upload a file or directory from the host machine to guest machine (VM), and a shell provisioning module, which allows to upload and execute a shells script on the guest machine.

Another provisioning module it provides is the Ansible Provisioner. This provisioner allows us to provision our guest machine (VM), using an Ansible Playbook, effectively combining the power of the 2 tools.

In this guide we are going to set up (provision) a vagrant box with ansible.

# Requirements

The following softwares needs to be installed:

  • Virtualbox as the virtualization platform
  • Vagrant as the tool to configure our vm
  • Ansible as the tool to provision the vm

# Setting up the vm and provisioning

Create a directory to use for this process. Inside it, add an empty file named Vagrantfile. This file defines the layout, structure and configuration of our VM. This is an example of a Vagrantfile:

Vagrant.configure("2") do |config|

    config.vm.define "debian-buster" do |s| = "generic/debian10"
        s.vm.memory = "512"
        s.vm.hostname = "debian-buster" 'private_network', ip: ""

        s.vm.provision :ansible do |vm|
            vm.verbose = true
            vm.playbook = "provision/main.yaml"

The above config will provision a debian 10 vm with 512mb of run with hostname debian-buster and network ip ""

  • = "generic/debian10": This is the os to run
  • s.vm.memory = "512": this is the ram to allocate
  • s.vm.hostname = "debian-buster": this is the hostname of the VM
  • s.vm.provision :ansible do |vm|: This block provisions the vm using the playbook in this path provision/main.yaml

In a scenario where you want to run multiple playbooks, please add another s.vm.provision :ansible do |vm| section.

The playbook provision/main.yaml content:

- hosts: all
  gather_facts: false
  become: true
    - name: Update all packages to their latest version
        name: "*"
        state: latest

    - name: Install some packages
          - vim
          - telnet
          - htop
        state: latest

The above playbook updates packages in debian server and installs the listed packages

comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy