puppet - How do I append to path variable?

拜拜、爱过 提交于 2019-12-05 09:37:21
Felix Frank

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

To append something to the PATH of all new login shells, you need to change the profile configuration file. If you're using a recent version of bash, there should be a /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',
}

Three problems:

1) You cannot access local client environment variables like PATH and JAVA_HOME unless you have a facter script that injects them into your Puppet client environment. My guess is that you don't.

2) Exec blocks set up their own local environment that is destroyed at the end of the Exec block. So you can set the path in an Exec block all you want and it won't do a thing for the rest of your blocks. See provider/exec.rb in the Puppet source code.

3) Unless some other block has a before => Exec["my_exec_block"] in it, the Exec block will run in some arbitrary semi-random order, probably not when you want it to run.

Your best bet is to run the action as a script and set up 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 would have any PATH assignments in it that you needed.

This is sort of clunky, but that's Puppet.

Here is an example of how to append to the path:

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

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

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

Sadly, the resource override cannot be circumventend - the +> syntax is only valid there.

I didn't double check wether this leads to a prepended path or appended (I'd assume the latter), so if that is of significance to you, you will want to double check.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!