Set Environment Variables with Puppet

2020-02-10 02:18发布

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?

7条回答
做个烂人
2楼-- · 2020-02-10 02:37

Something like this would work while preserving existing contents of the /etc/environment file:

/code/environments/{environment}/manifests/environment/variable.pp:

define profile::environment::variable (
    $variable_name,
    $value,
    $ensure => present,
) {
    file_line { $variable_name:
        path   => '/etc/environment',
        ensure => $ensure,
        line   => "$variable_name=$value",
        match  => "$variable_name=",
    }
}

Usage (in the body of a node manifest):

profile::environment::variable { 'JAVA_HOME':
    variable_name  => 'JAVA_HOME',
    value => '/usr/lib/jvm/java-1.8.0',
}
查看更多
虎瘦雄心在
3楼-- · 2020-02-10 02:38

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",
}
查看更多
Explosion°爆炸
4楼-- · 2020-02-10 02:49

If you only need the variables available in the puppet run, whats wrong with :

Exec { environment => [ "foo=$bar" ] }

?

查看更多
【Aperson】
5楼-- · 2020-02-10 02:51

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

查看更多
走好不送
6楼-- · 2020-02-10 02:51

I know this is an old question, but I was able to set the PS1 prompt value and add it to my .bashrc file like this:

$PS1 = '\[\e[0;31m\]\u\[\e[m\] \[\e[1;34m\]\w\[\e[m\] \$ '

and within a class:

exec {"vagrant-prompt":
  unless =>  "grep -F 'export PS1=\"${PS1}\"' ${HOME_DIR}/.bashrc",
  command => "echo 'export PS1=\"${PS1}\"' >> ${HOME_DIR}/.bashrc",
  user => "${APP_USER}",
}

The -F makes grep it interpret it as a fixed string. Otherwise it won't find it and keeps adding to the .bashrc file.

查看更多
时光不老,我们不散
7楼-- · 2020-02-10 02:56

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

查看更多
登录 后发表回答