Rex (aka Rexify) is a framework allowing you to automate the configuration and maintenance of many servers.

Rex articles:

Further Plans

  • Mention the idea of separating code and data (e.g. via CMDB, config files, APIs, etc.)
  • Maybe convert the nginx logic from Rexfile into a module (e.g. Rex::CodeMaven::Nginx)
  • add a cert to nginx (e.g. via Let's Encrypt)
  • Enable basic auth in nginx
  • Write a script that will create a new Droplet with my own personal public key. This is going to be the management host.

Getting started with Rex

  • Install Rex
  • Set up a remote host. e.g. Create a Droplet on Digital Ocean
  • Make sure you can ssh to the host without providing a password.
  • Run your first command using Rex: (after replacing USER by the remote username and REMOTE_HOST by the IP or dns name of the remote host.)

rex -u USER -H REMOTE_HOST -e 'say run(q{hostname})'

Get remote hostname using Rexify

rex -u USER -H REMOTE_HOST -e 'say run(q{hostname})'

The output will look like this:

[2021-03-23 19:11:22] INFO - Running task eval-line on REMOTE_HOST
HOSTNAME
[2021-03-23 19:11:28] INFO - All tasks successful on all hosts

List directory on remote host using Rexify

rex -u USER -H REMOTE_HOST -e 'say for run(q{ls -l})'
rex -u USER -H REMOTE_HOST -e 'say scalar run(q{ls -l})'

Create a Rexfile

examples/rex/minimal/Rexfile

use Rex -feature => [qw( 1.4 exec_autodie)];

This is still useless, but we already have a Rexfile that declares its featureset.

Rexfile with simple task showing remote hostname

examples/rex/hostname/Rexfile

use Rex -feature => [qw( 1.4 exec_autodie)];

desc 'Just printing hostname';
task 'print_hostname', sub {
    say run('hostname');
};

cd into the directory of this file.

Then type in

rex -T

it will print out the list of available tasks in the Rexfile.

Tasks
 print_hostname  Just printing hostname

The output will look like this: (you need to replace USER and REMOTE_HOST and it will show something else instead of HOSTNAME

$ rex -u USER -H REMOTE_HOST print_hostname
[2021-03-25 13:21:48] INFO - Running task print_hostname on REMOTE_HOST
HOSTNAME
[2021-03-25 13:21:54] INFO - All tasks successful on all hosts

Rexify update list of packages

apt-get update using Rexify

examples/rex/update/Rexfile

use Rex -feature => [qw( 1.4 exec_autodie)];

desc 'Update list of Linux packages';
task update_pacakges => sub {
    update_package_db;
};

Rexify install Nginx

examples/rex/setup-nginx/Rexfile

use Rex -feature => [qw( 1.4 exec_autodie)];

desc 'Setup nginx';
task setup_nginx => sub {
    # update_package_db;
    pkg 'nginx', ensure => 'present';
    service 'nginx', ensure => 'started';
};

You might first need to update the list of packages.