18

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

2
  • Do you mean a variable generated by facter?
    – Scott Pack
    Mar 30, 2010 at 0:49
  • I mean the bash environment variables like $PATH and $USER.
    – joeforker
    Mar 30, 2010 at 12:44

5 Answers 5

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 ?

4
  • 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, 2010 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. Jan 15, 2013 at 17:00
  • 2
    there is broken link
    – kenorb
    Jun 9, 2014 at 12:10
  • According to the new doc: puppet.com/docs/puppet/latest/core_facts.html the path and identity are returning the data the OP requested.
    – рüффп
    May 1, 2020 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")
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
  • 1
    The link doesn't work anymore. Could you please update the answer accordingly?
    – N A
    Mar 10, 2019 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
-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!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .