Set environment variable using saltstack

As an alternative to setting .bashrc or .profile, you could simply set the JAVA_HOME value directly in /etc/default/tomcat7:

tomcat_configuration:
  file.append:
    - name: /etc/default/tomcat7
    - text: export JAVA_HOME={{ pillar['java_home'] }}

If for some reason file.append is not suitable, salt offers file.replace and (new in 0.18.0) file.blockreplace.


Depends on the scope of the environmental variable:

If its for a single user, then a export to user's local .bash_profile would work e.g.

JAVA_HOME:
# File.append searches the file for your text before it appends so it won't append multiple times
  file.append:
    - name: /root/.bash_profile
    - text: export JAVA_HOME={{ pillar['java_home'] }}

You would probably need to log out for that user before the above variable can be used.

On the other hand, if you want to export that variable globally. You would probably want to use /etc/profile.d

JAVA_HOME:
  file.append:
    - name: /etc/profile.d/myglobalenvvariables.sh
    - text: export JAVA_HOME={{ pillar['java_home'] }}

Although I would recommend using file.managed when creating a new script. You of course, still need to log all of your users out in order for the variables to propagate.