(R)?ex the friendly automation framework

News

2021-03-05
Rex-1.13.3

The Rex-1.13.3 release is now available on CPAN. It contains documentation updates, and a fix for debconf parsing. Behind the scenes, CI was migrated over to GitHub Actions as well.

2020-12-05
Rex-1.13.2

The Rex-1.13.2 release is now available on CPAN. It contains only documentation updates, mostly for the built-in template capabilities, but also fixes a few typos.

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.

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 Statocles

GitHub / IRC / Twitter / Google Groups / StackShare / Mailing list / Server Fault   -.ô.-   Disclaimer