Ask Your Question
1

Can I define a variable/parameter to a facter?

asked 2014-05-09 14:24:58 -0600

Dionysius gravatar image

updated 2014-05-09 14:26:00 -0600

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 flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2014-05-10 07:19:31 -0600

doc75 gravatar image

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.

edit flag offensive delete link more
0

answered 2014-05-11 16:13:23 -0600

Stefan gravatar image

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
  Facter.add("group_#{group.name}_member".intern) do
    setcode do
      group.mem.join(',')
    end
  end
end

Facter.add(:groups) do
  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

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2014-05-09 14:24:58 -0600

Seen: 1,373 times

Last updated: May 11 '14