(R)?ex the friendly automation framework



The Rex-1.13.4 release is now available on CPAN. It contains documentation updates, and a fix for inconsistent behavior between the content and source options of the file command.


The Rex-1.13.3 release is now available on CPAN. It contains documentation updates, and a fix for debconf parsing. Behind the scenes, CI was migrated over to GitHub Actions as well.


The Rex-1.13.2 release is now available on CPAN. It contains only documentation updates, mostly for the built-in template capabilities, but also fixes a few typos.


Happy 10th birthday, Rex!

The Rex-1.13.1 release is now available on CPAN. It is focusing on CMDB bugfixes and documentation, while also fixing a missing feature flag. Upgrade is recommended for all users.


The Rex-1.13.0 release is now available on CPAN. It adds on_no_change hooks for file management commands to trigger further actions when files are left unchanged. Upgrade is recommended for all users.


Learning automation using Rex

Ferenc Erki (FErki) will be the guest of Gábor Szabó on the next Code Maven live stream to learn about automation using Rex. Register for the free event via Code Maven or Meetup, and join the discussion!

Unexpected use cases with Rex

Unexpected use cases with Rex at the 22nd German Perl/Raku Workshop 2020 in Erlangen by Ferenc Erki (FErki).

Rex & Friends

Rex & Friends talk at the Barcelona Perl & Friends 2019 by Ferenc Erki (FErki).

» 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

GitHub / Matrix / IRC / Twitter / Google Groups / StackShare / Mailing list / Server Fault   -.ô.-   Disclaimer