Ask Your Question
0

lookup puppet environment in custom fact

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

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:

Facter.value(:environment)

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
2

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

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
    Puppet[:environment]
  end
end
edit flag offensive delete link more

Comments

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

2 followers

Stats

Asked: 2014-07-24 08:18:48 -0500

Seen: 580 times

Last updated: Jul 24 '14