Question

I am writing some salt stack formulas which will install tomcat package. but after installation I have to set JAVA_HOME in /etc/default/tomcat7 file. Is there any option to set JAVA_HOME? Or is there any option to modify or add JAVA_HOME in environment variable (i.e. in .bashrc or .profile files)?

My pillar.example file will have these details:

    tomcat_version: 7
    java_home: '/usr/lib/jvm/java-7-oracle'
Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top