Puppet - How to add to path variable?

I have access to a module for our team where global manifests are maintained by the infrastructure team. The variable is PATH

set in global manifests.

I want to add to a variable PATH

, but puppet is ignoring my exec block.

file { "/etc/profile.d/set_java_home.sh":
    ensure => file,
    source => "puppet:///modules/teamXXX/set_java_home.sh",
    mode => "u=rw,go=r"
}

Exec { path => [ "\${PATH}", "\${JAVA_HOME}/bin" ] }

      

How can I add to a variable PATH

?

change

I should have mentioned, I want to improve the variable PATH

for the user shell environment, not the puppet environment.

+3


source to share


3 answers


Puppet cannot change the shell environment. No subprocess - the environment is copied into each child process, which then only has access to its individual copy.

To add something to PATH

all new login shells, you need to modify the config file profile

. If you are using the latest version bash

, there should be /etc/profile.d

. You can use a resource like this:



file { '/etc/profile.d/append-java-path.sh':
    mode    => '644',
    content => 'PATH=$PATH:/my/java/home/bin',
}

      

+5


source


Here's an example of adding to a path:

Exec { path => [ '/bin' ] }

exec { [ 'ls', 'who' ]: returns => 0; }

Exec[who] { path +> [ '/usr/bin' ] }

      



Unfortunately, overriding resources cannot be a workaround - the syntax +>

is only valid there .

I haven't double checked, this results in a provisional path or append (I would prefer the latter), so if that matters to you, you need to double check.

+1


source


Three problems:

1) You cannot access local client environment variables like PATH and JAVA_HOME unless you have a factor script that injects them into your Puppet client environment. I am assuming you do not.

2) Exec blocks set up their local environment, which is destroyed at the end of the Exec block. This way, you can set the path in the Exec block to whatever you want and it won't do anything for the rest of your blocks. See Provider / exec.rb in Puppet source code.

3) If any other block has => Exec ["my_exec_block"] in front of it, the Exec block will run in an arbitrary semi-random order, perhaps not when you want to run it.

Your best bet is to run the activity as a script and set the PATH inside the actual script. Thus:

file { "/opt/myapp/install_java_app":
      notify => Exec["install_java_app"],
      mode => 755,
      source => "puppet:///modules/myapp/install_java_app",
      before => Exec["install_java_app"]
    }
exec { "install_java_app" :
      path => "/usr/bin:/usr/sbin:/bin:/sbin:/opt/myapp",
      command => "install_java_app",
      refreshonly => true
    }

      

Then / opt / myapp / install _java_app will have whatever PATH assignments you need.

It's kind of clumsy, but this Puppet.

0


source







All Articles