Wednesday 7 September 2016

Introduction to (R)?ex


Rex is an agent less system administration automation tool written in perl. You don't really need to know much of perl programming to use Rex. Rex requires perl version 5.8 or greater to function.

Installation:

Rex can be installed by enabling the Rex repository, details of which are mentioned below:

[root@devbox yum.repos.d]# cat rex.repo
[rex]
name=Fedora $releasever - $basearch - Rex Repository
baseurl=https://rex.linux-files.org/CentOS/$releasever/rex/$basearch/
enabled=1

[root@devbox yum.repos.d]# yum repolist rex
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirror.dhakacom.com
 * epel: mirror.wanxp.id
 * extras: mirror.dhakacom.com
 * updates: mirrors.nhanhoa.com
repo id                                                                          repo name                                                                                              status
!rex/7/x86_64                                                                    Fedora 7 - x86_64 - Rex Repository                                                                     127
repolist: 127

Once we've verified that the repository is working we just need to run 'yum install rex' to install the utility.

We can check the installed version of rex with 'rex -v'

[root@devbox ~]# rex -v
(R)?ex 1.4.1
[root@devbox ~]#

Usage:

Rex offers a simplistic syntax to run commands in the shell. Below is an example:

[root@devbox ~]# rex -H "192.168.44.137 192.168.44.135" -e "say run 'uname -a'"
[2016-09-07 07:53:57] INFO - Running task eval-line on 192.168.44.137
Linux devbox 3.10.0-123.el7.x86_64 #1 SMP Mon Jun 30 12:09:22 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
[2016-09-07 07:53:58] INFO - Running task eval-line on 192.168.44.135
Linux rheldb 2.6.32-220.el6.x86_64 #1 SMP Wed Nov 9 08:03:13 EST 2011 x86_64 x86_64 x86_64 GNU/Linux
[2016-09-07 07:53:59] INFO - All tasks successful on all hosts
[root@devbox ~]#

If we wanted to run the commands as a different user, we can type:

[root@devbox ~]# rex -H "192.168.44.137 192.168.44.135" -e "say run 'uname -a; echo --- ; whoami; echo ------; date'" -u james -p 123
[2016-09-07 09:39:17] INFO - Running task eval-line on 192.168.44.137
Linux devbox 3.10.0-123.el7.x86_64 #1 SMP Mon Jun 30 12:09:22 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux---james------Wed Sep  7 09:39:18 PDT 2016
[2016-09-07 09:39:18] INFO - Running task eval-line on 192.168.44.135
Linux rheldb 2.6.32-220.el6.x86_64 #1 SMP Wed Nov 9 08:03:13 EST 2011 x86_64 x86_64 x86_64 GNU/Linux---james------Wed Sep  7 12:39:19 EDT 2016
[2016-09-07 09:39:20] INFO - All tasks successful on all hosts
[root@devbox ~]#

If we want to execute commands on multiple servers sorted in groups then we accomplish them by creating a file called Rexfile. When we run rex it it reads the file "Rexfile" in the current working directory. A Rexfile consists of 2 major parts: Configuration and Task Definitions.

The configuration part consists of the user credentials & server lists on which we need to run commands.
The task definitions are the actual commands that are to be run.

An example Rexfile is given below:

[root@devbox ~]# cat Rexfile
use Rex -feature => ['1.4'];

user "root";
password "123";

group myservers => "192.168.44.137", "192.168.44.135";

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

To execute, we type rex followed by the task name:

[root@devbox ~]# rex uptime
[2016-09-07 07:30:52] INFO - Running task uptime on 192.168.44.137
 07:30:53 up 21:16,  3 users,  load average: 0.00, 0.01, 0.05
[2016-09-07 07:30:53] INFO - Running task uptime on 192.168.44.135
 10:30:53 up 1 day,  1:28,  0 users,  load average: 0.00, 0.00, 0.00
[2016-09-07 07:30:54] INFO - All tasks successful on all hosts
[root@devbox ~]#

In addition to running commands on single/multiple servers, rex can be used for configuring & managing operating system services like ntp & httpd, managing crontab & user administration to some extent. You can check out the documentation here for further reading.

Final Word:
In my brief stint of using rex, I found it to be a neat utility for tunning simple commands across multiple servers. 

No comments:

Post a Comment

Using capture groups in grep in Linux

Introduction Let me start by saying that this article isn't about capture groups in grep per se. What we are going to do here with gr...