Using Vagrant, VirtualBox and Ansible with CentOS 7

This would be the case when you need to test playbooks locally before using Ansible to deploy them. We assume you already have installed Vagrant, Ansible and VirtualBox. Ansible is one of many provisioners integrated with Vagrant. Lets see how it works

Create Vagrant box with CentOS 7

mkdir centtest
cd centtest
sudo vagrant init

Use Publicly available boxes to add CentsOS 7 box
Add directives to Vagrantfile file

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
end

Next start up and ssh

vagrant up
vagrant ssh

Some useful Vagrant commands
To gracefully shutdown

vagrant halt "ID"

To stop it

vagrant destroy

To remove box

vagrant box remove

To view boxes

sudo vagrant global-status

Use Ansible to provision our vm

Make sure Ansible is installed on Vagrant host
Add lines below to Vigrantfile before the ‘end’

# Provisioning configuration for Ansible.
   config.vm.provision "ansible" do |ansible|
    ansible.playbook = "playbook.yml"
     ansible.sudo = true
   end	
# Run commands as root.
end

Create playbook

In the same directory as Vigrantfile create playbook.yml file with the following content. This test playbook will test weather ntp service is installed and running

---
- hosts: all
  tasks:
  - name: Ensure NTP (for time synchronization) is installed.
    yum: name=ntp state=installed
  - name: Ensure NTP is running.
    service: name=ntpd state=started enabled=yes

Run playbook from current directory

vagrant provision