Deploying an Ansible role on a Vagrant machine

Vagrant is a flexible tool to build a development environment, following the infrastructure as code paradigm (IaC). Using the same Ansible roles in production and in Vagrant can save a lot of time, and can make Vagrant local deployment much closer to production configuration (production parity). In this blog post I will show how to use Vagrant to create a local development environment to deploy your Ansible roles.

Prerequisites

The process of installing the tools can be different for each type of operating system. I recommend using the official documentation for the installation process. The official documentation links:

In this blog post, we will use VirtualBox as a Vagrant provider. If you haven’t installed VirtualBox yet, here is the link to the official download webpage, but the code that I used here will probably work with most Vagrant providers.

After this point let’s assume that the tools are already installed.

File Content

ansible.cfg

[defaults]
callbacks_enabled = profile_tasks,roles_tasks,timer
[ssh_connection]
pipelining = True

I used some Ansible callback plugins to help us deploy additional information and improve performance. In the reference section can you see more details about it.

playbook.yml

---
- name: Deploy a Ansible Role using Vagrant
hosts: all
roles:
- chrony

The playbook.yml is the Ansible playbook that will be executed after the creation of the virtual environment, in our case an Ubuntu 20.04 LTS (focal). 

Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/focal64"
config.vm.provision :"ansible" do |ansible|
ansible.playbook = "playbook.yml"
end
end

Vagrantfile describes the type and configures the provision of the virtual machines. The Vagrantfile can be generated manually with the following command:

vagrant init ubuntu/focal64 --minimal

A Vagrantfile has been placed in this directory. You are now ready to vagrant up your first virtual environment. Please read the comments in the Vagrantfile as well as documentation for more information on using Vagrant.

The roles directory is where the roles that want to deploy need to be installed. In this case, we will deploy an Ansible role for chrony (NTP client and server). 

Workdir with a Chrony Role

vettabase tree .
├── ansible.cfg
├── playbook.yml
├── roles
│   └── chrony
│       ├── defaults
│       │   └── main.yml
│       ├── handlers
│       │   └── main.yml
│       ├── meta
│       │   └── main.yml
│       ├── README.md
│       ├── tasks
│       │   ├── configure.yml
│       │   ├── debian.yml
│       │   ├── install.yml
│       │   ├── main.yml
│       │   └── redhat.yml
│       ├── templates
│       │   └── chrony.conf.j2
│       ├── tests
│       │   ├── inventory
│       │   └── test.yml
│       └── vars
│           └── main.yml
└── Vagrantfile
9 directories, 16 files

Deploying on a Vagrant machine

To build a Vagrant machine from the Vagrantfile:

vagrant up

To see your SSH configuration:

% vagrant ssh-config
Host default
HostName 127.0.0.1
User vagrant
Port 2222
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile ~/vettabase/.vagrant/machines/default/virtualbox/private_key
IdentitiesOnly yes
LogLevel FATAL

It can be useful if you want to know the ansible_hostansible_user , and ansible_port to put in the inventory file.

Let’s deploy our machine using Ansible as a provisioner:

vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[…] 
PLAY RECAP *********************************************************************
default                    : ok=9    changed=3    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
domingo 05 junho 2022  14:55:40 -0400 (0:00:00.566)       0:00:19.537 ********* 
===============================================================================
chrony : Install Chorny ----------------------------------------------------------------------- 16.81s
Gathering Facts ----------------------------------------------------------------------- 1.38s
chrony : Upload chrony.conf template ---------------------------------- 0.63s
chrony : Restart chrony ----------------------------------------------  0.57s
chrony : Validate if the ntp_servers is not none -----------------------0.04s
chrony : Load the Debian specific defaults ---------------------------- 0.02s
chrony : Override Chrony defaults for Debian systems. ------------------0.02s
chrony : include_tasks ------------------------------------------------ 0.02s
chrony : include_tasks ------------------------------------------------ 0.02s
chrony : Load the RedHat specific defaults ---------------------------- 0.01s

All the code for this blog post is available at deploy-ansible-role-with-vagrant GitHub repository.

Conclusion

In this post we discussed how to deploy Ansible roles on a Vagrant machine. If you need support setting up development environments with Vagrant, or if you need help with Ansible, consider our Database Automation service.

Aldo Junior

About Aldo Junior

Aldo is a MySQL and MariaDB consultant at Vettabase. He has a passion for open source database systems. He likes to play guitar and play with your dog in his free time.

4 Replies to “Deploying an Ansible role on a Vagrant machine”

Leave a Reply

Your email address will not be published.

*