How do I use environment variables when running as service?

I am using Logstash v5.1.0 on Ubuntu 16.04 and my logstash conf file includes an environment variable reference to set the output graphite server, but I cannot get it to work when running as a service.

My config file contains (missing a few lines for clarity):
graphite {
host => "${GRAPHITE_SERVER}"
}

and my /etc/environment file contains:
GRAPHITE_SERVER=54.40.235.208

Yet with configuration file logging, I see:
fetched an invalid config {}
reason=>"Cannot evaluate ${GRAPHITE_SERVER}. Environment variable GRAPHITE_SERVER is not set and there is no default value given."

This does work if I run logstash from bash manually.

Any help would be much appreciated - thanks.
Andrew

Hi @astickler,

I haven't tried it on 16.04 but on 14.04 it works when exporting it in /etc/default/logstash.
E.g. export GRAPHITE_SERVER=54.40.235.208.
I know, it sounds strange to prefix with export in that file, see https://github.com/elastic/logstash/issues/6414 for discussion.

14.04 uses upstart while 16.04 is using systemd, so they are probably souring the file in different ways, but it's worth a try.

Hi Jakob - I had already tried that, but it also doesn't work.
Andrew

I just tried this on a 16.04 VM and adding it to /etc/default/logstash with the export prefixed resulted in:

systemd[1]: logstash.service: Ignoring invalid environment assignment 'export LS_FPATH=/tmp/': /etc/default/logstash

However, just using LS_FPATH="/tmp/" worked for me.

# cat /etc/default/logstash 
LS_FPATH="/tmp/"

# echo 123 | ncat localhost 5011

# cat /tmp/lsout.log 
{"@timestamp":"2017-01-13T15:34:23.971Z","port":43886,"@version":"1","host":"127.0.0.1","message":"123"}

# cat /etc/logstash/conf.d/ls.conf 
#
input {
	tcp { port => 5011}
}


output {
	file {
		path => "${LS_FPATH}/lsout.log"
	}
}

I tested with Logstash 5.1.2, mind giving that a try and using my simple config, for testing?

Jakob - this is good news, your test worked with my currently installed 5.1.1 version, and when I tried the same with my config.

I know I tried this before and it didn't work, but I have realised I may have been confused by old errors in the logstash_plain log because my service is taking nearly 2 minutes to start (I am on an AWS instance, and although I have improved the entropy, it appears it's not yet good enough).

Anyway - all working now - thanks for your help.
Andrew

Glad to hear it's working now. About the entropy issue, see this GH issue, you might want to give haveged a try. Worked good for me to resolve this.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.