Ask Your Question

Set environment variables with puppet

asked 2013-08-23 16:29:55 -0600

bgrantdev gravatar image

I am trying to set some environment variables with puppet using exec but I am running into problems.

$bar = 'bar'

class foobar {
   exec { 'foobar':
     command => "export Foo=${bar}",
     path    => ["/usr/bin", "/usr/sbin"]

But I get an error: Could not find command 'export'. Am I just going about this the wrong way? I noticed that there is an environment parameter for exec that can be used to set variables, should I be using that and is it possible to pass in the $bar variable there?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2013-08-23 17:35:53 -0600

Trevor Vaughan gravatar image

updated 2013-08-23 18:44:23 -0600

'Export' is a shell builtin.

Your best bet is to set the variables inline with your command.

Something like:

exec { 'foo':
  environment => ["FOO=bar"],
  command => '/bin/echo $FOO > /tmp/bar'
edit flag offensive delete link more



Thanks for the response. I'm not going to use any commands that use FOO but I do need all these variables set once the virtual machine is finished setting up.

bgrantdev gravatar imagebgrantdev ( 2013-08-23 18:19:16 -0600 )edit

Sorry about my previous post. I've edited the code to be proper above. That's what I get for answering things at conferences!

Trevor Vaughan gravatar imageTrevor Vaughan ( 2013-08-23 18:44:08 -0600 )edit

I probably should have specified this in the original post, I'm using puppet with vagrant (on an Ubuntu 64 bit box). The command apparently executes correctly but when I ...(more)

bgrantdev gravatar imagebgrantdev ( 2013-08-23 19:33:14 -0600 )edit

Yeah, sorry about that, I certainly didn't get that from the original question.

Trevor Vaughan gravatar imageTrevor Vaughan ( 2013-08-24 13:32:54 -0600 )edit

answered 2013-08-23 22:17:13 -0600

extraordinaire gravatar image

Simplest way to acomplish this is to put your env vars in /etc/environment, this ensures they are available to everything (or pretty much everything).

Something like this:

class example($somevar) {
    file { "/etc/environment":
        content => inline_template("SOMEVAR=${somevar}")

Reason for having the class parameterised is so you can target it from hiera with automatic variable lookup ( ... if you're sticking something in /etc/environment, it's usually best if you actually make it environment specific.

note: I've only tested this on ubuntu

edit flag offensive delete link more



It will work also in CentOS/RedHat because they have the same mechanism ...

golja gravatar imagegolja ( 2013-08-25 05:51:28 -0600 )edit

This is a very brittle solution. It depends on processes inheriting their environment from a login shell and that the environment isn't cleared along the way (e.g sudo).

xraj gravatar imagexraj ( 2013-12-15 15:53:49 -0600 )edit

@xraj Sudo aside, adding to /etc/environment would appear to be the canonical solution for Ubuntu. See:

dnelson gravatar imagednelson ( 2014-02-03 14:18:31 -0600 )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

1 follower


Asked: 2013-08-23 16:29:55 -0600

Seen: 47,140 times

Last updated: Aug 23 '13