puppet - How do I append to path variable?

╄→гoц情女王★ 提交于 2019-12-07 04:56:23

问题


I have access to just a module for our team, where as the global manifests are maintained by infrastructure team. The PATH variable gets set in the global manifests.

I want to append to the PATH variable, but puppet ignores 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 append to the PATH variable?

edit

I should have mentioned, I am looking to enhance the PATH variable for users's shell environment, rather than puppet's execution environment.


回答1:


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',
}



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/25250006/puppet-how-do-i-append-to-path-variable

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