# Logstash environment plugin doesn't seem to work

**URL:** <https://discuss.elastic.co/t/logstash-environment-plugin-doesnt-seem-to-work/38872>\
**Category:** Logstash\
**Created:** [January 11, 2016, 11:51am UTC](https://discuss.elastic.co/t/logstash-environment-plugin-doesnt-seem-to-work/38872 "2016-01-11T11:51:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![schmorgs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/schmorgs/32/39476_2.png) [@schmorgs](https://discuss.elastic.co/u/schmorgs)\
**Post date:** [January 11, 2016, 11:51am UTC](https://discuss.elastic.co/t/logstash-environment-plugin-doesnt-seem-to-work/38872/1 "2016-01-11T11:51:39Z")

</div>

I want to use an environment variable in my logstash config and see there is an "environment" input community plugin which has a function called add\_metadata\_from\_env.

I've installed the plugin (under Logstash 2.1.0) and have this configuration :

```auto
environment {
    add_metadata_from_env => { "this_host" => "HOSTNAME" }
    add_field => ["hostname", "%{[@metadata][this_host]}" ]
  }

```

But when the event is sent to Elasticsearch, the field value is the literal text and not the value of the env variable.

```auto
hostname %{[@metadata][this_host]}

```

I've tried various ways of tying to reference the variable but nothing seems to work.

Any ideas?

---

<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:** [January 11, 2016, 1:35pm UTC](https://discuss.elastic.co/t/logstash-environment-plugin-doesnt-seem-to-work/38872/2 "2016-01-11T13:35:40Z")

</div>

HOSTNAME isn't a "real" environment variable (see below) so you can't use it here. Logstash normally adds the current hostname as a field so I'm surprised that you feel a need to add it from an environment variable. But if you insist you can inject the hostname via a ruby filter.

> <https://superuser.com/questions/132489/hostname-environment-variable-on-linux>

---

<div class="post-metadata">

**Author:** ![schmorgs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/schmorgs/32/39476_2.png) [@schmorgs](https://discuss.elastic.co/u/schmorgs)\
**Post date:** [January 13, 2016, 9:34am UTC](https://discuss.elastic.co/t/logstash-environment-plugin-doesnt-seem-to-work/38872/3 "2016-01-13T09:34:44Z")

</div>

The reason for this is that we have a Logstash Shipper sending messages to a load balancer, behind which there is a pool of Logstash Indexers.

We want to tag the hostname of the Indexer in the message so we can trace messages and measure throughput across the pool.

If HOSTNAME is not classed as a real environment variable (which seems odd to me but there we go), I'll look at the Ruby filter option instead.  
Thanks

---

<div class="post-metadata">

**Author:** ![gaurav1424](https://avatars.discourse-cdn.com/v4/letter/g/97f17d/32.png) [@gaurav1424](https://discuss.elastic.co/u/gaurav1424)\
**Post date:** [February 17, 2016, 12:05am UTC](https://discuss.elastic.co/t/logstash-environment-plugin-doesnt-seem-to-work/38872/4 "2016-02-17T00:05:34Z")

</div>

For me this didnt work.

here is example:

My input file :  
filter {  
environment {  
add\_metadata\_from\_env =\> {"region" =\> "REGION\_ENV"}  
add\_field =\> ["my\_env", "%{[@metadata][region]}"]  
}  
}

bash-3.2$ echo $REGION\_ENV  
amer  
bash-3.2$

My output :  
{  
"message" =\> "Hello world\r",  
"@version" =\> "1",  
"@timestamp" =\> "2016-02-17T00:04:13.540Z",  
"host" =\> "0:0:0:0:0:0:0:1",  
"port" =\> 61248,  
"my\_env" =\> "%{[@metadata][region]}"  
}

Anything I am missing here ?

---

<div class="post-metadata">

**Author:** ![gaurav1424](https://avatars.discourse-cdn.com/v4/letter/g/97f17d/32.png) [@gaurav1424](https://discuss.elastic.co/u/gaurav1424)\
**Post date:** [February 17, 2016, 12:44am UTC](https://discuss.elastic.co/t/logstash-environment-plugin-doesnt-seem-to-work/38872/5 "2016-02-17T00:44:15Z")

</div>

I tried this as well as mentioned on other thread.

Logstash Input :  
filter {  
environment {  
add\_metadata\_from\_env =\> {"region" =\> "REGION\_ENV"}  
add\_field =\> ["my\_env", "%{[@metadata][region]}"]  
}  
}

output {  
stdout  
{ codec =\> rubydebug {  
metadata =\> true  
}  
}  
}

Logstash Output:  
{  
"message" =\> "hello world\r",  
"@version" =\> "1",  
"@timestamp" =\> "2016-02-17T00:42:35.846Z",  
"host" =\> "0:0:0:0:0:0:0:1",  
"port" =\> 61853,  
"my\_env" =\> "%{[@metadata][region]}",  
"@metadata" =\> {  
"region" =\> nil  
}  
}

---

<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:** [February 17, 2016, 6:31am UTC](https://discuss.elastic.co/t/logstash-environment-plugin-doesnt-seem-to-work/38872/6 "2016-02-17T06:31:07Z")

</div>

@gaurav1424, please start new threads for questions that don't directly relate to the existing thread.

---

<div class="post-metadata">

**Author:** ![gaurav1424](https://avatars.discourse-cdn.com/v4/letter/g/97f17d/32.png) [@gaurav1424](https://discuss.elastic.co/u/gaurav1424)\
**Post date:** [February 17, 2016, 3:44pm UTC](https://discuss.elastic.co/t/logstash-environment-plugin-doesnt-seem-to-work/38872/7 "2016-02-17T15:44:13Z")

</div>

> [@gaurav1424](#):
>
> here is example:
> 
> My input file : filter { environment { add\_metadata\_from\_env =\> {"region" =\> "REGION\_ENV"} add\_field =\> ["my\_env", "%{[@metadata][region]}"] }}
> 
> bash-3.2$ echo $REGION\_ENVamerbash-3.2$
> 
> My output : { "message" =\> "Hello world\r", "@version" =\> "1", "@timestamp" =\> "2016-02-17T00:04:13.540Z", "host" =\> "0:0:0:0:0:0:0:1", "port" =\> 61248, "my\_env" =\> "%{[@metadata][region]}"}
> 
> Anything I am missing here ?

Ok. Here you go : [Environment filter is not working](https://discuss.elastic.co/t/environment-filter-is-not-working/42040)

---

<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, 5:11am UTC](https://discuss.elastic.co/t/logstash-environment-plugin-doesnt-seem-to-work/38872/8 "2017-07-06T05:11:01Z")

</div>


