(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 » Guides » How to start using (R)?ex

How to start using (R)?ex

This is a small how to showing the first steps with Rex.

Basic architecture

Basic architecture

Rex is an automation framework which can manage local machines and uses SSH as an agent to manage remote ones.

Rex provides a simple DSL that is completely syntax-compatible with Perl. You can use it without any Perl experience if you want, but you can easily learn just enough Perl for Rex.

The starting point of every Rex project is the so-called Rexfile. You can think of this file like a Makefile. You can define your inventory, environments, authentication info and tasks in this file, among many other things.

A task is really just a bunch of related commands. For example installing a package, uploading the configuration file and starting the service. You can also call tasks from other tasks or create rollback scenarios to handle cases when something goes wrong during the execution.


To run Rex you don't need much.

Control host

You can run Rex from your workstation or on a central control host.

Rex requires at least Perl 5.10.1 on this machine, and of course some modules as dependencies. For the best experience we recommend using the latest stable release of Perl. If you're using the packages provided by your OS distribution, or you install Rex from CPAN, then those dependencies will be installed automatically.

You can find information on the Get Rex page about how to install Rex itself.

Managed hosts

On the systems you want to manage you need only two things: a Perl 5 interpreter, and a valid account for the SSH server running there.

Rex only uses core Perl modules on the remote hosts, but some operating systems (e.g. RHEL/CentOS, Fedora, OpenWRT) might not supply all of them out of the box. In this case you might need to install them first, and this mostly means the Digest::MD5 module (but it depends on the OS, really). You can install this module with yum install perl-Digest-MD5.

If you want to run administrative tasks on the remote machines, you obviously would need root access on them, or at least sudo to escalate privileges.

Creating a Rexfile

First we need to create a new directory to store your Rexfile in it, and change to that directory:

$ mkdir my-first-rex-project
$ cd my-first-rex-project

Now change into this directory and create a file called Rexfile with the following contents:

use Rex -feature => ['1.4'];

user 'my-user';
password 'my-password';

group myservers => 'mywebserver', 'mymailserver', 'myfileserver';

desc 'Get the uptime of all servers';
task 'uptime',
  group => 'myservers',
  sub {
    my $output = run 'uptime';
    say $output;

The special first line use Rex -feature => ['1.4']; enables all features that are available for version 1.4. If you want to know more about feature flags, please check out the How to enable or disable features page.

Executing the uptime task will login as my-user with the password my-password to all the servers in the group myservers and run the command uptime. You can run it like this:

$ rex uptime

Adding a second task

To add a second task, just add the next lines to your Rexfile.

desc 'Start Apache service';
task 'start_apache',
  group => 'myservers',
  sub {
    service 'apache2' => 'start';

This task will start the service named apache2 on all the servers in the myservers group.

Display all tasks in a Rexfile

Use the following command to display all tasks in your Rexfile use the following command:

$ rex -T
  start_apache                   Start Apache service
  uptime                         Get the uptime of all servers
Server Groups
  myservers                       mywebserver, mymailserver, myfileserver


In the previous example we showed how you can login with a user and a password. Of course, it's easy to use key authentication instead, just add key_auth instead of password:

user 'my-user';

It is also possible to use your keys with a passphrase if you specify both key_auth and password:

user 'my-user';
password 'key-passphrase';

On Windows, you also have to point to your private and public keys:

user 'my-user';
private_key '/home/user/.ssh/id_rsa';
public_key '/home/user/.ssh/id_rsa.pub';

If you don't want to add your password or passphrase to the Rexfile, you can also get it from an external source, or you can use ssh-agent. Rex will automatically try to use it when you remove both password and key_auth.

And if you omit even user, it will just try to use the name of the currently logged in user.

For even more options, check out this Authentication page.

Managing services

Managing services really only need a few common steps: install, configure, and run (of course, add in destroy to get full life cycle management).

In this example you will learn how to install and configure an NTP server. You can adapt this example to any other service easily.

# Rexfile
use Rex -feature => ['1.4'];

user 'root';

group all_servers => 'srv[001..003]';

task 'setup_ntp',
  group => 'all_servers',
  sub {
    pkg 'ntpd', ensure => 'present'; # let's install the package first

    file '/etc/ntp.conf',                # then upload a configuration file
      source    => 'files/etc/ntp.conf', # use a source file under files/etc
      on_change => sub {                 # and execute something if the file has changed
        service ntpd => 'restart';       # in this case, restart the service

    service 'ntpd', ensure => 'started'; # start the service now and also after reboot

That's all.

This task will now install the ntpd package on your servers, upload a configuration file and start the service.

In case you wonder about the order of commands, Rex will always execute them from top to bottom.

If you want to learn more, it's probably best to continue reading about using modules and templates.

Proudly powered by Statocles

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