Automation with Perl Rex

Today I’ve started to play with perl Rex which is one of the great tool for automating server management tasks. I have never used Chef or Puppet or Ansible, so I did not know what to expect and what are the options available in Chef, Puppet and Ansible, but so far I love it.

Since Rex is using SSH as its transport layer, so we only have to install Rex on one machine (it can be your laptop). We don’t need to install anything on client machines exept SSH server (openssh). Rex configuration is also simple (only if you know perl :D).

Rex runtime configration file name is “Rexfile” and its perl syntax with some Rex own methods. An example of Rexfile as follow:

use Rex -feature => ['1.0'];
use Data::Dumper;

user "root";

group web_server => "prajith.in", "repo.prajith.in";
group db_server  => "192.168.22.225", "192.168.22.249", "192.168.22.250";

desc "Get Disk Free";

task "disk_free", group => "own_server", sub {
  my $output = run "uptime";
  print $output;
};

desc "Get server information";

task "get_os", group => "own_server", sub {
  my $os         = get_operating_system();
  my $os_version = operating_system_version();

  my $memory = memory();
  Rex::Logger::info("RAM: $memory->{total} Mb");
  
  my $cpu_stat = `grep '^cpu[0-9]' /proc/stat`;
  my $cpu = scalar ($cpu_stat =~ /(cpu[0-9])/g);

  my $data = {
    os         => $os,
    os_version => $os_version,
    memory     => $memory,
    cpu        => $cpu,
};
print Dumper $data;
};

You can run the abouve task with the following command:

 
$> rex get_os
$> rex disk_free

You can find more examples at (R)?ex commands

Rex has a great set of modules for managing different things like installing RHEL packages, and managing virtualization(Virtualbox, openvz, etc..)

Automation with Perl Rex

Leave a Reply

Your email address will not be published. Required fields are marked *

Fork me on GitHub