Set environment variables with puppet

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 close merge delete 2 Answers Sort by » oldest newest most voted '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'
}

more

1

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.

( 2013-08-23 18:19:16 -0500 )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!

( 2013-08-23 18:44:08 -0500 )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)

( 2013-08-23 19:33:14 -0500 )edit

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

( 2013-08-24 13:32:54 -0500 )edit

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 (http://docs.puppetlabs.com/hiera/1/puppet.html#automatic-parameter-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

more

1

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

( 2013-08-25 05:51:28 -0500 )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).

( 2013-12-15 15:53:49 -0500 )edit

@xraj Sudo aside, adding to /etc/environment would appear to be the canonical solution for Ubuntu. See: https://help.ubuntu.com/community/EnvironmentVariables#System-wide_environment_variables

( 2014-02-03 14:18:31 -0500 )edit