Get subdirectories by fact

80 views
Skip to first unread message

Daniel K.

unread,
Feb 20, 2014, 10:21:02 AM2/20/14
to puppet...@googlegroups.com
Hello everyone,

i am trying to write my first custom fact but i don't know how. I didn't find any good instructions or list of functions i can use.

What i want to do is the following:

On our machines we have the directory "/work", There is a list of all possible subdirectories, but no machine has all of them.
Depending on the existence of the subdirectories i have to create files. Now all i need is a fact that gives me all subdirectories of "/work" but only depth=1.

e.g.
/work
   |_ Sub_a
   |_ Sub_b
        |_ Sub_b_c

I would only need "Sub_a" and "Sub_b" returned by the fact.

Does anyone know how to do it?
   
Thanks Daniel

jcbollinger

unread,
Feb 21, 2014, 11:44:06 AM2/21/14
to puppet...@googlegroups.com


On Thursday, February 20, 2014 9:21:02 AM UTC-6, Daniel K. wrote:
Hello everyone,

i am trying to write my first custom fact but i don't know how. I didn't find any good instructions or list of functions i can use.


Well, the first place to look for docs would be among PL's online facter docs: http://docs.puppetlabs.com/guides/custom_facts.html.
 

What i want to do is the following:

On our machines we have the directory "/work", There is a list of all possible subdirectories, but no machine has all of them.
Depending on the existence of the subdirectories i have to create files. Now all i need is a fact that gives me all subdirectories of "/work" but only depth=1.

e.g.
/work
   |_ Sub_a
   |_ Sub_b
        |_ Sub_b_c

I would only need "Sub_a" and "Sub_b" returned by the fact.

Does anyone know how to do it?



This shouldn't be too hard.  Supposing you do it as a Ruby plugin, the Ruby code might look something like this:

work_dirs.rb:
=========

Facter.add('work_dirs') do
  setcode do
    begin
      Dir.entries('/work').select {|entry| File.directory?(entry) }.join(',')
    rescue SystemCallError => not_a_directory
      ''     
    end
  end
end


The fact value for your example would be 'Sub_a,Sub_b', which you would need to split inside Puppet if you need them in array form.  (Puppet and Facter do not currently support arrary-valued facts.)


John

Reply all
Reply to author
Forward
0 new messages