Set Environment Variables with Puppet
Solution 1
If you only need the variables available in the puppet run, whats wrong with :
Exec { environment => [ "foo=$bar" ] }
?
Solution 2
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
Solution 3
You can set an environment variable by defining it on a line in /etc/environment
and you can ensure a line inside a file using file_line
in puppet. Combine these two into the following solution:
file_line { "foo_env_var":
ensure => present,
line => "Foo=${bar}",
path => "/etc/environment",
}
Solution 4
The way I got around it is to also use /etc/profile.d:
$bar = 'bar'
file { "/etc/profile.d/my_test.sh":
content => "export Foo=${bar}",
mode => 755
}
This ensures that everytime you login (ex ssh), the variable $MYVAR gets exported to your environment. After you apply through puppet and login (ex ssh localhost), echo $Foo would return bar
Solution 5
You could try the following, which sets the environment variable for this exec
:
class foobar {
exec { 'foobar' :
command => "/bin/bash -c \"export Foo=${bar}\"",
}
}
bgrantdev
Updated on May 09, 2021Comments
-
bgrantdev about 3 years
I am using vagrant with puppet to set up virtual machines for development environments. I would like to simply set a few environment variables in the .pp file. Using virtual box and a vagrant base box for Ubuntu 64 bit.
I have this currently.
$bar = 'bar' class foobar { exec { 'foobar': command => "export Foo=${bar}", } }
but when provisioning I get an error: Could not find command 'export'.
This seems like it should be simple enough am I missing some sort of require or path for the exec type? I noticed in the documentation there is an environment option to set up environment variables, should I be using that?
-
bgrantdev over 10 yearsThanks for the response I actually found that solution. While it removes the error about the command not being found it unfortunately does not produce the expected result. The environment variable Foo is not set when I ssh into the virtual machine.
-
ptierno over 10 yearsexec statements wont export the variable for when he logs in to the server.
-
John over 10 yearsI know that - thats why i said 'if you only need the variables available in the puppet run'. The question does not state that the variables are required to be set in user shells
-
Felix Frank about 9 yearsThis is a noop. It modifies the environment of a shell that has been forked specifically for this
exec
resource. This shell then immediately terminates. The export has no effect. -
7yl4r almost 7 yearsI like the simple & succinct fix much more than I dislike that this pollutes my
/etc/environment
. Thanks. -
N. DaSilva over 5 yearsThis is the cleanest solution if you need to coexist with other environment variables in your /etc/environment file. You can also use the match parameter to ensure that the line isn't duplicated after it is changed.
-
LanFeusT almost 3 yearsHow would you use that variable? Just
$foo_env_var
? -
7yl4r almost 3 years
foo_env_var
is the name of the line used by puppet only. The environment variable being set here isFoo
so using it will be like$Foo
- eg:echo $Foo