You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/docs/guides/using_modules_and_templates.md
+47-17Lines changed: 47 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,11 +2,41 @@
2
2
title: Using Modules and Templates
3
3
---
4
4
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.
10
40
11
41
rexify Service::NTP --create-module
12
42
@@ -28,23 +58,23 @@ This file is a normal Perl module. The only special thing is the filename, but d
28
58
```perl
29
59
package Service::NTP;
30
60
use Rex -feature => ['1.3'];
31
-
61
+
32
62
task prepare => sub {
33
63
my $service_name = case operating_system, {
34
64
Debian => "ntp",
35
65
default => "ntpd",
36
66
};
37
67
pkg "ntp", ensure => "present";
38
-
68
+
39
69
file "/etc/ntp.conf",
40
70
source => "files/etc/ntp.conf",
41
71
on_change => sub {
42
72
service $service_name => "restart";
43
73
};
44
-
74
+
45
75
service $service_name, ensure => "started";
46
76
};
47
-
77
+
48
78
1;
49
79
```
50
80
@@ -88,31 +118,31 @@ But if you want to change a parameter in your ntp.conf file you have to edit 4 f
0 commit comments