How do I access an environment variable in a puppet manifest?

Solution 1:

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 ?

Solution 2:

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

Solution 3:

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.

Solution 4:

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