Setting environment variables with puppet

12,062

Solution 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"'
}

Solution 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.

Share:
12,062
Jake232
Author by

Jake232

Updated on June 04, 2022

Comments

  • Jake232
    Jake232 almost 2 years

    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.

  • Jake232
    Jake232 about 11 years
    Thanks, and how would I tell puppet to only run the command if it isn't within the ENV hash?
  • Spooner
    Spooner about 11 years
    It should be ENV['VAR'] to access the environment variable VAR
  • Steven Spasbo
    Steven Spasbo over 10 years
    unless ENV[VAR]; puts "This is your code"; end;