Skip to content

Commit 19cd534

Browse files
committed
improve intro with more context for the guide
1 parent 0817785 commit 19cd534

File tree

1 file changed

+47
-17
lines changed

1 file changed

+47
-17
lines changed

content/docs/guides/using_modules_and_templates.md

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,41 @@
22
title: Using Modules and Templates
33
---
44

5-
Uploading hardcoded configuration files to your servers often isn't enough. Sometimes you must add values dynamically inside a file. For example if you want to bind a service to a special network interface or to configure different database servers for your application for your different environments. This can be easily achieved with templates.
6-
7-
In this example you will learn how to build a ntp module that uploads custom ntp.conf files for your test-, pre-prod-, and production environment.
8-
9-
First, we create a new module. For this guide you need at least Rex 0.41 (because of the --create-module command and the case keyword.) Execute the following command in the same directory where your Rexfile lives.
5+
To scale Rex to manage a large array of servers, two important tools will be
6+
invaluable to you: modules and templates.
7+
8+
Software is all about providing tools to manage complexity. And so once your
9+
library of Rex tasks grows beyond a few dozen, you'll definitely want to
10+
use modules to help you manage and organize your tasks. While it's possible to
11+
create separate Rexfiles and place them in their own directories to help keep
12+
your task files manageable, modules offer a much better solution. With modules,
13+
you'll still have just one Rexfile, but your tasks will be grouped and
14+
categorized nicely into their own manageable files. More importantly, modules
15+
are able to call tasks from other modules and the main, central Rexfile.
16+
17+
Templates also help you manage complexity by making it easier to generate and
18+
modify configuration files to change the behavior of the software on your
19+
machine(s). For example you may need to change the configuration file for a
20+
service to bind it to a special network interface or you may wish to configure
21+
different hostnames for your database servers depending on which environment
22+
your application is running in (testing, pre-production, production, etc.).
23+
This can all be easily achieved with templates.
24+
25+
The following guide shows you both of these more advanced techniques with a
26+
real-world example that builds a module for managing an ntp service and shows
27+
you how to use templates for automating the generation of the `ntp.conf` files
28+
for a "test," "pre-prod," and "production" environment.
29+
30+
For those not familiar with ntp, it's a software package that runs a service
31+
for synchronizing the clocks of computer called the ["Network Time
32+
Protocol."](http://www.ntp.org) But familiarity with the ntp software is not a
33+
prerequisite to learn how to use Rex's modules and templates features.
34+
35+
## Creating a Module
36+
37+
First, we create a new module. For this guide you need at least Rex 0.41
38+
(because of the --create-module command and the case keyword.) Execute the
39+
following command in the same directory where your Rexfile lives.
1040

1141
rexify Service::NTP --create-module
1242

@@ -28,23 +58,23 @@ This file is a normal Perl module. The only special thing is the filename, but d
2858
```perl
2959
package Service::NTP;
3060
use Rex -feature => ['1.3'];
31-
61+
3262
task prepare => sub {
3363
my $service_name = case operating_system, {
3464
Debian => "ntp",
3565
default => "ntpd",
3666
};
3767
pkg "ntp", ensure => "present";
38-
68+
3969
file "/etc/ntp.conf",
4070
source => "files/etc/ntp.conf",
4171
on_change => sub {
4272
service $service_name => "restart";
4373
};
44-
74+
4575
service $service_name, ensure => "started";
4676
};
47-
77+
4878
1;
4979
```
5080

@@ -88,31 +118,31 @@ But if you want to change a parameter in your ntp.conf file you have to edit 4 f
88118
```perl
89119
package Service::NTP;
90120
use Rex -feature => ['1.0'];
91-
121+
92122
task prepare => sub {
93123
my $service_name = case operating_system, {
94124
Debian => "ntp",
95125
default => "ntpd",
96126
};
97-
127+
98128
my $ntp_server = case environment, {
99129
prod => [ "ntp01.company.tld", "ntp02.company.tld" ],
100130
preprod => ["ntp01.preprod.company.tld"],
101131
test => ["ntp01.test.company.tld"],
102132
default => ["ntp01.company.tld"],
103133
};
104-
134+
105135
pkg "ntp", ensure => "present";
106-
136+
107137
file "/etc/ntp.conf",
108138
content => template( "files/etc/ntp.conf", ntp_servers => $ntp_server ),
109139
on_change => sub {
110140
service $service_name => "restart";
111141
};
112-
142+
113143
service $service_name, ensure => "started";
114144
};
115-
145+
116146
1;
117147
```
118148

@@ -223,9 +253,9 @@ To use your module you have to add it to your Rexfile. This can be simply achiev
223253
use Rex -feature => ['1.3'];
224254
user "root";
225255
password "foo";
226-
256+
227257
require Service::NTP;
228-
258+
229259
1;
230260
```
231261

0 commit comments

Comments
 (0)