🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
### 当文件更新时运行命令 当某个特定的文件更新后 Puppet 就该采取一些行动,这是一个非常常见的模式。 例如,在 rsync 配置片段的例子中,一旦修改了某个片段文件,就会调用 exec 资源更新主配置文件 rsyncd.conf。 每次运行 Puppet,exec 资源都会被运行,除非指定了如下参数中的一个: * creates * onlyif * unless * refreshonly => true refreshonly 参数的意思是:仅当从其他资源(例如一个文件资源)获得一个 notify 才执行 exec 资源。 #### 准备工作 安装 nginx 包(实际上,我们只需要配置文件,但这是获得它的最简单方式): ``` # apt-get install nginx ``` #### 操作步骤 1. 创建一个 nginx 模块,其目录结构如下: ``` # mkdir /etc/puppet/modules/nginx # mkdir /etc/puppet/modules/nginx/files # mkdir /etc/puppet/modules/nginx/manifests ``` 2. 使用如下内容创建 /etc/puppet/modules/nginx/manifests/nginx.pp 文件: ``` class nginx { package { "nginx": ensure => installed } service { "nginx": enable => true, ensure => running, } exec { "reload nginx": command => "/usr/sbin/service nginx reload", require => Package["nginx"], refreshonly => true, } file { "/etc/nginx/nginx.conf": source => "puppet:///modules/nginx/nginx.conf", notify => Exec["reload nginx"], require => Package["nginx"], } } ``` 3. 复制系统中的 nginx.conf 文件到模块的相应目录: ``` cp /etc/nginx/nginx.conf /etc/puppet/modules/nginx/files ``` 4. 添加如下代码到你的配置清单: ``` include nginx ``` 5. 对复制到 Puppet 中的 nginx.conf 文件做个小改动,以便进行后续的测试: ``` # echo \# >>/etc/puppet/modules/nginx/files/nginx.conf ``` 6. 运行 Puppet: ``` # puppet agent --test info: Retrieving plugin info: Caching catalog for cookbook.bitfieldconsulting.com info: Applying configuration version '1303745502' --- /etc/nginx/nginx.conf 2010-02-15 00:16:47.000000000 -0700 +++ /tmp/puppet-file20110425-31239-158xcst-0 2011-04-25 09:39:49.586322042 -0600 @@ -48,3 +48,4 @@ # proxy on; # } # } +# info: FileBucket adding /etc/nginx/nginx.conf as {md5}7bf139588b5e cd5956f986c9c1442d44 info: /Stage[main]/Nginx/File[/etc/nginx/nginx.conf]: Filebucketed /etc/nginx/nginx.conf to puppet with sum 7bf139588b5ecd5956f986c9c1442d44 notice: /Stage[main]/Nginx/File[/etc/nginx/nginx.conf]/content: content changed '{md5}7bf139588b5ecd5956f986c9c1442d44' to '{md5} d28d08925174c3f6917a78797c4cd3cc' info: /Stage[main]/Nginx/File[/etc/nginx/nginx.conf]: Scheduling refresh of Exec[reload nginx] notice: /Stage[main]/Nginx/Exec[reload nginx]: Triggered 'refresh' from 1 events notice: Finished catalog run in 1.69 seconds ``` #### 工作原理 对于大多数服务来说,你应该简单地定义一个服务资源,它从配置文件获得 notify。 这会使 Puppet 重启服务,从而应用改变之后的更新。 然而,nginx 有时不能正确的重新启动,尤其是当使用 Puppet 重新启动时, 所以我为某个站点炮制了一个让 Puppet 运行 /etc/init.d/nginx reload 的补救措施, 用它替代服务的重启。下面阐述它是如何工作的。 将 exec 资源中的 refreshonly 参数设置成 true: ``` exec { "reload nginx": command => "/usr/sbin/service nginx reload", require => Package["nginx"], refreshonly => true, } ``` 所以,仅当它获得通知才会运行。 如果配置文件发生改变,就提供所需的通知(notify): ``` file { "/etc/nginx/nginx.conf": source => "puppet:///modules/nginx/nginx.conf", notify => Exec["reload nginx"], } ``` 每当 Puppet 更新了这个配置文件,它就会运行 exec, 这将会调用如下命令重新加载修改后的配置文件: ``` /usr/sbin/service nginx reload ``` 如果服务支持 reload 命令参数,就会在不中断服务的情况下为 daemon 发出一个重新读取配置文件的进程信号。 实际上,对于本例,更好的方法是为 nginx 服务定义如下的 restart 命令: ``` service { "nginx": restart => "/etc/init.d/nginx reload", } ``` 但是我想要和你分享一些我写的用于展示 notify -> Exec 技术的实际代码, 并且那时,我还不知道 restart 而且它也不存在。 不过作为一种通用的模式,当更新文件后需要采取某种行动时,你会发现它有用。 #### 更多用法 每当遇到资源更新就要采取某些行动的情况,你就可以使用这个类似的模式。可能的用途包括: * 触发服务重新加载配置文件 * 运行语法检查,然后再重新启动服务 * 连接 config 片段 * 运行测试 * 链接 exec 资源 如果当一个文件(假设名为 somefile)更新后需要执行多个命令,那么在每个命令的 exec 资源声明中都使用 subscribe => File[somfile], (即一旦检测到 somefile 文件的变化就重新执行 exec 资源)会更简单, 而不是在 file 资源中使用 notify 执行命令。 效果是一样的。 > ![注记](https://box.kancloud.cn/2016-05-12_5733eec619643.png) 译者注 > 你也可以在 service 资源定义中使用 subscribe, 例如在一个 Redhat 风格的系统中,你可以使用如下的 service 声明: > ``` > service { "nginx": > enable => true, > ensure => running, > restart => "/etc/init.d/nginx reload", > subscribe => [ File["/etc/nginx/nginx.conf"], > File["/etc/sysconfig/nginx"] ] > } > > ``` > 在上例中,使用数组为 subscribe 指定了两个要检测的文件, 也就是说,一旦检测到两个文件中的任何一个发生变化就重启服务。