Using environment variables in Puppet

Welcome back to the next part of my little series “Cfengine 3 vs. Puppet”.  When sticking to the order of the Cfengine 3 blog post series, our next topic would be something like “using the reporting feature of Puppet”, but unfortunately I do not know how to make use of a reporting feature in Puppet (or there is no such thing).

That is why we move on to the next topic: Accessing environment variables in Puppet! I searched the Internet for documentation about this specific feature; however, all I found was “this is not a built-in feature of Puppet” and a ruby script which enhances Facter.

That is why we have two “scripts” this time.

Writing the Facter script and Puppet manifest
At first create the file /etc/puppet/modules/customfacts/lib/facter/env.rb:

ENV.each do |k,v|
	Facter.add("env_#{k.downcase}".to_sym) do
		setcode do

Then create the Puppet manifest /etc/puppet/manifests/xenuser_org-009-using_environment_variables.pp:

file { "demonstrate_env_var_usage":
	path => "/etc/myenvfile",
	ensure => present,
	mode => 0640,
	content => "Content of env var PWD: $env_pwd",

Now let’s see if Facter now collects all the environment variables on the target system:

mintbox manifests # facter |grep env
env__ => /usr/bin/facter
env_colorterm => gnome-terminal
env_display => :0.0
env_home => /root
env_lang => C
env_logname => root
env_mail => /var/mail/root
env_oldpwd => /usr/lib/ruby/vendor_ruby/facter
env_path => /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
env_pwd => /etc/puppet/manifests
env_shell => /bin/bash
env_shlvl => 1
env_sudo_command => /bin/su
env_sudo_gid => 1000
env_sudo_uid => 1000
env_sudo_user => valentin
env_term => xterm
env_user => root
env_username => root
env_xauthority => /home/valentin/.Xauthority
env_xdg_session_cookie => af6d4563e613c62d87f9eb7400000008-1360505842.231246-1540805281

Yay! Facter now sees our environment variables. Now let’s move on:

mintbox manifests # puppet apply xenuser_org-009-using_environment_variables.pp 
Warning: Could not retrieve fact fqdn
Notice: /Stage[main]//File[demonstrate_env_var_usage]/ensure: created
Notice: Finished catalog run in 0.02 seconds
mintbox manifests # cat /etc/myenvfile 
Content of env var PWD: /etc/puppet/manifests

Great, it works! We now can access environment variables within Puppet. Please do not forget that you have to place that ruby script (env.rb) on every Puppet agent system.

Analyzing the Facter script and Puppet manifest
In the first step we created a custom ruby script and placed it on the right location where Facter can find it. Facter runs on every system there the Puppet agent is installed.

Facter now adds the environment variables to the catalogue; that is the reason why we can access our environment variables within Puppet manifests.

The puppet manifest itself is very simple and self-explaining.

Learning that accessing environment variables is not possible within Puppet manifests was a big disappointment. At least we are able to enhance Facter and help ourselves.

The source of the env.rb script is the old Puppet wiki:

As usual, you can download all “scripts” from this blog post here: env.rb, xenuser_org-009-using_environment_variables.pp

2 thoughts on “Using environment variables in Puppet

  1. Another option to do this:

Comments are closed.