custom fact syntax

28 views
Skip to first unread message

Mike Reed

unread,
Sep 3, 2014, 5:40:13 PM9/3/14
to puppet...@googlegroups.com
Hello all,

I'm looking to create a custom fact based on the result of a host name prefix.  Some examples of host names are:

home-server-01
work-server-02

I'd like the fact to assign a role to the server based on it's prefix and I'm having a hard time getting this one to work:

require 'facter'
Facter.add('host_role') do
    setcode do
        hostname_array = Facter.value(:hostname).split('-')
        first_in_array = hostname_array.first
        first_in_array.each do |x|
        if x =~ /^(home|work)/
            role = '"#{x}" server'
        else
            role = 'other server'
        end
        role
    end
end

Would anybody have an pointers on why this may not be working.  I feel stupid for asking this but my understanding of ruby is quite basic and google has stopped taking my calls.

Thank you in advance for the help.

Cheers,

Mike

Mike Reed

unread,
Sep 4, 2014, 12:34:10 PM9/4/14
to puppet...@googlegroups.com
Just in case anybody come across this post, I've used the following successfully:


require 'facter'
Facter.add('host_role') do
  setcode do
    location = case Facter.value(:hostname)
      when /home/ then $&
      else 'unknown'
      end
      Q%[This is a #{location} server]
  end
end

I'm sure this can be done more elegantly but for now, it will work.   
Reply all
Reply to author
Forward
0 new messages