Ask Your Question

How can I access $vardir inside a custom fact?

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

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

2 Answers

Sort by ยป oldest newest most voted

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

DarylW gravatar image

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

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

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

Hyder gravatar image

updated 2017-03-10 10:03:43 -0600

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 -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

1 follower


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

Seen: 52 times

Last updated: Mar 14 '17