# Setting the value of variable in ruby logstash

**URL:** <https://discuss.elastic.co/t/setting-the-value-of-variable-in-ruby-logstash/95130>\
**Category:** Logstash\
**Created:** [July 31, 2017, 7:14am UTC](https://discuss.elastic.co/t/setting-the-value-of-variable-in-ruby-logstash/95130 "2017-07-31T07:14:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![deepanshu\_mittal](https://avatars.discourse-cdn.com/v4/letter/d/c6cbf5/32.png) [@deepanshu\_mittal](https://discuss.elastic.co/u/deepanshu_mittal)\
**Post date:** [July 31, 2017, 7:14am UTC](https://discuss.elastic.co/t/setting-the-value-of-variable-in-ruby-logstash/95130/1 "2017-07-31T07:14:14Z")

</div>

I am trying to set the value in ruby logstash variable but it is saying me undefined variable.

if "el5" in [message] {  
ruby {  
init =\> "@@linuxversion='5'"  
code =\> "event.set('linux','(@@linuxversion)')  
puts linux"  
}  
drop {}  
}

I am getting error :

06:21:54.143 [[main]\>worker0] ERROR logstash.filters.ruby - Ruby exception occurred: undefined local variable or method `linux' for #LogStash::Filters::Ruby:0x5d1d0122

I want to use this variable through out in my logstash filter.

Can you help how to resolve this issue ?

---

<div class="post-metadata">

**Author:** ![paz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paz/32/28003_2.png) [@paz](https://discuss.elastic.co/u/paz)\
**Post date:** [July 31, 2017, 8:41am UTC](https://discuss.elastic.co/t/setting-the-value-of-variable-in-ruby-logstash/95130/2 "2017-07-31T08:41:26Z")

</div>

Just remove the line I've pointed.

```auto
if "el5" in [message] {
    ruby {
    init => "@@linuxversion='5'"
    code => "
        event.set('linux',@@linuxversion)
        puts linux <--- this line is the issue
    "
    }
    drop {}
}
```

Also, why are you using a drop just after the ruby filter? It will discard the event althogether.

---

<div class="post-metadata">

**Author:** ![deepanshu\_mittal](https://avatars.discourse-cdn.com/v4/letter/d/c6cbf5/32.png) [@deepanshu\_mittal](https://discuss.elastic.co/u/deepanshu_mittal)\
**Post date:** [July 31, 2017, 9:20am UTC](https://discuss.elastic.co/t/setting-the-value-of-variable-in-ruby-logstash/95130/3 "2017-07-31T09:20:32Z")

</div>

I need to set the variable value on the basis of this line and then use in further cases. Nd also i dont want to parse this line as this is of none use except that.

---

<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:** [August 2, 2017, 12:54pm UTC](https://discuss.elastic.co/t/setting-the-value-of-variable-in-ruby-logstash/95130/4 "2017-08-02T12:54:47Z")

</div>

If you set a field value with event.set('fieldname') you can retrieve it with event.get('fieldname').

> I need to set the variable value on the basis of this line and then use in further cases.

You mean future events? Then you can't use event.set(). That sets a field in the current event which isn't carried over to other events. Setting a variable like @@linuxversion might work though.

---

<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:** [August 30, 2017, 12:55pm UTC](https://discuss.elastic.co/t/setting-the-value-of-variable-in-ruby-logstash/95130/5 "2017-08-30T12:55:11Z")

</div>

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