(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 » 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.

# Rexfile
task "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.

# Rexfile
task "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.

# Rexfile
task "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.

# Rexfile
task "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.

# Rexfile
use 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.

# Rexfile
use 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.

# Rexfile
use 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.

# Rexfile
use 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.

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

task "setup", sub {
    file "/etc/my.conf",
      ensure => "present",
      owner  => "root",
      group  => "root",
      mode   => 600;
};

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