Ask Your Question

lookup puppet environment in custom fact

asked 2014-07-24 08:18:48 -0600

banjer gravatar image

I'm writing a custom fact and am trying to lookup the puppet environment. My first thought was to access the environment fact:


but realized "environment" is not actually a fact, but rather it's a puppet config variable. How can I grab the puppet environment in ruby/facter?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2014-07-24 10:09:36 -0600

BikerTux gravatar image

If you add require 'puppet' into the fact then it will be able to pick up the Puppet-based variable for you to assign it to a fact. My custom fact to store the environment, very simple is below.

require 'facter'
require 'puppet'
Facter.add("environment") do
  setcode do
edit flag offensive delete link more


Thanks! Also, this is a ruby variable-handling question, but how can I assign the value Puppet[:environment] to a variable that I can manipulate? e.g. I want to do `env.slice! "foo_"` to remove foo_ from the environment name, but this actually changes the environment in puppet. I'm a rüb noob.

banjer gravatar imagebanjer ( 2014-07-28 09:43:41 -0600 )edit

FYI I'm not actually wanting to change the environment name, but use the sliced environment name for a conditional check later in the code.

banjer gravatar imagebanjer ( 2014-07-28 09:45:12 -0600 )edit

Then you don't want to do a Facter.add, but just use the value of Puppet[:environment]. You can set the value of any variable to that or run the slice function on it directly.

lavaman gravatar imagelavaman ( 2014-08-01 21:49:14 -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: 2014-07-24 08:18:48 -0600

Seen: 814 times

Last updated: Jul 24 '14