2

I need to set OS environment variables on my Centos/7 machine.

I've tried it using on my variables.rb file:

host = 'locahost'
port = 9201

ENV['LV_ES_HOST'] = host
ENV['LV_ES_PORT'] = '#{port}'

Nevertheless, on shell:

$ echo $LV_ES_HOST

$
  1. How could I set an Os environment variable?
  2. Could it be possible to set a OS enviroment variable only visible for an user?

I need a OS available environment variable in order for my jee applications to be able to read its value.

I'm running several jee containers on my machine, and the applications are running on need to get the LC_ES_HOST environment variable.

Jordi
  • 20,868
  • 39
  • 149
  • 333
  • 4
    Possible duplicate of [How can you use a Chef recipe to set an environment variable?](http://stackoverflow.com/questions/6284517/how-can-you-use-a-chef-recipe-to-set-an-environment-variable) – Tensibai Mar 07 '17 at 10:04
  • What about user environment variables? I remember that I was able to set which environment variables a user has availaible... – Jordi Mar 08 '17 at 07:23
  • Well your question start with OS env var. but they will be available to each user also. If your need is when starting a daemon there's other way but this usually depends on the target app. – Tensibai Mar 08 '17 at 07:33

1 Answers1

2

The other question doesn't really explain the underlying problem. Unix environment variables are inherited from parent process to child process during a fork(). So when you set things via ENV in your Chef code, it does affect subprocesses that come under Chef like things run via an execute resource. When you log in via SSH, however, your shell is not a subprocess of chef-client so those variables aren't visible. Unfortunately Unix has no general purpose solution for global environment variables so you need to pick one of a number of compromises, which are listed in the other question.

coderanger
  • 52,400
  • 4
  • 52
  • 75
  • What about user environment variables? I remember that I was able to set which environment variables a user has availaible... – Jordi Mar 08 '17 at 07:24
  • 1
    You would have to set per-user variables in the shell init scripts for that user in their home directory. – coderanger Mar 08 '17 at 08:06
  • Yes, I meant that. Nevertheless, is it able to do using chef? – Jordi Mar 08 '17 at 08:09
  • 1
    Sure, those are just files so you can write them like any other file using a `template` or `file` resource. – coderanger Mar 08 '17 at 08:12