Rex::Commands::File - Transparent File Manipulation
With this module you can manipulate files.
task "read-passwd", "server01", sub { my $fh = file_read "/etc/passwd"; for my $line = ($fh->read_all) { print $line; } $fh->close; }; task "read-passwd2", "server01", sub { say cat "/etc/passwd"; }; task "write-passwd", "server01", sub { my $fh = file_write "/etc/passwd"; $fh->write("root:*:0:0:root user:/root:/bin/sh\n"); $fh->close; }; delete_lines_matching "/var/log/auth.log", matching => "root"; delete_lines_matching "/var/log/auth.log", matching => qr{Failed}; delete_lines_matching "/var/log/auth.log", matching => "root", qr{Failed}, "nobody";
Parse a template and return the content.
my $content = template("/files/templates/vhosts.tpl", name => "test.lan", webmaster => 'webmaster@test.lan');
This function is the successor of install file. Please use this function to upload files to you server.
task "prepare", "server1", "server2", sub { file "/etc/passwd", source => "/files/etc/passwd"; file "/etc/passwd", content => template("/files/templates/etc/passwd.tpl"); file "/etc/httpd/conf/httpd.conf", source => "/files/etc/httpd/conf/httpd.conf", on_change => sub { service httpd => "restart"; }; };
This function opens a file for writing (it will truncate the file if it already exists). It returns a Rex::FS::File object on success.
On failure it will die.
my $fh; eval { $fh = file_write("/etc/groups"); }; # catch an error if($@) { print "An error occured. $@.\n"; exit; } # work with the filehandle $fh->write("..."); $fh->close;
This function opens a file for reading. It returns a Rex::FS::File object on success.
my $fh; eval { $fh = read("/etc/groups"); }; # catch an error if($@) { print "An error occured. $@.\n"; exit; } # work with the filehandle my $content = $fh->read_all; $fh->close;
This function returns the complete content of $file_name as a string.
print cat "/etc/passwd";
Delete lines that match $regexp in $file.
task "clean-logs", sub { delete_lines_matching "/var/log/auth.log" => "root"; };
Append $new_line to $file if none in @regexp is found.
task "add-group", sub { append_if_no_such_line "/etc/groups", "mygroup:*:100:myuser1,myuser2"; };
To install Rex, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Rex
CPAN shell
perl -MCPAN -e shell install Rex
For more information on module installation, please visit the detailed CPAN module installation guide.