Awesome Open Source
Awesome Open Source

Ansible Role: Packer RHEL/CentOS Configuration for Vagrant VirtualBox


This role configures RHEL/CentOS (either minimal or full install) in preparation for it to be packaged as part of a .box file for Vagrant/VirtualBox deployment using Packer.

The role may be made more flexible in the future, so it could work with other Linux flavors and/or other Packer builders besides VirtualBox, but I'm currently only focused on VirtualBox, since the main use case right now is developer VMs.


Prior to running this role via Packer, you need to make sure Ansible is installed via a shell provisioner, and that preliminary VM configuration (like adding a vagrant user to the appropriate group and the sudoers file) is complete, generally by using a Kickstart installation file (e.g. ks.cfg) with Packer. An example array of provisioners for your Packer .json template would be something like:

"provisioners": [
    "type": "shell",
    "execute_command": "echo 'vagrant' | {{.Vars}} sudo -S -E bash '{{.Path}}'",
    "script": "scripts/"
    "type": "ansible-local",
    "playbook_file": "ansible/main.yml",
    "role_paths": [

The files should contain, at a minimum:


#!/bin/bash -eux
# Add the EPEL repository, and install Ansible.
rpm -ivh
yum -y install ansible python-setuptools


- hosts: all
  sudo: yes
  gather_facts: yes
    - geerlingguy.packer_rhel

You might also want to add another shell provisioner to run cleanup, erasing free space using dd, but this is not required (it will just save a little disk space in the Packer-produced .box file).

If you'd like to add additional roles, make sure you add them to the role_paths array in the template .json file, and then you can include them in main.yml as you normally would. The Ansible configuration will be run over a local connection from within the Linux environment, so all relevant files need to be copied over to the VM; configuratin for this is in the template .json file. Read more: Ansible Local Provisioner.

Role Variables

Available variables are listed below, along with default values (see defaults/main.yml):

packer_rhel_libselinux_package: libselinux-python

The libselinux python package to be installed. This is overridden for RHEL 8 to be python3-libselinux.



Example Playbook

- hosts: all
    - { role: geerlingguy.packer_rhel }



Author Information

This role was created in 2014 by Jeff Geerling, author of Ansible for DevOps.

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
html (10,720
ansible (404
build (125
centos (93
virtualbox (86
packer (67
role (49
hashicorp (39
redhat (30
rhel (27
box (22

Find Open Source By Browsing 7,000 Topics Across 59 Categories