Can I define a variable/parameter to a facter?

Hi

I've created a facter where it lists users by a specific group. Now I'm interested in different groups to look into, how can I use a variable/parameter and use them then in the fact?

Facter.add(:users_by_group) do
setcode do
Facter::Util::Resolution.exec("/usr/bin/getent group mydesiredgroup | sed s/':'/' '/g | awk '{print $4}'") end end  I'd like to use a variable for 'mydesiredgroup' edit retag close merge delete 2 Answers Sort by » oldest newest most voted I have never tried this, but I would use environment variable to do it. On the client machine: export CUR_GROUP=mydesiredgroup In your fact declaration: Facter.add(:users_by_group) do setcode do Facter::Util::Resolution.exec("/usr/bin/getent group #{ENV['CUR_GROUP']} | sed s/':'/' '/g | awk '{print$4}'")
end
end


I have not tested it, you should give it try. Hope this helps.

more

Facter does not really take arguments so I'd just create a fact for each group on the system.

Something like the following should work (this also does not fork additional processes and uses the native ruby calls):

# lib/facter/groups.rb
require 'etc'

groups = []

Etc.group do |group|
groups << group.name
setcode do
group.mem.join(',')
end
end
end

setcode do
groups.join(",")
end
end


You should then be able to use group_mydesiredgroup_member in your manifests.

Note that the above fact works on facter < 2.0. In facter 2.0 this should probably be written as a structured fact (so the fact can return an array instead of a stringified list). See writing structured facts if you already run facter 2.x

more