简体   繁体   中英

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 :

<% 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. The result is always false, however the file exists.

If I add the file on the puppet master, the result is true. So the ruby code seems to be executed on the master.

How can I check on my template if a file exists on client ?

Tested on puppet 2.7.5 and 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 :

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

then within erb template use something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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