Cannot access environment variables

Hi,

My config is really simple, I'm just trying to get a simple envornment variable to be accessed and used by a Logstash pipeline. It looks like this:

input{
  file {  
    path => "${test}"
  }
}

filter {}

output {
 stdout{ }       
}

before I run this config I exported the path like this:
export path="/path/to/file/*.csv"

And I get this error:
"Unable to configure plugins: Cannot evaluate ${test}. Replacement variable test is not defined in a Logstash secret store or an environment entry and there is no default value given.

The docs don't indicate that I need to do anything other than export the variable but I can't figure it out. What am I doing wrong?

The name of variable should match the name you export it as.

export PATH="/path/to/file/*.csv"
path => "${PATH}"

sorry, that was a typo.

the variable exported is in fact test and i validated that by running env and seeing the test variable there.

How are you running logstash?

from the command line with this line:

sudo /usr/share/logstash/bin/logstash -f /home/varTest.config

You are exporting the variable as a normal user and running logstash with sudo, which runs under root and the root user environment does not have the variable you exported before.

To solve this you need to tell sudo to preserve the variable.

Something like this:

$ export VARNAME="VALUE"
$ sudo --preserve-env=VARNAME /usr/share/logstash/bin/logstash -f /home/varTest.config

You can also run this way:

$ sudo VARNAME="VALUE" /usr/share/logstash/bin/logstash -f /home/varTest.config
2 Likes

That worked, thank you!

Can I ask if there are any similar nuances when using variables in pipelines that are configured and set up within the Elastic UI?

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