# Environment variables in IF-statement

**URL:** https://discuss.elastic.co/t/environment-variables-in-if-statement/63613
**Category:** Logstash
**Created:** [October 21, 2016, 12:45pm UTC](https://discuss.elastic.co/t/environment-variables-in-if-statement/63613 "2016-10-21T12:45:12Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![krzysztof\_pl](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@krzysztof\_pl](https://discuss.elastic.co/u/krzysztof_pl)
#### Post date: [October 21, 2016, 12:45pm UTC](https://discuss.elastic.co/t/environment-variables-in-if-statement/63613/1 "2016-10-21T12:45:12Z")

</div>

hi Everyone!

My goal is to 'implement' filter to process only traces with specific levels.  
Perfect solution: levels should be defined in separated file or environment variable.

So, I would like to achive it in this way:  
`if [level] in [${MY_LEVEL_FILTER}] { ... // to some logic } else { drop {} }`  
For some reason above solution does not work correctly, nor:  
`if [level] == MY_LEVEL_OK_1 or [level] == MY_LEVEL_OK_2 {`

Hard-coded IF-statement works:  
`if [level] in ["INFO", "ERROR"] { ... }`

I have got Logstash 2.4. It works as a service with flag --allow-env  
(So it is according to the [documentation](https://www.elastic.co/guide/en/logstash/current/environment-variables.html); and I have noted ' **experimental**' word)

This is what I did to make 'environemt variables' achievable:

1. In /etc/init.d/logstash file I put  
`export MY_LEVEL_FILTER='"INFO", "ERROR"' export MY_LEVEL_OK_1=INFO export MY_LEVEL_OK_2=ERROR`

( just to make sure I call  
`sudo /opt/logstash/bin/logstash -f /etc/logstash/conf.d/mylogstash1.conf --configtest` )  
and I call:  
`sudo service logstash start`

I have got some enviroment variables, e.g.:  
`MY_INPUT_FILE_NAME=/path/to-input-file MY_LEVEL_FILTER=["INFO", "WARN", "ERROR"]`

Below example works perfectly fine:  
`input { file { path => "${MY_INPUT_FILE_NAME}" } }`  
What is more, in other plugin (in Ruby source code)  
I have got: `puts ENV['MY_LEVEL_FILTER']`  
which displays desired string.

Maybe ENV in Ruby source code is something else than in logstash configuration file?  
Maybe above pieces of information will be useful for someone.  
Thank you for helping.

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [October 23, 2016, 4:59pm UTC](https://discuss.elastic.co/t/environment-variables-in-if-statement/63613/2 "2016-10-23T16:59:14Z")

</div>

```
if [level] in [${MY_LEVEL_FILTER}] {

```

And you expect that if you set MY\_LEVEL\_FILTER to `"a", "b", "c"` that the line above would be equivalent to this?

```
if [level] in ["a", "b", "c"] {

```

That won't work. The variable expansion takes place later when Logstash has already parsed the configuration file.

I'd use a template language to generate the configuration files.

---

<div class="post-metadata">

### Author: ![krzysztof\_pl](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@krzysztof\_pl](https://discuss.elastic.co/u/krzysztof_pl)
#### Post date: [October 23, 2016, 7:53pm UTC](https://discuss.elastic.co/t/environment-variables-in-if-statement/63613/3 "2016-10-23T19:53:08Z")

</div>

hi, thank you for your reply. I have implemented "workaround":  
One step before I added certain fields based on environment variables and I used those fields in IF-statement. It works fine 🙂

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 6, 2017, 4:33am UTC](https://discuss.elastic.co/t/environment-variables-in-if-statement/63613/4 "2017-07-06T04:33:09Z")

</div>


