Posted at
PerlDay 13

How to use Rex to manage VirtualBox

More than 5 years have passed since last update.

Fist of all, I'm Chinese. I found no PerlChina Advent and came here. Can here post some artices without using japanese? Seems no prohibit~OK, let's go!

Rex means Remote EXecute. It's a Deployment & Configuration Management, which works like ansible, and DSL designed like puppet. Find here:

But today, I will show something different -- use rex to manage a vbox -- yes, just like vagrant do!

First, generate a vbox template:

$ rexify project-name --template box

$ cd project-name

Now we get a file called 'Rexfile', just like 'Vagrantfile' after vagrant init. An example of Rexfile as follow:


set box => "VBox";

task mytask => sub {
box {
my ($box) = @_;
$box->network(1 => {
type => "bridged" # default "nat",
bridge => "eth0",
$box->forward_port(ssh => [2222, 22]);
$box->share_folder(boxhome => "/path/to/myuser");
user => "root",
password => "box",

Then, we can run the box start/stop:

$ rex init --name=boxname

$ rex stop --name=boxname

Attention, there is one setup task in the Rexfile, we can define our own automatic task to customize this box after it start, a small example as follow:


task 'setup_frontend', sub {

install nginx;
file '/etc/nginx.conf',
content => template('template/httpd.conf.tpl'),
owner => "root",
group => "root",
on_change => sub { service nginx => "restart"; };

Vagrant has provision support for puppet/chef, Rex do it all himself.

BTW, Rex support to define these vboxes in YAML:


type: VBox

type: bridged
bridge: eth0
setup: setup_frontend
type: bridged
bridge: eth0
setup: setup_db

Only one line was needed if we use YAML:

use Rex::Commands::Box init_file => "boxes.yml";

Rex support VMware and Amazon vms too