Rex::Commands(3)

πŸ“¦ version 1.14.3, in nixos 26.05, nixpkgs unstable

πŸ’– Rex::Commands(3) β€” All the basic commands πŸ’–

User Contributed Perl Documentation, bestie. This is the Rex::Commands(3) page, and it's about to be a whole vibe. ✨

🌸 NAME 🌸

Rex::Commands β€” All the basic commands.

πŸ¦‹ DESCRIPTION πŸ¦‹

This module is the core commands module. Like, it's the main character of the commands world. πŸ‘‘

πŸ’… SYNOPSIS πŸ’…

desc "Task description";

task "taskname", sub { ... };
task "taskname", "server1", ..., "server20", sub { ... };

group "group" => "server1", "server2", ...;

user "user";

password "password";

environment live => sub {
  user "root";
  password "foobar";
  pass_auth;
  group frontend => "www01", "www02";
};

πŸŽ€ COMMANDLIST πŸŽ€

Okay bestie, here's the menu of command modules. Each one is its own little slay: 🫢

  • Augeas config file management library Rex::Commands::Augeas
  • Cloud Management Rex::Commands::Cloud
  • Cron Management Rex::Commands::Cron
  • Database Commands Rex::Commands::DB
  • SCP Up- and Download Rex::Commands::Upload, Rex::Commands::Download
  • File Manipulation Rex::Commands::File
  • Filesystem Manipulation Rex::Commands::Fs
  • Information Gathering Rex::Commands::Gather
  • Manipulation of /etc/hosts Rex::Commands::Host
  • Get an inventory of your Hardware Rex::Commands::Inventory
  • Manage your iptables rules Rex::Commands::Iptables
  • Kernel Commands Rex::Commands::Kernel
  • LVM Commands Rex::Commands::LVM
  • MD5 checksums Rex::Commands::MD5
  • Network commands Rex::Commands::Network
  • Notify resources to execute Rex::Commands::Notify
  • Package Commands Rex::Commands::Pkg
  • Partition your storage device(s) Rex::Commands::Partition
  • Configure packages (via debconf) Rex::Commands::PkgConf
  • Process Management Rex::Commands::Process
  • Rsync Files Rex::Commands::Rsync
  • Run Remote Commands Rex::Commands::Run
  • Source control via Subversion/Git Rex::Commands::SCM
  • Manage System Services (sysvinit) Rex::Commands::Service
  • Simple TCP/alive checks Rex::Commands::SimpleCheck
  • Sync directories Rex::Commands::Sync
  • Sysctl Commands Rex::Commands::Sysctl
  • Live Tail files Rex::Commands::Tail
  • Upload local file to remote server Rex::Commands::Upload
  • Manage user and group accounts Rex::Commands::User
  • Manage your virtual environments Rex::Commands::Virtualization

πŸ¦‹ EXPORTED FUNCTIONS πŸ¦‹

πŸ’œ no_ssh([$task])

