Ask Your Question

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

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

gh gravatar image

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

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

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

haus gravatar image

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

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:


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


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

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

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

zleslie gravatar imagezleslie ( 2014-02-12 23:24:23 -0600 )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 -0600 )edit

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: . The docs there mention Facter::Core::Execution.exec(). Please clarify this discrepancy.

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

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

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"

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

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

Hi, all.

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

edit flag offensive delete link more


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 -0600 )edit

thanks stefan.

eduardo gravatar imageeduardo ( 2012-12-18 18:21:28 -0600 )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


Asked: 2012-12-17 16:36:18 -0600

Seen: 5,896 times

Last updated: Dec 18 '12