Facter - custom fact regex

593 views
Skip to first unread message

Mike Reed

unread,
Jan 12, 2016, 6:42:45 PM1/12/16
to Puppet Users
Hello all,

I'm having some trouble with a custom fact and I was hoping somebody could tell me what I'm doing wrong.  

Here is an example of the code:

require 'facter'
Facter.add('network') do
  setcode do
    hostname = Facter.value(:hostname)
    ipaddress = Facter.value(:ipaddress)
    case 
      when ipaddres.match(/^(\d[10]\.)(\d[10]\.)([1][2][7]\.)([1-9][1-9][1-9])?/)
        network = 'net1'  
      when hostname.match(/^nettwo/)
        network = 'net2'
      when hostname.match(/^netthree/)
        network = 'net3'
      when hostname.match(/^netfour/)
        network = 'net4'
      else
        network = 'nonet'
      end
    network
    end
end

I've gotten myself into a pickle in that most hosts have a hostname prefix to designate what network they are on.  In this case, I don't have a prefix but the physical ip is different and my thoughts were to regex match the ip address and bingo...but no luck.

Puppet runs and pluginsync does pull down the fact but I never get the value of network back.   No errors are displayed or logged when running 'facter -p' but again, I never get the value of network back.  I've done quite a bit of reading and can't figure out why this one doesn't work.  

Does anybody perhaps have any suggestions as to how I might accomplish this?

Thank you in advance for your assistance.

Cheers,

Mike

Hristo Mohamed

unread,
Jan 13, 2016, 6:26:18 AM1/13/16
to puppet...@googlegroups.com
Try :
setcode { network }

From puppett docs:

  1. A call to Facter.add('fact_name'), which determines the name of the fact
  2. A setcode statement for simple resolutions, which is evaluated to determine the fact’s value.

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/cbf9ea68-fffd-4e14-a467-486d8832bc0e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jcbollinger

unread,
Jan 13, 2016, 9:31:20 AM1/13/16
to Puppet Users
You misspell "ipaddress" in the first "when" clause.  I wouldn't have expected that to cause the fact to be silently ignored, and maybe it doesn't, but you should fix it regardless.

Otherwise, I don't see anything inherently wrong with your fact code.  If after you fix the misspelling, the fact is not submitted to the master, and 'facter -p' neither prints it nor emits any error message, then my first guess would be that it has not properly been synced, your assertion to the contrary notwithstanding.  In that case, look into where the synced file actually went (if anywhere).  If it is indeed in a subdirectory "facter" of some directory in the Ruby load path, then verify that the file and every directory in the path to it is accessible to puppet and facter.  Even with the agent running with privilege, as normally it must do, it is possible for it to be denied access to some files and directories.


John

Thomas Müller

unread,
Jan 14, 2016, 1:23:06 PM1/14/16
to Puppet Users
IMHO the fact "network" is a default fact from facter >= 3. i would change the name.

Mike Reed

unread,
Jan 14, 2016, 4:40:45 PM1/14/16
to Puppet Users
Hey Hristo,

This did not work for me but I wanted to thank you for the info.  It turns out the fact wasn't being sync'd correctly to the client and therefore not all my changes were being reflected locally.

Thank you again for the info. 

Cheers,

Mike.

Mike Reed

unread,
Jan 14, 2016, 4:42:05 PM1/14/16
to Puppet Users
Thank you for the info John.  I did fix the typo and it looks like the fact wasn't sync'd correctly to the client.  It's all fixed up now and working as expected.

You've always been a great help and I very much appreciate your time.

Let me know where to send the six pack.

Cheers,

Mike

Mike Reed

unread,
Jan 14, 2016, 4:42:37 PM1/14/16
to Puppet Users
That is a great idea and I'll get that changed to something else.

Cheers,

Mike
Reply all
Reply to author
Forward
0 new messages