(R)?ex the friendly automation framework

News

2020-11-05
Rex-1.13.1

Happy 10th birthday, Rex!

The Rex-1.13.1 release is now available on CPAN. It is focusing on CMDB bugfixes and documentation, while also fixing a missing feature flag. Upgrade is recommended for all users.

2020-10-05
Rex-1.13.0

The Rex-1.13.0 release is now available on CPAN. It adds on_no_change hooks for file management commands to trigger further actions when files are left unchanged. Upgrade is recommended for all users.

2020-09-05
Rex-1.12.2

The Rex-1.12.2 release is now available on CPAN. It fixes various regressions, as well as bugs around file management. Upgrade is highly recommended for all users.

2020-08-05
Rex-1.12.1

The Rex-1.12.1 release is now available on CPAN. It adds documentation for feature flags, and fixes a release packaging issue.

2020-07-05
Rex-1.12.0

The Rex-1.12.0 release is now available on CPAN. It adds support for local rsync operations, improves tab completion for Bash and Zsh, and fixes related bugs. It also discontinues support for running Rex on Windows 7, Windows Server 2008 R2, and older versions since both mainstream and extended support has already ended for these products.

Conferences

2016-06-21

Need Help?

Rex is a pure open source project, you can find community support in the following places:

Professional support is also available.

» 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 Statocles

Google Group / Twitter / GitHub / Mailinglist / irc.freenode.net #rex   -.ô.-   Disclaimer