Search code examples
rubyjrubyenvironment-variablesproduction-environmentpuppet

How to set an environment variable on a server with puppet?


I'm starting to use puppet in my current project and I'm having some issues.

I'm using a recipe to install jruby, but I want to set a environment variable (in this case, JRUBY_HOME and modify the PATH to include JRUBY_HOME/bin) after it finishes installing jruby.

Here's the recipe:

class jruby {
  $jruby_home = "/opt/jruby"


  exec { "download_jruby":
    command => "wget http://jruby.org.s3.amazonaws.com/downloads/1.7.0.RC2/jruby-bin-1.7.0.RC2.tar.gz",
    path => $path,    
    timeout => 0,
    unless => "ls /opt | grep jruby-1.7.0",
    require => Package["openjdk-7-jre-headless"]
  }                

  exec { "unpack_jruby" :
    command => "tar -zxf jruby-bin-1.7.0.RC2.tar.gz -C /opt",
    path => $path,
    creates => "${jruby_home}-1.7.0.RC2",
    require => Exec["download_jruby"]
  }                             

  file { $jruby_home:
    ensure => link,
    target => "${jruby_home}-1.7.0.RC2",
    require => Exec["unpack_jruby"]
  }                          

}  

So, what's the best way to add /opt/jruby as JRUBY_HOME and then add JRUBY_HOME/bin to PATH?


Solution

  • Solved it:

    # init.pp
    $jruby_sh = "/etc/profile.d/jruby.sh"
    file { $jruby_sh:
      ensure => present,
      source => "puppet:///modules/jruby/jruby.sh",
      owner => "root",
      group => "root",
      mode => 644,
      require => File[$jruby_home]
    }
    
    # jruby.sh
    export JRUBY_HOME=/opt/jruby
    export PATH=$PATH:$JRUBY_HOME/bin