Hi Pablo,

The Ruby part of Facts is a little daunting at first but not too difficult, here's an example with some added comments:

#This custom fact simple tests if a file exists and sets the fact true
#or false.
#simple conditional logic to decide whether this Fact gets set or not
if File.exists?("/var/lib/puppet/rebirth_managed_network") and File.exists?("/usr/bin/virt-install")
  #Start of the Fact method, defines the name of the Fact
  Facter.add("rebirth_libvirt_ready") do
#The confine line will ensure this Fact only appears for Linux kernels, would thus exclude Solaris and Windows machines
    confine :kernel => :linux
    #start the block of code that will generate the value of the Fact
    setcode do
      #Since this is a simple boolean Fact, can just call 'true'
      true
    end
  end
end

This blog post is helpful:

http://ppadron.blog.br/2009/09/25/facter-puppet-writing-custom-facts-to-manage-plesk-servers/

When developing facts remember that you can set FACTERLIB to the directory that contains the Fact you're writing and run 'facter -p' to test it.

On 23/03/12 10:42, Pablo Fernandez wrote:

Dear all,

This is a continuation of another thread, but I think the question diverged enough to create a new one.

I have a hiera hierarchy like this:

:hierarchy:

- %{fqdn}

- %{secundary_group}

- %{primary_group}

- %{productname}

- all

And I need to define the secondary/primary groups as facts, on the nodes. Gary has suggested me to use plugins, that they will provide the facts before puppet runs... but I was thinking: for plugins to give facts, taken directly from within the puppet code, puppet needs to run first, doesn't it?

So, I guess I could create a module and a ruby script:

- mygroups/lib/facter/addgroup.rb

And write some code in Ruby to call Facter.add(:primary_group).

My problem is (besides the fact that I know nothing about Ruby)... how do I insert the values for primary_group and secondary_group inside that function, within a simple puppet run?


Technically you're not inserting them inside a Puppet run, it's done as part of Facter, which runs before Puppet gets to do any real work.

I'm not exactly sure where you get your primary and secondary groups from, are they user defined or is there something on your servers that describes what a server's group is? If there is, then a Fact is the best way to get this information into Puppet. This could be a config file (like /etc/facts.d/groups below), maybe something in your server's hostname or the packages installed on the server?

If however they are completely arbitrary groups that are defined inside Puppet itself then the hiera-puppet module might help, as this allows you to pull variables out of specifics class. There's been some recent posts on the list about how to use hiera-puppet.

I guess I could use a file with the definitions, like /etc/facts.d/groups... but that would require two puppet runs: one to create the file, and the second that loads the facts.

I was thinking of a possible alternative... a module "mygroups" with sub-classes, that I import from the nodes:

node 'blabla' { include mygroups::green }

And then in the module, make a mygroups/lib/facter/green.rb that does Facter.add(:primary_group) = 'green' (or however you do it with Ruby). But how do I make sure the module green.rb is loaded, but not the other modules that will be there too?


Note that *all* Custom Facts are synced to every agent regardless of what module they appear in, so this approach will not work.

Or maybe I could use parameters, that the plugin will recognize somehow?

I hope the questions are clear enough, because this is clearly not clean in my head.

Thanks!
Pablo

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To post to this group, send email to puppet-users@googlegroups.com.
To unsubscribe from this group, send email to puppet-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.


--
Luke Bigum

Information Systems
Ph: +44 (0) 20 3192 2520
luke.bi...@lmax.com | http://www.lmax.com
LMAX, Yellow Building, 1A Nicholas Road, London W11 4AN



The information in this e-mail and any attachment is confidential and is 
intended only for the named recipient(s). The e-mail may not be disclosed or 
used by any person other than the addressee, nor may it be copied in any way. 
If you are not a named recipient please notify the sender immediately and 
delete any copies of this message. Any unauthorized copying, disclosure or 
distribution of the material in this e-mail is strictly forbidden. Any view or 
opinions presented are solely those of the author and do not necessarily 
represent those of the company.

--
You received this message because you are subscribed to the Google Groups "Puppet 
Users" group.
To post to this group, send email to puppet-users@googlegroups.com.
To unsubscribe from this group, send email to 
puppet-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/puppet-users?hl=en.

Reply via email to