简体   繁体   English

检查文件是否存在人偶模板

[英]Check if a file exists with puppet template

I try to check if a file exists on client who run puppet agent. 我尝试检查运行木偶代理的客户端上是否存在文件。 On my puppet master, I have a template.erb like this : 在我的木偶大师上,我有一个template.erb像这样:

<% if File.exists?('/usr/bin/lwp-request') %>SCRIPTWHITELIST="/usr/bin/lwp-request"<% end %>

This little code in my template is needed to my rkhunter module. 我的rkhunter模块需要模板中的这段小代码。 The result is always false, however the file exists. 结果始终为false,但是该文件存在。

If I add the file on the puppet master, the result is true. 如果将文件添加到人偶主文件上,则结果为true。 So the ruby code seems to be executed on the master. 因此,Ruby代码似乎在主数据库上执行。

How can I check on my template if a file exists on client ? 如果客户端上存在文件,如何检查模板?

Tested on puppet 2.7.5 and 2.8.1. 在人偶2.7.5和2.8.1上测试。

Thanks 谢谢

The only information you have about the node when compiling manifests and templates are Facts that are sent by the node when requesting a catalog. 编译清单和模板时,关于节点的唯一信息是节点在请求目录时发送的事实

If you need additional information from the node, then you need to add a Custom Fact that retrieves the information you need (like whether or not a file exists). 如果您需要节点中的其他信息,则需要添加一个自定义事实 ,以检索所需的信息(例如文件是否存在)。 You can then use the custom fact inside of templates. 然后,您可以在模板内部使用自定义事实。

Within a Puppet module create a custom fact lib/facter/lwp.rb : 在一个Puppet模块中,创建一个自定义事实lib/facter/lwp.rb

Facter.add(:lwp_request_exists) do
  setcode do
    File.exists?('/usr/bin/lwp-request')
  end
end

then within erb template use something like: 然后在erb模板中使用类似以下内容的内容:

<% if $::lwp_request_exists -%>
some code...
<% end -%>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM