(R)?ex the friendly automation framework



The Rex-1.12.2 release is now available on CPAN. It fixes various regressions, as well as bugs around file management. Upgrade is highly recommended for all users.


The Rex-1.12.1 release is now available on CPAN. It adds documentation for feature flags, and fixes a release packaging issue.


The Rex-1.12.0 release is now available on CPAN. It adds support for local rsync operations, improves tab completion for Bash and Zsh, and fixes related bugs. It also discontinues support for running Rex on Windows 7, Windows Server 2008 R2, and older versions since both mainstream and extended support has already ended for these products.

Rex on StackShare

Thanks to the support from their amazing team, Rex is now listed on StackShare.


The Rex-1.11.0 release is now available on CPAN. It enhances the behaviour of sysctl and file management, fixes bugs about group management and OpenSSH connection options, and clarifies quite a bit of documentation.



Need Help?

Rex is a pure open source project, you can find community support in the following places:

Professional support is also available.

» Home » Docs » Rex book » The rex dsl » Grouping servers

Grouping servers

Rex offers you a powerful way to group your servers. The simplest way to use groups is to just define a group name and add all your desired servers to this group.

group frontends => "frontend01", "frontend02", "frontend03";
group backends => "backend01", "backend02";

Rex offers also a simple notation to define server ranges, so that you don't need to type so much.

group frontends => "frontend[01..03]";
group backends  => "backend[01..02]";

This notation will just expand to frontend01, frontend02 and frontend03 for the frontends group and to backend01 and backend02 for the backends group.

Custom parameters for servers are possible with a slightly enhanced syntax since version 0.47:

group frontends => "frontend01" => { user => "bob" },
  "frontend02"  => { user => "alice" },

Because the Rexfile is a Perl script you can just use more advanced things like querying a database, ldap or your dns.

To add your groups to the tasks you have to use the group option within the task generation.

task "mytask",
  group => "mygroup",
  sub {
    # do something

If you need to define multiple groups for a task, you can just use an array.

task "mytask",
  group => [ "mygroup", "mygroup2" ],
  sub {
    # do something

Using an INI file to define server groups

Rex offers a simple way to query ini files for group creation. To use ini files you have to use the Rex::Group::Lookup::INI module.

use Rex -feature => ['1.0'];
use Rex::Group::Lookup::INI;

groups_file "/path/to/your/file.ini";

Rex expects the following format inside your INI file.



Rex also offers a little bit advanced functions for the ini file. You can define custom parameters for each server or include groups inside groups.

frontend02 user=root password=f00bar auth_type=pass maintenance=1



These additional options (in this example maintenance can be queried with the option method from the connection object.

task "prepare",
  group => "frontends",
  sub {
    if ( connection->server->option("maintenance") ) {
        say "This server is in maintenance mode, so i'm going to stop all services";
        service [ "apache2", "postfix" ] => "stop";

Quering a database to define server groups

If you want to get your server groups right out of an existing database you can use DBI to connect to your database server. In this example you will learn how to connect to a MySQL database and to get the hosts out of a table.

use Rex -feature => ['1.0'];
use DBI;

my $username = "dbuser";
my $password = "dbpassword";
my $database = "hostdb";
my $hostname = "mysql.intern.lan";
my $port     = 3306;
my $dsn      = "DBI:mysql:database=$database;host=$hostname;port=$port";
my $dbh      = DBI->connect( $username, $password );

my $sth = $dbh->prepare("SELECT * FROM hostlist WHERE enabled=1");

my %server_group = ();
while ( my $row = $sth->fetchrow_hashref ) {
    my $group_name  = $row->{server_group};
    my $server_name = $row->{server_name};
    push @{ $server_group{$group_name} }, $server_name;

map { group $_ => @{ $server_group{$_} }; } keys %server_group;

This example expects the following column names:

Creating custom groups

If there is no built-in function that fits your needs for group creation, you can do it all by yourself. Because the Rexfile is just a plain perl script and the group command is just a perl function that expects the group name as first parameter, and uses all other parameters for the servers, you can create your own function.

my @list = ( "some", "list", "entries" );
group mygroup  => grep { /list/ } @list;
group myserver => map  { "s$_.domain.com" } qw(1 3 7);

Proudly powered by Statocles

Google Group / Twitter / GitHub / Mailinglist / irc.freenode.net #rex   -.ô.-   Disclaimer