傀儡事实的空值/空值

我怎样才能快速简单地说出一个空/空值对于木偶中的一个事实是可以的?

在组装自定义事实时,我正在做类似以下的事情:

/木偶/生产/模块/主机/ LIB / facter

Facter.add(:hostslocal) do setcode do Facter::Util::Resolution.exec("cat /etc/hosts.local 2> /dev/null") end end 

这个效果很好,除非文件不存在,在这种情况下,在Puppet中使用时会出现类似下面的内容。

 Detail: Could not find value for 'hostslocal' 

我已经能够解决它类似于“如果文件不存在写一行只包含评论”,但似乎kludgy。

在撰写这样的自定义事实时,您可以使用Ruby的强大function。 因此,在执行exec操作之前,请检查相关文件是否存在。 就像是:

 Facter.add(:hostslocal)做的
   setcode做
    如果File.exist?  “/etc/hosts.local”
       Facter :: Util :: Resolution.exec(“cat /etc/hosts.local 2> / dev / null”)
    结束
  结束
结束

当然没有经过testing,但这应该是你想要去的方向。 查看http://docs.puppetlabs.com/guides/custom_facts.html获取有关自定义事实的更多详细信息。

Facter有许多事实,只有在某些情况下才会设定。 在使用它们之前,你应该检查它们是否是未定义的。

 if $::hostslocal != undef { do_your_thing_here } 

如果你真的想要你自定义的事实总是有价值的,你可以做类似的事情

 Facter.add(:hostslocal) do setcode do if File.exist? "/etc/hosts.local" Facter::Util::Resolution.exec("cat /etc/hosts.local 2> /dev/null") else "unknown" end end end