# Environment variables in if..then logic

**URL:** https://discuss.elastic.co/t/environment-variables-in-if-then-logic/197099
**Category:** Logstash
**Created:** [August 28, 2019, 11:34am UTC](https://discuss.elastic.co/t/environment-variables-in-if-then-logic/197099 "2019-08-28T11:34:20Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![pgf](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pgf/32/47996_2.png) [@pgf](https://discuss.elastic.co/u/pgf)
#### Post date: [August 28, 2019, 11:34am UTC](https://discuss.elastic.co/t/environment-variables-in-if-then-logic/197099/1 "2019-08-28T11:34:20Z")

</div>

I am using a number of environment variables in my logstash config which all work well, except for one. In my output section I am trying to use a variable in an if.. then statement and it does not appear to be working. Perhaps it is my syntax. The user, password and proxy variables substitute correctly, but the code always takes the first branch regardless of the value of ${LS\_USE\_PROXY}. What am I doing wrong?

```
if "${LS_USE_PROXY}" == "true" {
    elasticsearch {
        hosts => ["https://xxx.eu-central-1.aws.cloud.es.io:443"]
        index => "%{[@metadata][index_alias]}"
        user => "${LS_ELASTIC_USER}"
        password => "${LS_ELASTIC_PASSWD}"
        proxy => "${LS_PROXY}"
    }
} else {
    elasticsearch {
        hosts => ["https://xxx.eu-central-1.aws.cloud.es.io:443"]
        index => "%{[@metadata][index_alias]}"
        user => "${LS_ELASTIC_USER}"
        password => "${LS_ELASTIC_PASSWD}"
    }
}
```

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [August 28, 2019, 12:13pm UTC](https://discuss.elastic.co/t/environment-variables-in-if-then-logic/197099/2 "2019-08-28T12:13:37Z")

</div>

> [@pgf](#):
>
> What am I doing wrong?

This is not [supported](https://github.com/elastic/logstash/issues/5115). The workaround is to use mutate+add\_field to add a field to [@metadata] then test that.

---

<div class="post-metadata">

### Author: ![pgf](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pgf/32/47996_2.png) [@pgf](https://discuss.elastic.co/u/pgf)
#### Post date: [August 28, 2019, 1:05pm UTC](https://discuss.elastic.co/t/environment-variables-in-if-then-logic/197099/3 "2019-08-28T13:05:21Z")

</div>

Sorry @Badger, what specifically is not supported? Using an environment variable in a condition?

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [August 28, 2019, 1:51pm UTC](https://discuss.elastic.co/t/environment-variables-in-if-then-logic/197099/4 "2019-08-28T13:51:52Z")

</div>

Exactly.

---

<div class="post-metadata">

### Author: ![pgf](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pgf/32/47996_2.png) [@pgf](https://discuss.elastic.co/u/pgf)
#### Post date: [August 30, 2019, 2:53pm UTC](https://discuss.elastic.co/t/environment-variables-in-if-then-logic/197099/5 "2019-08-30T14:53:06Z")

</div>

Ok. Thanks.

---

<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: [September 27, 2019, 2:53pm UTC](https://discuss.elastic.co/t/environment-variables-in-if-then-logic/197099/6 "2019-09-27T14:53:09Z")

</div>

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