Custom Facts

636 views
Skip to first unread message

Yaniv Fine

unread,
Aug 6, 2012, 8:49:40 AM8/6/12
to puppet...@googlegroups.com
Hi experts .

i would like to write a custom fact the will  add a fact to a machine yaml file
i am a file /etc/file.conf
inside this file a have many lines like
value1 = test1
value2 = test2

i want to create a custom rb file that will grep test2 from  value2  value from /etc/file.conf   , take the test2 and add it to a fact

how can i achieve this ?
thank you 




Dan White

unread,
Aug 6, 2012, 9:20:00 AM8/6/12
to puppet...@googlegroups.com
I think you are trying to re-invent a wheel that is already in use.

Consider:
http://puppetlabs.com/blog/module-of-the-week-puppetlabsstdlib-puppetlabs-standard-library-part-3/

facter-dot-d; a simple way of pulling facts from external sources.

From the docs:

A simple little framework to get facts from external sources. You can create files in /etc/facter/facts.d which can be text, yaml, json or executables. In the case of executables it supports caching so you only need to run your expensive scripts every now and then.

“Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us.”
Bill Waterson (Calvin & Hobbes)
> --
> You received this message because you are subscribed to the Google Groups "Puppet Users" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/Zqakzj9PoBMJ.
> To post to this group, send email to puppet...@googlegroups.com.
> To unsubscribe from this group, send email to puppet-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
>

Yaniv Fine

unread,
Aug 6, 2012, 9:51:09 AM8/6/12
to puppet...@googlegroups.com

Can this be file in other path ?

Dan White

unread,
Aug 6, 2012, 10:06:00 AM8/6/12
to puppet...@googlegroups.com
Probably not

Dan White

unread,
Aug 6, 2012, 11:59:36 AM8/6/12
to puppet...@googlegroups.com
Let me re-answer that:

Using the stdlib::facter-dot-d, I expect you are restricted to /etc/facts.d

However, if you look at the examples here ( http://puppetlabs.com/blog/facter-part-1-facter-101/ ), you can create facts that reference files anywhere in the system. The example shows using a file at /etc/system_role to populate a fact.

Looking at your original question again, it might be possible to create a Ruby script to do what you want.

Look through the examples in that blog entry and the two that follow it. It's all great stuff.

Yaniv Fine

unread,
Aug 7, 2012, 4:19:26 AM8/7/12
to puppet...@googlegroups.com
Hi Dan .
thank you for your help and pointers .


i need your advice pls .
lets say i have /tmp/yanivf.txt
inside i have a line testing
when i run

require 'facter'
Facter.add("yaniv") do
  setcode do 'cat /tmp/yanivf.txt'
  end
end

and than i run facter i get nothing

when i run
export FACTER_yanivval=`cat /tmp/yanivf.txt`; facter
all works fine .
why facter.add doesnt work ?


thank you

Foster Rilindo

unread,
Aug 7, 2012, 5:49:59 AM8/7/12
to puppet...@googlegroups.com
Since you are directly executing a command, you really want to use the built-it Puppet function here:

require 'facter'
Facter.add("yaniv") do
  setcode do 
       Facter::Util::Resolution.exec("cat /tmp/yanivf.txt")
    end
end

There is probably some newlines that file that may not be immediately apparent. The above function takes care of that, plus path searching as well.

Yaniv Fine

unread,
Aug 7, 2012, 5:59:01 AM8/7/12
to puppet...@googlegroups.com
Thank you for your answer .
any reson why   Facter::Util::Resolution.
exec not working .
i don't see either when running factor on the client or on the puppet server yaml file any record of fact named yaniv ?

llowder

unread,
Aug 7, 2012, 9:27:42 AM8/7/12
to puppet...@googlegroups.com
On Tuesday, August 7, 2012 4:59:01 AM UTC-5, Yaniv Fine wrote:
Thank you for your answer .
any reson why   Facter::Util::Resolution.
exec not working .
i don't see either when running factor on the client or on the puppet server yaml file any record of fact named yaniv ?


Have you distributed the fact to the master and all nodes (using pluginsync)?  See this page for details on that: http://docs.puppetlabs.com/guides/plugins_in_modules.html

Also, when running facter, you need to add the -p switch to see the puppet specific things, and many times you'll need to run it as root.

sudo facter -p

Also, if you put that code into an executable .rb file, does it give you the results you expect?

--Lee

Yaniv Fine

unread,
Aug 7, 2012, 9:45:45 AM8/7/12
to puppet...@googlegroups.com
well its wroking for me now .
newbie question .
if i put the test.rb file inside

modules/common/test/lib/facter/test.rb
is it normal that it runs automatically on all servers without a way for me to control what machine will run it ?

all my modules reside :
modules/common/apache java ......

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/PAn8VJbatFIJ.

jcbollinger

unread,
Aug 8, 2012, 10:16:41 AM8/8/12
to puppet...@googlegroups.com


On Tuesday, August 7, 2012 8:45:45 AM UTC-5, Yaniv Fine wrote:
well its wroking for me now .
newbie question .
if i put the test.rb file inside

modules/common/test/lib/facter/test.rb
is it normal that it runs automatically on all servers without a way for me to control what machine will run it ?



Yes.  Ruby sources placed in lib/facter are expected to contain custom facts.  They will be synced automatically to every client node that has pluginsync enabled, and they will be run via Facter on every node on which they appear, to gather the node's fact values.  They should not contain any code other than that of the custom fact(s) they define.

Within a fact definition, however, you can control whether your code will run on the node via the 'confine' function provided by the custom fact framework.


John

Yaniv Fine

unread,
Aug 8, 2012, 11:06:27 AM8/8/12
to puppet...@googlegroups.com
what is i put the rb file in another dir bellow the module dir .
any way i can control what node will load it ?

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/CXjH8Sz0J50J.

jcbollinger

unread,
Aug 8, 2012, 5:56:49 PM8/8/12
to puppet...@googlegroups.com


On Wednesday, August 8, 2012 10:06:27 AM UTC-5, Yaniv Fine wrote:
what is i put the rb file in another dir bellow the module dir .
any way i can control what node will load it ?

I don't know, but I think it unlikely that you could get some nodes to run it but not others.

What are you trying to accomplish?  There is probably a better way.


John

Yaniv Fine

unread,
Aug 9, 2012, 1:21:53 AM8/9/12
to puppet...@googlegroups.com

In general . I am trying to parse a variable i have in a file in few of my servers and add it as a fact i can than use in a module .
This specific file that reside in few of my servers have looks like value : var .inside the rb file i do facter.add .and add the value to a fact .there is no reson to run the rb file on all of my servers

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/3A2dy5yllEsJ.

Denmat

unread,
Aug 9, 2012, 4:46:07 AM8/9/12
to puppet...@googlegroups.com
As I think has been mentioned, use :confine to limit on which node this fact is listed. All facts are distributed to all nodes.

Cheers

Dan White

unread,
Aug 9, 2012, 7:45:45 AM8/9/12
to puppet...@googlegroups.com

On Aug 9, 2012, at 4:46 AM, Denmat wrote:

> As I think has been mentioned, use :confine to limit on which node this fact is listed. All facts are distributed to all nodes.
>
> Cheers

Would this include "local" facts created with puppetlabs-stdlib:/etc/facter/facts.d ?
Or are you referring to facts that are defined by ruby code on the puppet-master ?

Reply all
Reply to author
Forward
0 new messages