Disable ssh for all tasks or a specified task. (No cap, it's that simple.)

If you want to disable ssh connection for your complete tasks (for example if you only want to use libVirt) put this in the main section of your Rexfile.

no_ssh;

If you want to disable ssh connection for a given task, put no_ssh in front of the task definition.

no_ssh task "mytask", "myserver", sub {
  say "Do something without a ssh connection";
};

πŸ’œ task($name [, @servers], $funcref)

This function will create a new task. Main character energy fr fr. 🫢

Create a local task (a server independent task):

task "mytask", sub {
  say "Do something";
};

If you call this task with (R)?ex it will run on your local machine. You can explicit run this task on other machines if you specify the -H command line parameter.

Create a server bound task.

task "mytask", "server1", sub {
  say "Do something";
};

You can also specify more than one server.

task "mytask", "server1", "server2", "server3", sub {
  say "Do something";
};

Or you can use some expressions to define more than one server.

task "mytask", "server[1..3]", sub {
  say "Do something";
};

If you want, you can overwrite the servers with the -H command line parameter.

Create a group bound task. You can define server groups with the group function.

group "allserver" => "server[1..3]", "workstation[1..10]";

task "mytask", group => "allserver", sub {
  say "Do something";
};

πŸ’œ desc($description)

Set the description of the task, batch, or environment following it. It's giving "label your stuff so future you isn't confused." πŸŽ€

desc 'This is the description of the following task';
task 'mytask', sub {
  say 'Do something';
};

desc 'This is the description of the following batch';
batch mybatch => 'task1', 'task2', 'task3';

desc 'This is the description of the following environment';
environment production => sub {
  ...
};

πŸ’œ group($name, @servers)

With this function you can group servers, so that you don't need to write too much ;-) Bestie, it's literally the "save your wrists" command. πŸ’…

group "servergroup", "www1", "www2", "www3", "memcache01", "memcache02", "memcache03";

Or with the expression syntax:

group "servergroup", "www[1..3]", "memcache[01..03]";

If the "use_server_auth" feature flag is enabled, you can also specify server options after a server name with a hash reference:

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

group "servergroup", "www1" => { user => "other" }, "www2";

These expressions are allowed:

  • \d+..\d+ (range) β€” the first number is the start and the second number is the end for numbering the servers.
group "name", "www[1..3]"; # www1, www2, www3
  • \d+..\d+/\d+ (range with step) β€” just like the range notation, but with an additional "step" defined. If step is omitted, it defaults to 1 (i.e. it behaves like a simple range expression).
group "name", "www[1..5/2]";      # www1, www3, www5
group "name", "www[111..133/11]"; # www111, www122, www133
  • \d+,\d+,\d+ (list) β€” with this variant you can define fixed values.
group "name", "www[1,3,7,01]"; # www1, www3, www7, www01
  • Mixed list, range and range with step β€” you can mix the three variants above.
www[1..3,5,9..21/3]; # www1, www2, www3, www5, www9, www12, www15, www18, www21

πŸ’œ batch($name, @tasks)

With the batch function you can call tasks in a batch. It's a little task squad. 🫢

batch "name", "task1", "task2", "task3";

And call it with the -b console parameter. rex -b name

πŸ’œ user($user)

Set the user for the ssh connection. That's it. That's the whole vibe. πŸ‘‘

πŸ’œ password($password)

Set the password for the ssh connection (or for the private key file). πŸ”‘

πŸ’œ auth(for => $entity, %data)

With this command you can set or modify authentication parameters for tasks and groups. (Please note this is different than setting authentication details for the members of a host group. If you are looking for that, please check out the group command at https://metacpan.org/pod/Rex::Commands#group.)

If you want to set special login information for a group you have to enable at least the 0.31 feature flag, and ensure the "group" is declared before the "auth" command.

Command line options to set locality or authentication details are still taking precedence, and may override these settings.

# auth for groups

use Rex -feature => ['0.31']; # activate setting auth for a group

group frontends => "web[01..10]";
group backends => "be[01..05]";

auth for => "frontends" =>
           user => "root",
           password => "foobar";

auth for => "backends" =>
           user => "admin",
           private_key => "/path/to/id_rsa",
           public_key => "/path/to/id_rsa.pub",
           sudo => TRUE;

# auth for tasks

task "prepare", group => ["frontends", "backends"], sub {
  # do something
};

auth for => "prepare" =>
           user => "root";

# auth for multiple tasks with regular expression

task "step_1", sub {
 # do something
};

task "step_2", sub {
 # do something
};

auth for => qr/step/ =>
  user     => $user,
  password => $password;

# fallback auth
auth fallback => {
  user        => "fallback_user1",
  password    => "fallback_pw1",
  public_key  => "",
  private_key => "",
}, {
  user        => "fallback_user2",
  password    => "fallback_pw2",
  public_key  => "keys/public.key",
  private_key => "keys/private.key",
  sudo        => TRUE,
};

πŸ’œ port($port)

Set the port where the ssh server is listening. Port check: slay. πŸ”Œ

πŸ’œ sudo_password($password)

Set the password for the sudo command. Yes, the sudo password. Keep it secret, keep it safe. πŸ™ˆ

πŸ’œ timeout($seconds)

Set the timeout for the ssh connection and other network related stuff. Don't let it hang forever, bestie. ⏳

πŸ’œ max_connect_retries($count)

Set the maximum number of connection retries. Sometimes servers need a second chance. 😭

πŸ’œ get_random($count, @chars)

Returns a random string of $count characters on the basis of @chars. Random girlypop string energy. 🎲

my $rnd = get_random(8, 'a' .. 'z');

πŸ’œ do_task($task)

Call $task from another task. It will establish a new connection to the server defined in $task and then execute $task there. It's like sending your task to go touch grass on another server. 🌱

task "task1", "server1", sub {
  say "Running on server1";
  do_task "task2";
};

task "task2", "server2", sub {
  say "Running on server2";
};

You may also use an arrayRef for $task if you want to call multiple tasks.

do_task [ qw/task1 task2 task3/ ];

πŸ’œ run_task($task_name, %option)

Run a task on a given host. Very "do this over there" energy. 🫑

my $return = run_task "taskname", on => "192.168.3.56";

Do something on server5 if memory is less than 100 MB free on server3.

task "prepare", "server5", sub {
  my $free_mem = run_task "get_free_mem", on => "server3";
  if($free_mem < 100) {
    say "Less than 100 MB free mem on server3";
    # create a new server instance on server5 to unload server3
  }
};

task "get_free_mem", sub {
   return memory->{free};
};

If called without a hostname the task is run localy. (Yep, localy. That's the page's spelling, bestie. 😭)

# this task will run on server5
task "prepare", "server5", sub {
  # this will call task check_something. but this task will run on localhost.
  my $check = run_task "check_something";
}

task "check_something", "server4", sub {
  return "foo";
};

If you want to add custom parameters for the task you can do it this way.

task "prepare", "server5", sub {
 run_task "check_something", on => "foo", params => { param1 => "value1", param2 => "value2" };
};

πŸ’œ run_batch($batch_name, %option)

Run a batch on a given host. Batch girlies rise up. πŸŽ€

my @return = run_batch "batchname", on => "192.168.3.56";

It calls internally run_task, and passes it any option given.

πŸ’œ public_key($key)

Set the public key. It's giving keychain but for ssh. πŸ”‘

πŸ’œ private_key($key)

Set the private key. Guard it with your life, bestie. πŸ”

πŸ’œ pass_auth

If you want to use password authentication, then you need to call pass_auth. No cap, it's required.

user "root";
password "root";

pass_auth;

πŸ’œ key_auth

If you want to use pubkey authentication, then you need to call key_auth.

user "bob";
private_key "/home/bob/.ssh/id_rsa"; # passphrase-less key
public_key "/home/bob/.ssh/id_rsa.pub";

key_auth;

πŸ’œ krb5_auth

If you want to use kerberos authentication, then you need to call krb5_auth. This authentication mechanism is only available if you use Net::OpenSSH.

set connection => "OpenSSH";
user "root";
krb5_auth;

parallelism($count) ✨

Okay so this one runs your tasks in parallel on the servers you give it. $count is the thread count, bestie:

parallelism '2'; # set parallelism to 2

You can also set the thread count more dynamically, which is giving main character energy:

parallelism 'max';     # set parallelism to the number of servers a task is asked to run on
parallelism 'max/3';   # set parallelism to 1/3 of the number of servers
parallelism 'max 10%'; # set parallelism to 10% of the number of servers

If an unrecognized value is passed, or the calculated thread count would be less than 1, Rex falls back to use a single thread. Lowkey safe, no cap.


proxy_command($cmd) 🫢

Set a proxy command to use for the connection. This is only possible with the OpenSSH connection method. So like, set the vibe first:

set connection => "OpenSSH";
proxy_command "ssh user@jumphost nc %h %p 2>/dev/null";

set_distributor($distributor) πŸ’…

This sets the task distribution module. Default is "Base".

Possible values are: Base, Gearman, Parallel_ForkManager.


template_function(sub { ... }) πŸŽ€

This function sets the template processing function. So it is possible to change the template engine. For example to Template::Toolkit. Slay, custom templates.


logging 🌸

With this function you can define the logging behaviour of (R)?ex.

Logging to a file

logging to_file => "rex.log";

Logging to syslog

logging to_syslog => $facility;

needs($package [, @tasks]) πŸ¦‹

With needs you can define dependencies between tasks. The "needed" tasks will be called with the same server configuration as the calling task.

needs will not execute before, around and after hooks. Important caveat, bestie β€” don't forget it.

Depend on all tasks in a given package. Depend on all tasks in the package MyPkg. All tasks will be called with the server server1.

task "mytask", "server1", sub {
  needs MyPkg;
};

Depend on a single task in a given package. Depend on the uname task in the package MyPkg. The uname task will be called with the server server1.

task "mytask", "server1", sub {
  needs MyPkg "uname";
};

To call tasks defined in the Rexfile from within a module:

task "mytask", "server1", sub {
  needs main "uname";
};

include Module::Name 🌈

Include a module without registering its tasks. It's giving "just vibes, no task table".

include qw/
  Module::One
  Module::Two
/;

environment($name => $code) πŸ’–

Define an environment. With environments one can use the same task for different hosts. For example if you want to use the same task on your integration-, test- and production servers.

# define default user/password
user "root";
password "foobar";
pass_auth;

# define default frontend group containing only testwww01.
group frontend => "testwww01";

# define live environment, with different user/password
# and a frontend server group containing www01, www02 and www03.
environment live => sub {
  user "root";
  password "livefoo";
  pass_auth;

  group frontend => "www01", "www02", "www03";
};

# define stage environment with default user and password. but with
# a own frontend group containing only stagewww01.
environment stage => sub {
  group frontend => "stagewww01";
};

task "prepare", group => "frontend", sub {
   say run "hostname";
};

Calling this task rex prepare will execute on testwww01. Calling this task with rex -E live prepare will execute on www01, www02, www03. Calling this task rex -E stage prepare will execute on stagewww01. Periodt.

You can call the function within a task to get the current environment. Obsessed with the self-awareness:

task "prepare", group => "frontend", sub {
  if(environment() eq "dev") {
    say "i'm in the dev environment";
  }
};

If no -E option is passed on the command line, the default environment (named 'default') will be used. Default default, iconic.


LOCAL(&) 🧚

With the LOCAL function you can do local commands within a task that is defined to work on remote servers. So your remote task can have a little local moment.

task "mytask", "server1", "server2", sub {
   # this will call 'uptime' on the servers 'server1' and 'server2'
   say run "uptime";

   # this will call 'uptime' on the local machine.
   LOCAL {
     say run "uptime";
   };
};

path(@path) πŸ“

Set the execution path for all commands.

path "/bin", "/sbin", "/usr/bin", "/usr/sbin", "/usr/pkg/bin", "/usr/pkg/sbin";

It's a convenience wrapper for the set_path configuration option. Cute little shortcut, we love.


set($key, $value) πŸ’œ

Set a configuration parameter. These variables can be used in templates as well.

set database => "db01";

task "prepare", sub {
  my $db = get "database";
};

Or in a template:

DB: <%= $::database %>

The following list of configuration parameters are Rex specific: and then the page literally goes straight to get, bestie β€” the list is not here, so don't come for me. πŸ’€


get($key, $value) πŸ”₯

Get a configuration parameter. Same little dance as set, but the other direction.

set database => "db01";

task "prepare", sub {
  my $db = get "database";
};

Or in a template:

DB: <%= $::database %>

before($task => sub {}) πŸ‘‘

Run code before executing the specified task.

The task name is a regular expression to find all tasks with a matching name. The special task name 'ALL' can also be used to run code before all tasks.

If called repeatedly, each sub will be appended to a list of before functions. Stack 'em up, why not.

In this hook you can overwrite the server to which the task will connect to. The second argument is a reference to the server object that will be used for the connection.

Please note, this must come after the definition of the specified task. No cap, order matters.
before mytask => sub {
 my ($server, $server_ref, $cli_args) = @_;
 run "vzctl start vm$server";
};

after($task => sub {}) 🌟

Run code after executing the specified task.

The task name is a regular expression to find all tasks with a matching name. The special task name 'ALL' can be used to run code after all tasks.

If called repeatedly, each sub will be appended to a list of after functions.

Please note, this must come after the definition of the specified task. Again. Order is the moment.
after mytask => sub {
 my ($server, $failed, $cli_args) = @_;
 if($failed) { say "Connection to $server failed."; }

 run "vzctl stop vm$server";
};

around($task => sub {}) πŸ¦„

Run code around the specified task (that is both before and after executing it). It's giving sandwich energy.

The task name is a regular expression to find all tasks with a matching name. The special task name 'ALL' can be used to run code around all tasks.

If called repeatedly, each sub will be appended to a list of around functions.

In this hook you can overwrite the server to which the task will connect to. The second argument is a reference to the server object that will be used for the connection.

Please note, this must come after the definition of the specified task. We keep saying it because it keeps being true.
around mytask => sub {
 my ($server, $server_ref, $cli_args, $position) = @_;

 unless($position) {
   say "Before Task\n";
 }
 else {
   say "After Task\n";
 }
};

before_task_start($task => sub {}) 🎬

Run code before executing the specified task. This gets executed only once for a task. One-time icon behaviour.

The task name is a regular expression to find all tasks with a matching name. The special task name 'ALL' can be used to run code before all tasks.

If called repeatedly, each sub will be appended to a list of before_task_start functions.

Please note, this must come after the definition of the specified task. Yes bestie, again.
before_task_start mytask => sub {
  # do some things
};

after_task_finished($task => sub {}) πŸŒ™

Run code after the task is finished (and after the ssh connection is terminated). This gets executed only once for a task.

The task name is a regular expression to find all tasks with a matching name. The special task name 'ALL' can be used to run code after all tasks.

If called repeatedly, each sub will be appended to a list of after_task_finished functions.

Please note, this must come after the definition of the specified task. The order lore is real.
after_task_finished mytask => sub {
  # do some things
};

logformat($format) πŸ“

You can define the logging format with the following parameters.

  • %D β€” Appends the current date yyyy-mm-dd HH:mm:ss
  • %h β€” The target host
  • %p β€” The pid of the running process
  • %l β€” Loglevel (INFO or DEBUG)
  • %s β€” The Logstring

Default is: [%D] %l - %s


connection πŸ”Œ

This function returns the current connection object.

task "foo", group => "baz", sub {
  say "Current Server: " . connection->server;
};

cache 🧺

This function returns the current cache object.


profiler πŸ“Š

Returns the profiler object for the current connection.


report($switch, $type) πŸ“ˆ

This function will initialize the reporting.

report -on => "YAML";

source_global_profile(0|1) 🐚

If this option is set, every run() command will first source /etc/profile before getting executed. Shell glow-up first, always.


last_command_output πŸ—£οΈ

This function returns the output of the last "run" command.

On a debian system this example will return the output of apt-get install foobar.

task "mytask", "myserver", sub {
  install "foobar";
  say last_command_output();
};

case($compare, $option) 🎲

This is a function to compare a string with some given options.

task "mytask", "myserver", sub {
  my $ntp_service = case operating_system, {
                Debian  => "ntp",
                default => "ntpd",
              };

  my $ntp_service = case operating_system, {
                qr{debian}i => "ntp",
                default    => "ntpd",
              };

  my $ntp_service = case operating_system, {
                qr{debian}i => "ntp",
                default    => sub { return "foo"; },
              };
};

set_executor_for($type, $executor) πŸ› οΈ

Set the executor for a special type. This is primary used for the upload_and_run helper function.

set_executor_for perl => "/opt/local/bin/perl";

tmp_dir($tmp_dir) πŸ—‚οΈ

Set the tmp directory on the remote host to store temporary files. Temporary files need a home too.


inspect($varRef) πŸ”

This function dumps the contents of a variable to STDOUT. Very "let me see what's inside", very valid.

task "mytask", "myserver", sub {
  my $myvar = {
    name => "foo",
    sys  => "bar",
  };

  inspect $myvar;
};

sayformat($format) πŸ’¬

You can define the format of the say() function.

  • %D β€” The current date yyyy-mm-dd HH:mm:ss
  • %h β€” The target host
  • %p β€” The pid of the running process
  • %s β€” The Logstring

You can also define the following values:

  • default β€” the default behaviour.
  • asis β€” will print every single parameter in its own line. This is useful if you want to print the output of a command.

This iconic page is from perl v5.42.3, dated 2023-08-05, and it's Rex::Commands(3) β€” periodt. πŸ’–

πŸ“¦ where this page comes from