使用模板:
一个模板是一个文本文件包含特定变量或者Perl代码
因此这个技术可以生成动态的配置文件。
比如 如果你需要配置apache 只侦听特定的网络设备(比如eth0) 那么模板就是你需要的
默认的模板引擎是一个特殊的Rex模板引擎。
语法有点像php或者erb
但是你可以使用任何模板引擎
比如:
Hello <%= $name %>!
如果$name 包含"World" 这个模板会生成字符串Hello World!
使用模板:
首先你创建它:
[root@node01 my-first-rex-project]# ls -ltr
total 48
-rw-r--r-- 1 root root 20480 Apr 29 2017 rex.tar
-rw-r--r-- 1 root root 1318 Apr 29 2017 Rexfile.bak
-rw-r--r-- 1 root root 0 Apr 29 2017 {parameter1}
-rw-r--r-- 1 root root 0 Apr 29 2017 {parameter2}
-rw-r--r-- 1 root root 0 Apr 29 2017 a1.sh
drwxr-xr-x 2 root root 4096 Apr 29 2017 script
-rw-r--r-- 1 root root 1640 May 3 2017 Rexfile.0523
drwxr-xr-x 4 root root 4096 Jun 15 07:48 lib
-rw-r--r-- 1 root root 305 Jun 15 08:11 ntp.conf
-rw-r--r-- 1 root root 1225 Jun 19 03:33 Rexfile
drwxr-xr-x 2 root root 4096 Jun 19 09:21 files
$ mkdir files
$ vim files/my.cnf.tpl
[mysqld]
datadir = /var/lib/mysql
socket = /var/run/mysqld/mysqld.sock
user = mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links = 0
datadir = /var/lib/mysql
tmpdir = /tmp
skip-external-locking
max_allowed_packet = 64M
thread_stack = 192K
max_connections = <%= exists $conf->{"max_connections"} ? $conf->{"max_connections"} : "1000" %>
max_connect_errors = 1000
table_cache = <%= exists $conf->{"table_cache"} ? $conf->{"table_cache"} : "5000" %>
table_open_cache = <%= exists $conf->{"table_open_cache"} ? $conf->{"table_open_cache"} : "5000" %>
thread_concurrency = 10
然后你可以在你的rexfile中引用它
[root@node01 my-first-rex-project]# rex -H 192.168.137.3 prepare_databases
task "prepare_databases", sub {
file "/tmp/my.cnf",
owner => "mqm",
group => "mqm",
mode => "644",
content => template("files/my.cnf.tpl", conf => {
max_connections => "500",
table_cache => "2500",
});
};