18

How do I access an environment variable (from the puppet daemon's environment) in a puppet manifest?

joeforker
  • 2,399
  • 4
  • 26
  • 35

5 Answers5

8

I think we need more informations on what you are trying to achieve... Facter exposes by default FACTER_ environment variables :

https://docs.puppetlabs.com/guides/faq.html#can-i-access-environment-variables-with-facter

 $ FACTER_FOO="bar" 
 $ export FACTER_FOO
 $ facter | grep 'foo'
   foo => bar

But for $PATH or $USER... Why not tells puppet to use a given path or a user (for an exec ?) explicitly ?

jnrg
  • 269
  • 1
  • 6
  • It's very ordinary to control an interpreter with environment variables, for a variety of reasons... especially if you think about sometimes running puppet as an interpreter (in the #! line?) instead of a daemon... – joeforker Mar 31 '10 at 19:29
  • Excellent answer. I wasted a lot of time not noticing that all Facter variables have lowercase names even if the environment variable has an uppercase name. Everybody pay close attention to the example jnrg gives. – Russell Silva Jan 15 '13 at 17:00
  • 2
    there is broken link – kenorb Jun 09 '14 at 12:10
  • According to the new doc: https://puppet.com/docs/puppet/latest/core_facts.html the path and identity are returning the data the OP requested. – рüффп May 01 '20 at 11:34
7

You'd need to use a server side function for this if you want the puppetmaster's environment. Since facter gets you client facts.

$RUBYLIB/puppet/parser/functions/env.rb:

module Puppet::Parser::Functions
  newfunction(:env) do |args|
    variable = args[0]
    ENV[variable]
  end
end

Use it in your manifests like:

$blah = env("PATH")
AndrewF
  • 223
  • 3
  • 6
6

From what I can tell Puppet runs without any Bash environment variables. It seems to get all its environment from Facter. There is a script here to import your regular envvars as Facter envvars.

  • 1
    The link doesn't work anymore. Could you please update the answer accordingly? – N A Mar 10 '19 at 14:56
1

In Puppet enterprise 2.5.1 you can access it via /etc/env.

Also check whether you have the correct environment defined in your /etc/puppetlabs/puppet/puppet.conf -- it should look something like this:

[production]
  modulepath = /etc/puppetlabs/puppet/environments/production/modules:/opt/puppet/share/puppet/modules
  manifest   = /etc/puppetlabs/puppet/environments/production/manifests/site.pp
mgorven
  • 30,615
  • 7
  • 79
  • 122
Nalinda
  • 11
  • 1
-1

The answer is a bit disappointing: You can't (unless you enhance Puppet or Facter). I recently wrote a short blog post about this topic: Accessing environment variables within Puppet

A short summary: Facter is running on every Puppet agent system. Simply enhance Facter to collect all environment variables and then you can access them within Puppet manifests.

Hope that helps!

PythonLearner
  • 1,032
  • 2
  • 12
  • 31