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?
I should have mentioned, I am looking to enhance the PATH
variable for users's shell environment, rather than puppet's execution environment.
To add a path to the PATH environment variableIn the System dialog box, click Advanced system settings. On the Advanced tab of the System Properties dialog box, click Environment Variables. In the System Variables box of the Environment Variables dialog box, scroll to Path and select it.
In the Environment Variables window (as shown below), highlight the Path variable in the System Variable section and click the Edit button. Add or modify the path lines with the paths you want the computer to access. Each different directory is separated with a semicolon, as shown below.
The PATH variable is an environment variable containing an ordered list of paths that Linux will search for executables when running a command. Using these paths means that we don't have to specify an absolute path when running a command. Linux traverses the colon-separated paths in order until finding an executable.
The PATH environment variable is an important security control. It specifies the directories to be searched to find a command. The default systemwide PATH value is specified in the /etc/profile file, and each user normally has a PATH value in the user's $HOME/. profile file.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With