为什么 puppet 中的 exec 资源总是有一个 previous_value 的 notrun?

Why do exec resources in puppet always have a previous_value of notrun?

我有几个 Puppet exec 资源,其中一个有一个 onlyif 节。在报告和 Puppetboard 中,它们总是显示为 changed,因为它们的 previous_value 总是 notrun。这没有任何意义,特别是对于没有 onlyif 的那个(每次都是 运行)。

这是一个错误吗?我该怎么做才能解决这个问题?

编辑:按照 Ina 的建议添加代码

exec { 'make-plugins-executable':
  command => "/usr/bin/chmod a+x ${pluginsdir}/check*",
  require => File['plugins']
}

exec { 'nagios-permissions':
  command => "/usr/bin/chown -R nagios:nagios ${confdir}",
  onlyif  => "/usr/bin/test ! -z \"/usr/bin/find ${confdir} ! -user nagios -o ! -group nagios\""
}

此外,这是日志中的输出:

2018-07-23 11:40:48 +0100 /Stage[main]/Bcc_nagios/Exec[make-plugins-executable]/returns (notice): executed successfully
2018-07-23 11:40:48 +0100 /Stage[main]/Bcc_nagios::Master/Exec[nagios-permissions]/returns (notice): executed successfully
2018-07-23 11:40:58 +0100 Puppet (notice): Applied catalog in 10.09 seconds

他们得到 运行,但他们在 last_run_summary.yaml

中显示为 notrun

您写道,以下 Exec 始终报告为已更改对您来说没有任何意义,显然是因为您希望 onlyif 阻止这种情况发生:

exec { 'nagios-permissions':
  command => "/usr/bin/chown -R nagios:nagios ${confdir}",
  onlyif  => "/usr/bin/test ! -z \"/usr/bin/find ${confdir} ! -user nagios -o ! -group nagios\""
}

但它确实有意义。您在 onlyif 中给出的命令并没有按照您认为的那样执行。下面是它在没有 Puppet 级引用和转义的情况下的显示方式:

/usr/bin/test ! -z "/usr/bin/find /some/directory ! -user nagios -o ! -group nagios"

该命令总是 return 0(真):它只是测试给定的字符串是否为空。没有find命令是运行;测试只是通过检查参数。结果,主command中的chown始终是运行,并报告了

您似乎想要更像这样的东西:

exec { 'nagios-permissions':
  command => "/usr/bin/chown -R nagios:nagios ${confdir}",
  onlyif  => "/bin/sh -c \"'!' -z `/usr/bin/find /some/directory '!' -user nagios -o '!' -group nagios`\""
}

注意三种不同类型的引号(如果包含 \ 转义,则为四种)。还要注意,您需要确保 shell 被调用到 运行 命令,如图所示,因为它依赖于命令替换。

然而,正如在对该问题的评论中所建议的,对于此类工作使用递归 File 资源可能比通过 Exec 处理它更好。我希望仅当一个或多个文件的属性实际发生更改时才报告更改。