(R)?ex Deployment & Configuration Management

News

2019-07-02
Deprecating official Rex packages

As part of our efforts to simplify maintenance around RexOps projects, we have decided to stop building Rex packages on our side. We recommend relying on OS packages maintained by the various distributions in their respective upstream repositories, or to install Rex from CPAN.

2019-06-09
Post-migration updates & clean-up

Over the course of the past weeks many clean-ups have been done, reaping the benefits provided by our new Statocles-based site:

2019-05-19
New site engine for rexify.org

After months of work on a new site backend, we reached the MVP today for our site to be switched over to a new engine: from now on, our site will be maintained using the static site generator Statocles.

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 » Working with files and packages » Working with Files

Working with Files

One task in configuration management is managing files and keeping them in a consistent state. Rex gives you some easy to use functions to work with files.

Simple changes

If you need to verify that a given line exists or gets removed from a file you can use append_if_no_such_line and delete_lines_according_to.

# Rexfiletask "setup", sub {
⁠  append_if_no_such_line "/etc/modules", "loop";
⁠};

This code will just append the line loop to /etc/modules if it doesn't exists.

Using regular expression to match the line

It is also possible to define more complex rules. For example if you want to use a regular expression.

# Rexfiletask "setup", sub {
⁠  append_if_no_such_line "/etc/modprobe.d/thinkfan.conf",
⁠    line   => "options thinkpad_acpi fan_control=1",
⁠    regexp => qr{thinkpad_acpi};
⁠};

Using a template and executing extra code if the file changed

If you need to add more than one line to a file or need to add dynamic content to it, you can also use the template() function to do this.

You can also execute code if the file was changed, for example to restart services.

# Rexfiletask "setup", sub {
⁠  append_if_no_such_line "/etc/nagios/hosts.d/frontends.cfg",
⁠    line      => template( "templates/nagios/host.cfg.tpl", %tpl_variables ),
⁠    regexp    => qr/\s*host_name\s*$host/,
⁠    on_change => sub { service nagios => "reload"; };
⁠};

Deleting lines

If you need to remove a line you can use delete_lines_according_to.

# Rexfiletask "setup", sub {
⁠  delete_lines_according_to qr{loop}, "/etc/modules",
⁠    on_change => sub { say "file was modified."; };
⁠};

Managing files with snippets

Sometimes you need to create large configuration files because you don't have the ability to use conf.d folders.

You can do this by creating the snippets and concatenating them at the end.

For this you can use the Rex::Commands::Concat module from http://modules.rexify.org/module/Rex::Commands::Concat.

# Rexfileuse Rex -feature => ['1.0'];
⁠use Rex::Commands::Concat;
⁠
⁠task "prepare", sub {
⁠  concat_fragment "config-header",
⁠    target  => "/etc/some.conf",
⁠    content => "# managed by Rex\n",
⁠    order   => "01";
⁠
⁠  concat_fragment "listen-entry",
⁠    target  => "/etc/some.conf",
⁠    content => "Listen *:80\n",
⁠    order   => "20";
⁠};
⁠
⁠task "setup", sub {
⁠  concat "/etc/some.conf",
⁠    ensure    => "present",
⁠    owner     => "root",
⁠    group     => "root",
⁠    mode      => 644,
⁠    on_change => sub { say "changed..."; };
⁠};

You can create as many concat_fragment() as you need. And you can create them anywhere you want. At the end of your code you have to call the concat() resource to generate the file out of all the fragments that have been written so far.

The file() resource

If you want to manage complete files you can use the file() resource to do so. The file() resource supports also setting the permissions and executing actions for changes.

# Rexfileuse Rex -feature => ['1.0'];
⁠
⁠task "setup", sub {
⁠  file "/etc/my.conf", source => "files/etc/my.conf";
⁠};

This example will just upload the file files/etc/my.conf to the server and stores it at /etc/my.conf.

You can also define the permissions for the file.

# Rexfileuse Rex -feature => ['1.0'];
⁠
⁠task "setup", sub {
⁠  file "/etc/my.conf",
⁠    source => "files/etc/my.conf",
⁠    owner  => "root",
⁠    group  => "root",
⁠    mode   => 600;
⁠};

If you want to execute a command when the file was changed, you can use the on_change option.

# Rexfileuse Rex -feature => ['1.0'];
⁠
⁠task "setup", sub {
⁠  file "/etc/my.conf",
⁠    source    => "files/etc/my.conf",
⁠    owner     => "root",
⁠    group     => "root",
⁠    mode      => 600,
⁠    on_change => sub { service mysqld => "restart"; };
⁠};

This will restart the mysqld service if the file was modified.

Supervising files

It is also possible to just supervise files if they are present or have special permissions. To do this, just create the file() resource without the source or content parameter.

# Rexfileuse Rex -feature => ['1.0'];
⁠
⁠task "setup", sub {
⁠  file "/etc/my.conf",
⁠    ensure => "present",
⁠    owner  => "root",
⁠    group  => "root",
⁠    mode   => 600;
⁠};
Google Group / Twitter / GitHub / Mailinglist / irc.freenode.net #rex   -.ô.-   Disclaimer