Setting environment variables with puppet

拈花ヽ惹草 提交于 2019-12-09 04:42:48

问题


I'm trying to work out the best way to set some environment variables with puppet.

I could use exec and just do export VAR=blah. However, that would only last for the current session. I also thought about just adding it onto the end of a file such as bashrc. However then I don't think there is a reliable method to check if it is all ready there; so it would end up getting added with every run of puppet.


回答1:


I would take a look at this related question.

*.sh scripts in /etc/profile.d are read at user-login time (as the post says, at the same time /etc/profile is sourced)

Variables export-ed in any script placed in /etc/profile.d will therefore be available to your users.

You can then use a file resource to ensure this action is idempotent. For example:

file { "/etc/profile.d/my_test.sh":
  content => 'export MYVAR="123"'
}



回答2:


Or an alternate means to an indempotent result:

Example

if [[ ! grep PINTO_HOME /root/.bashrc | wc -l > 0 ]] ; then
        echo "export PINTO_HOME=/opt/local/pinto" >> /root/.bashrc ;
fi

This option permits this environmental variable to be set when the presence of the pinto application makes it warrented rather than having to compose a user's .bash_profile regardless of what applications may wind up on the box.




回答3:


If you add it to your bashrc you can check that it's in the ENV hash by doing

ENV[VAR]

Which will return => "blah"




回答4:


If you take a look at Github's Boxen they source a script (/opt/boxen/env.sh) from ~/.profile. This script runs a bunch of stuff including:

for f in $BOXEN_HOME/env.d/*.sh ; do
  if [ -f $f ] ; then
    source $f
  fi
done

These scripts, in turn, set environment variables for their respective modules.




回答5:


If you want the variables to affect all users /etc/profile.d is the way to go.

However, if you want them for a specific user, something like .bashrc makes more sense.

In response to "I don't think there is a reliable method to check if it is all ready there; so it would end up getting added with every run of puppet," there is now a file_line resource available from the puppetlabs stdlib module:

"Ensures that a given line is contained within a file. The implementation matches the full line, including whitespace at the beginning and end. If the line is not contained in the given file, Puppet appends the line to the end of the file to ensure the desired state. Multiple resources can be declared to manage multiple lines in the same file."

Example:

file_line { 'sudo_rule':
  path => '/etc/sudoers',
  line => '%sudo ALL=(ALL) ALL',
}

file_line { 'sudo_rule_nopw':
  path => '/etc/sudoers',
  line => '%sudonopw ALL=(ALL) NOPASSWD: ALL',
}


来源:https://stackoverflow.com/questions/15440972/setting-environment-variables-with-puppet

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