Friday, November 23, 2012

Using (R)?ex for Fame and Fortune


(R)?ex, according to its website, is a tool written in Perl for configuration management and software deployment. The only requirement to have is an SSH connection to the remote server(s).

Alternative Uses

(R)?ex is a very capable tool to automate system administration tasks. For example, I have recently built an RPM build script that copies my local code/files to a build server, builds an RPM package, and downloads the finished RPM to my RPM repository on my local box. I have also used it to generate reports. One recent report was required to see how many of our servers were able to access a certain DNS view.

In fact, the use cases are pretty much unlimited. You can use (R)?ex for any process that requires some sort of automation on a remote server. That said, as powerful as it is, (R)?ex still requires some initial setup to become usable. I have included a couple of my tips below.

Tips and Tricks

Code Structure Wiki recommends using revision control system externals to distribute your modules among your folders that contain your folders. In my case, I wanted to have a single project checked into my GitHub account so I utilized a symbolic link called "lib" in each of the project folders instead.

.... /lib
........ /Module
.... /project_a
........ lib -> ../lib
........ rexfile_1
........ rexfile_2
.... /project_b
........ lib -> ../lib
........ rexfile_3
........ rexfile_4

Naming (R)?ex Files

The online documentation is not clear about the options you have when it comes to naming your (R)?ex files. The default approach is to use the name "Rexfile". This is not really a good implementation as you don't want to have a bunch of files with the same name sitting around on your hard disk. The solution is to name your files appropriately and use the "-f" for the (R)?ex command to execute your scripts:

rex -f script_name

Code Highlighting

If you are using Eclipse like me, you will have problems when dealing with files without any extensions. The solution is to ensure that you have a Perl file with a ".pl" extension and a "use Rex -base;" statement added to the beginning of your scripts to get rid of all the syntax warnings and errors in your IDE.

Dealing with Hard-Coded Passwords

Remember that the (R)?ex file is actually a Perl script. This means that you can utilize the IO::Prompt package to collect SSH user and password information instead of hard-coding your passwords in each script.

use Rex -base;
use IO::Prompt;

my $username = prompt('Enter username: ');
my $password = prompt('Enter password: ', -e => '*');

user qq/$username/;
password qq/$password/;


Certain tasks may take longer than expected so do not forget to utilize the "parallelism" call for those scripts.


(R)?ex output from your script can easily be separated from your script output by piping STDOUT to a file.

rex -f > output.log

However, if you wish your output to look like the (R)?ex output, you can utilize the Rex::Logger module instead.