(R)?ex the friendly automation framework

News

2023-08-05
Rex-1.14.3

Rex-1.14.3 is now available on CPAN. It contains bug fixes for local package installation, command existence checks, and Git tests.

2023-05-05
Rex-1.14.2

The Rex-1.14.2 release is now available on CPAN. It contains bug fixes for running local commands on Windows, cloning git repositories, and test suite fixes for the upcoming perl-5.38.0 release.

2023-03-17
Call for papers TPRC 2023

Dean Hamstead from the The Perl and Raku Foundation Marketing Committee has sent an invitation to present about Rex at TPRC 2023. I’m posting it here to increase visibility.

2023-03-05
Rex-1.14.1

The Rex-1.14.1 release is now available on CPAN. It contains bug fixes and documentation updates.

2023-02-05
Rex-1.14.0

The Rex-1.14.0 release is now available on CPAN. It contains improved Rexfile loading, documentation updates, and bumps the minimum required Perl version to 5.12.5.

Events

2021-03-08
Learning automation using Rex

Ferenc Erki (FErki) will be the guest of Gábor Szabó on the next Code Maven live stream to learn about automation using Rex. Register for the free event via Code Maven or Meetup, and join the discussion!

2020-03-05
Unexpected use cases with Rex

Unexpected use cases with Rex at the 22nd German Perl/Raku Workshop 2020 in Erlangen by Ferenc Erki (FErki).

2019-11-09
Rex & Friends

Rex & Friends talk at the Barcelona Perl & Friends 2019 by Ferenc Erki (FErki).

» Home » Docs » Rex book » The rex dsl » Using Environments

Using Environments

With environments it is easy to group your servers depending on the maturity of your configuration or your code.

You can create environments for dev, staging and production machines. There is no limit for environments, so you can create as much as you need.

The classic way is to have 3 environments. The development environment for integration tests, mostly with fewest machines. The staging environment, mostly with the same resource layers as production. And the production environment.

Environments

Creating Environments

Creating environments is as easy as creating groups. To create environments you can use the environment function. Inside an environment you can place everything that is specific for this environment (like authentication, server groups, tasks, ...).

# Rexfile
use Rex -feature => ['1.0'];

environment test => sub {
    user "root";
    password "b0x";

    group frontend   => "fe01.test";
    group middleware => "mw01.test";
    group dbwrite    => "dbm01.test";
};

environment stage => sub {
    user "root";
    password "b0xst4g3";

    group loadbalancer => "lb01.stage";
    group frontend     => "fe01.stage";
    group middleware   => "mw01.stage";
    group dbread       => "dbs01.stage";
    group dbwrite      => "dbm01.stage";
};

environment live => sub {
    user "admin";
    password "b0xl1v3";
    sudo_password "b0xl1v3";
    sudo TRUE;

    group loadbalancer => "lb[01..02].live";
    group frontend     => "fe[01..03].live";
    group middleware   => "mw[01..02].live";
    group dbread       => "dbs[01..02].live";
    group dbwrite      => "dbm01.live";
};

Running tasks

To run the task inside a special environment you have to use the cli option -E

$ rex -E stage $task

If you need to configure systems depending on the environment you can get the current environment inside a task with the environment function.

# Rexfile
task "prepare",
  group => "frontend",
  make {
    # configure ntp.conf depending on the environment
    my $ntp_server = case environment, {
        test      => ["ntp01.test"],
          stage   => ["ntp01.stage"],
          live    => [ "ntp01.live", "ntp02.live" ],
          default => ["ntp01.test"],
    };

    file "/etc/ntp.conf",
      content   => template( "templates/etc/ntp.conf", ntp_server => $ntp_server ),
      owner     => "root",
      group     => "root",
      mode      => 644,
      on_change => make { service ntpd => "restart"; };
  };

Environments and the CMDB

If you're using a CMDB to separate data from code you can also create YAML files for the different environments.

The lookup path for the default YAML CMDB is as follow:

The YAML files

# File: cmdb/default.yml
ntp_server:
  - ntp01.test

# File: cmdb/test/default.yml
ntp_server:
  - ntp01.test

# File: cmdb/stage/default.yml
ntp_server:
  - ntp01.stage

# File: cmdb/live/default.yml
ntp_server:
  - ntp01.live
  - ntp02.live

The Rexfile

To use the CMDB you have to require and configure the Rex::CMDB module first.

# Rexfile
use Rex -feature => ['1.0'];
use Rex::CMDB;

set cmdb => {
    type => "YAML",
    path => "./cmdb",
};

task "prepare",
  group => "frontend",
  make {
    # configure ntp.conf depending on the environment
    my $ntp_server = get cmdb "ntp_server";

    file "/etc/ntp.conf",
      content   => template( "templates/etc/ntp.conf", ntp_server => $ntp_server ),
      owner     => "root",
      group     => "root",
      mode      => 644,
      on_change => make { service ntpd => "restart"; };
  };

Now you can run the task with rex -E test prepare.

Proudly powered by Perl and built with Statocles

GitHub repository and discussions / Chat on Matrix and IRC / Mailing list on Google Groups (retired: rex-users@freelists)

MetaCPAN / Twitter / StackShare / Server Fault   -.ô.-   Disclaimer