仅当文件存在时才应停止 puppet-service

puppet-service should be stopped only if file exists

根据我的示例中的文档,如果目标资源 require => Exec['checkForFile'] 成功应用,资源 'service x' 将被执行。 exec 命令只有在文件 '/etc/init.d/x' 存在时才会执行。

所以当文件'/etc/init.d/x'不存在时,Exec['checkForFile']中的命令不应该被执行并且 如果未执行目标资源,则应跳过资源 'service x'。

对我来说,资源服务 x 不是 运行 这是正确的,但同时我没有收到任何错误或退出代码 1 来显示 Exec['checkForFile'] 失败运行。这是预期的行为吗?

class test2::testcleanup (
         $target_location,
        )
        {
         service { 'x':
            ensure => stopped,
            restart => "service x restart",
            stop => "service x stop",
            status => "service x status",
            require => Exec['checkForFile'],
            before => [
              File["/etc/init.d/x"],
              File[ "remove_directory"],
            ],
          }

         exec { "checkForFile":
            command => "/bin/true",
            path    =>  ["/usr/bin","/usr/sbin", "/bin"],
            onlyif  => 'test -f /etc/init.d/x',
            logoutput => true, 
          }

        file {'/etc/init.d/x':
             ensure => absent,
        }

        file {'remove_directory':
           ensure => absent,
           path => $target_location,
           recurse => true,
           purge => true,
           force => true,
           }

    }

According to the documentation in my example, the resource 'service x' will be executed if target resource require => Exec['checkForFile'] is successfully applied. And the exec command will execute onlyif the file '/etc/init.d/x' is present.

是的,如果Exec['checkForFile']未成功应用,则不会应用Service['x']

so when the file '/etc/init.d/x' is absent,the command in Exec['checkForFile'] should not be executed

是的,但这与未应用的 Exec 不同。

与任何其他资源一样,Execs 模型目标机器状态,尽管在他们的情况下,该状态对于一个目录 运行 的上下文是本地的。通常它被描述为 Exec 的命令是否为 运行,但最好反过来描述,如 Exec 的命令是否为 是否需要 运行。如果它处于 "needs to be run" 状态,则成功执行 Exec 的命令会将其转换为 "does not need to be run" 状态。

Execunlessonlyifcreates 属性用于确定相应物理(可以这么说)资源的初始状态。如果提供了 none 个,则物理资源最初处于 "needs to be run" 状态。如果提供了其中一个或多个,则它们可能表示 Exec 最初处于 "does not need to be run" 状态。 这就是目标状态。那么,在那种情况下,Exec 已成功应用,而无需 运行 执行其命令。

and the resource 'service x' should be skipped if target resource is not executed.

没有。如果应用 Exec 失败,将跳过 Service['x']。仅当其命令为 运行 并以失败状态退出时才会发生这种情况。

例如,不要在 Exec 中使用 onlyif,而是使用 'test ! -f /etc/init.d/x' 作为其 command

For me, the resouce service x is not run which is correct, but at the same time i am not receiving any error or exit code 1 to show Exec['checkForFile'] failed to run. is this expected behavior?

也许吧。

在您的情况下,Exec 总是成功应用。如果 /etc/init.d/x 存在,则 /bin/true 是 运行(成功)。如果该文件不存在,则 Exec 成功但 运行 宁 /bin/trueService['x'] 将以任何一种方式应用,但如果物理服务已经处于其目标状态(已停止),那么 Puppet 将不会采取任何进一步的行动,并且该资源的任何内容都不会出现在默认日志的输出中等级。打开 --debug 登录代理应该可以更清楚地了解正在发生的事情。


但这一切都是落后的。 Puppet 无意成为一个脚本引擎,而且它作为一个脚本引擎也不能很好地工作。确定机器的适当目标状态所需的任何瞬态机器信息最好以事实的形式传送给 Puppet。然后,您在目录构建期间根据这些事实和任何其他可用数据确定要声明的资源。

此外,最好尽量减少用于确定其目标状态的当前机器状态的数量。相反,更喜欢将目标状态与机器的身份联系起来,并在必要时根据不变特性(OS、硬件架构等)调整细节。尽管此模型可能无法满足所有需求,但通常足以满足需求,并且可以作为出色的基准。