使用模块和模板:


使用模块和模板:

上传硬编码配置文件到服务器通常是不够的.

有时候你必须动态的增加至到一个文件

比如  如果你需要绑定一个服务到一个指定的网络接口或者配置不同的数据库对于不同的应用环境

这个可以通过模板实现

在这个例子你可以学会创建一个ntp 模块,上传自定义的ntp.conf文件用于你的测试 ,

预生产环境和生产环境


首先,我们创建一个新的模块。对于这个指南你需要至少Rex0.41(因为--create-module命令和case关键字)

执行下面的命令在相同的目录 

[root@node01 my-first-rex-project]# rexify Service::NTP --create-module
Creating module Service::NTP...
   mkdir lib/Service/NTP
   Creating template file: __module__.pm
11111111111112222334414
   Creating template file: meta.yml
11111111111112222334414

Your module has been created in lib/Service/NTP.
[root@node01 my-first-rex-project]# 


This will create some new directories and files in your lib directory.

.
├── Rexfile
└── lib
    └── Service
        └── NTP
            ├── __module__.pm
            └── meta.yml
			
meta.yml 文件可以被忽略, 这个文件是重要的只有你需要共享i的模块在Rex模块目录

最重要的文件是 __module__.pm.  打开这个文件使用一个文本编辑器,

这个文件是一个普通的Perl模块。

唯一特别的是文件名,但是一开始不要想太多

package Service::NTP;
use Rex -feature => ['1.3'];

task prepare => sub {
   my $service_name = case operating_system, {
                         Debian  => "ntp",
                         default => "ntpd",
                      };
   pkg "ntp",
     ensure => "present";

   file "/etc/ntp.conf",
      source    => "files/etc/ntp.conf",
      on_change => sub {
         service $service_name => "restart";
      };

   service $service_name,
     ensure => "started";
};

1;


首先这个模块检查如果OS是一个debian(或者ubuntu) 设置服务名为ntp 否则为ntpd


然后它安装ntp包和上传配置文件。

注意 on_change 这里,这个会重启ntp 服务如果文件改变

最后Rex 验证服务会启动在系统启动时, 现在创建基本的ntp.conf文件 


创建目录 lib/Service/NTP/files/etc 防止ntp.conf文件

如果你需要分配不同的ntp.conf 文件  你可以增加不同的ntp.conf files到不同的目录 

Rex 会然后 选择-E $env cli 参数 来绝对实用哪个文件

Rex 首先尝试找到文件名为ntp.conf.


使用模块:

使用你的模块 ,你需要增加它到你的Rexfile.

use Rex -feature => ['1.3'];
user "root";
password "foo";

require Service::NTP;

1;





[root@node01 NTP]# cat __module__.pm
package Service::NTP;
use Rex -feature => ['1.3'];

task prepare => sub {
   my $service_name = case operating_system, {
                         Debian  => "ntp",
                         default => "ntpd",
                      };
   pkg "ntp",
     ensure => "present";

   file "/etc/ntp.conf",
      source    => "files/etc/ntp.conf",
      on_change => sub {
         service $service_name => "restart";
      };

   service $service_name,
     ensure => "started";
};

1;





[root@node01 my-first-rex-project]# rex -H 192.168.137.2  Service:NTP:prepare
[2019-06-18 08:14:55] INFO - Running task Service:NTP:prepare on 192.168.137.2
[2019-06-18 08:14:57] INFO - Installing ntp.
[2019-06-18 08:15:42] INFO - Service ntpd restarted.
[2019-06-18 08:15:43] INFO - All tasks successful on all hosts
[root@node01 my-first-rex-project]# 


[root@node01 my-first-rex-project]# chkconfig --list | grep ntp
ntpd           	0:off	1:off	2:on	3:on	4:on	5:on	6:off
ntpdate        	0:off	1:off	2:off	3:off	4:off	5:off	6:off
[root@node01 my-first-rex-project]# chkconfig ntpd off
[root@node01 my-first-rex-project]# chkconfig --list | grep ntp
ntpd           	0:off	1:off	2:off	3:off	4:off	5:off	6:off
ntpdate        	0:off	1:off	2:off	3:off	4:off	5:off	6:off
[root@node01 my-first-rex-project]# rex -H 192.168.137.2  Service:NTP:prepare
[2019-06-18 08:17:18] INFO - Running task Service:NTP:prepare on 192.168.137.2
[2019-06-18 08:17:21] INFO - All tasks successful on all hosts
[root@node01 my-first-rex-project]# chkconfig --list | grep ntp
ntpd           	0:off	1:off	2:on	3:on	4:on	5:on	6:off
ntpdate        	0:off	1:off	2:off	3:off	4:off	5:off	6:off
[root@node01 my-first-rex-project]# 




原文地址:https://www.cnblogs.com/hzcya1995/p/13348710.html