Ask Your Question

How can I access $vardir inside a custom fact?

asked 2017-03-10 05:54:52 -0500

larsen gravatar image


I have a working custom fact (that needs to call a helper script), but I want to get rid of that hard-coded path. It probably should be something like "$vardir/lib/facter", but I couldn't find the correct way to access "$vardir"...

Facter.add("raid") {
    value=`/bin/bash /var/lib/puppet/lib/facter/`
    setcode {

How can I do this?

edit retag flag offensive close merge delete


there are many ways to do it. but below are the basic ones which I answered.

Hyder gravatar imageHyder ( 2017-03-10 10:05:41 -0500 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2017-03-14 08:59:49 -0500

DarylW gravatar image

updated 2017-03-14 13:48:59 -0500

I can think of two possible ways to get the information you need.

You could make a switch/case based on the $::puppetversion fact, either inside of your custom fact (since you can look it up via Facter.value('puppetversion') ) and hard code the appropriate location based on the puppet version.


You could call puppet config print vardir from your fact to see the applied configuration value. It won't be 100% accurate (if you called puppet manually overriding something with an argument, that argument wouldn't get passed by that fact) but it may be close enough for your needs.

edit flag offensive delete link more


Thx, looks good! I am now using (in case anybody else needs this): setcode {`puppet config print vardir`.chomp + "/lib/facter"}

larsen gravatar imagelarsen ( 2017-03-14 09:51:35 -0500 )edit

I would probably call `Facter::Util::Resolution.exec(puppet config print vardir)` instead of just calling a subshell, that gives you the option, later, if you write a unit test for your code, to stub out the return value from the call.

DarylW gravatar imageDarylW ( 2017-03-14 13:51:20 -0500 )edit

answered 2017-03-10 09:43:55 -0500

Hyder gravatar image

updated 2017-03-10 10:03:43 -0500

one way you can do is declare it once on the top

  $vardir = /var/lib/puppet

  Facter.add("raid") {
        value=`/bin/bash $vardir/lib/facter/`
        setcode {

use it wherever and whenever you need, you can also use it as many times as you can.

The Second way is write a class which contains all the variables you want to use for example

class myvariables {
    $vardir = /var/lib/puppet,
    $modulepath = /etc/puppet/modules

To access these you can do

edit flag offensive delete link more


Thanks, but this is still hard-coded and wouldn't work on my servers that are running different puppet versions. I'm working on unifying these. Though, it would be useful, if I could use the same code in the meantime.

larsen gravatar imagelarsen ( 2017-03-13 03:18:27 -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

1 follower


Asked: 2017-03-10 05:54:52 -0500

Seen: 20 times

Last updated: Mar 14