Ask Your Question
3

What is the preferred method for executing an arbitrary command for a custom fact?

asked 2012-12-17 16:36:18 -0500

gh gravatar image

updated 2012-12-17 16:36:55 -0500

What is the preferred method for executing an arbitrary command for a custom fact?

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
9

answered 2012-12-17 17:44:04 -0500

haus gravatar image

updated 2012-12-18 17:01:52 -0500

Most of the facts in core Facter that execute arbitrary commands on the system use Facter::Util::Resolution.exec with the command as an argument. The output of that command is then captured or parsed before used in determining the end fact.

For example, the memtotal fact uses the following code:

memtotal = Facter::Util::Resolution.exec("sysctl hw.physmem | cut -d'=' -f2")

And hostname uses the following code in one case:

Facter::Util::Resolution.exec('hostname')

exec searches the path for the command, so if the binary is not in the current path, it must be fully qualified.

Double Edit ... (more)

edit flag offensive delete link more

Comments

exec does only capture stdout and stderr will show up in your terminal unfiltered.

Stefan gravatar imageStefan ( 2012-12-18 12:35:41 -0500 )edit

What is the path environment here? How can it be adjusted?

zleslie gravatar imagezleslie ( 2014-02-12 23:24:23 -0500 )edit

It is simply the $PATH environment variable (and at least /sbin and /usr/sbin, so even if you run facter as an unprivileged user with no /sbin in path, it ...(more)

Stefan gravatar imageStefan ( 2014-02-13 12:30:28 -0500 )edit
1

I don't' get this at all: Why is this here, whereas the Facter docs don't clearly mention Facter::Util::Resolution.exec() at all? I mean docs here: http://docs.puppetlabs.com/facter/2.4/ . The docs there mention Facter::Core::Execution.exec(). Please clarify this discrepancy.

JDS29 gravatar imageJDS29 ( 2016-02-10 14:21:39 -0500 )edit
4

answered 2012-12-18 12:30:29 -0500

Stefan gravatar image

If your command already provides the final value for your fact, you can pass the commandline as a string to setcode (instead of providing a block). The command is then run via Facter::Util::Resolution.exec (btw: If facter cannot find the command, Facter::Util::Resolution.exec will not raise an error and only return nil. So your fact does not show up when the command is not present on your target machine.

Facter.add(:id) do
  setcode "whoami"
end

If you have to parse or manipulate the output first to get the final fact value use Facter::Util::Resolution ... (more)

edit flag offensive delete link more
0

answered 2012-12-18 13:52:39 -0500

Hi, all.

I'd wondering about advantage of Facter::Util::Resolution.exec over %x{} ruby sentence.

edit flag offensive delete link more

Comments

1) runs the command with a C locale for predictable output 2) Makes sure the command is present before running is 3) Will probably not rely on the PATH environment ...(more)

Stefan gravatar imageStefan ( 2012-12-18 17:18:07 -0500 )edit

thanks stefan.

eduardo gravatar imageeduardo ( 2012-12-18 18:21:28 -0500 )edit

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: 2012-12-17 16:36:18 -0500

Seen: 4,733 times

Last updated: Dec 18 '12